A simple clock program

Source: Internet
Author: User

Objective:

I learn VC not how long time! From the vckbase I learned a lot of things, of course, I know some things also want to share with you, I made a small clock program, I hope with the majority of beginners to learn together, improve!

Specific process:

1. First, we build a program based on the dialog box

2, in the dialog box to establish three static text its ID is idc_hour idc_minute idc_second

3, with MFC for three ID add variable variable type CString m_hours m_minutes m_seconds

4, then map message wm_timer generate OnTimer () function

5. Add a private function to the Dlg class CurrentTime ():

void CClockDlg::CurrentTime()
{
  time_t tCurrentTime;
  ///声明
  char szBuffer [ 256 ];
  // 获取系统时间
  tCurrentTime = time ( ( time_t* ) NULL );
  ////////////
  strftime ( szBuffer, sizeof ( szBuffer ), "%H", localtime ( &tCurrentTime ) );
  m_hours = szBuffer;
  strftime ( szBuffer, sizeof ( szBuffer ), "%M", localtime ( &tCurrentTime ) );
  m_minutes = szBuffer;
  strftime ( szBuffer, sizeof ( szBuffer ), "%S", localtime ( &tCurrentTime ) );
  m_seconds = szBuffer;
  // 将各变量值赋给对话框相应ID号 对应的项
  ( GetDlgItem ( IDC_HOUR ) ) -> SetWindowText ( m_hours );
  ( GetDlgItem ( IDC_MINUTE ) ) -> SetWindowText ( m_minutes );
  ( GetDlgItem ( IDC_SECOND ) ) -> SetWindowText ( m_seconds );
}

6. Before OnInitDialog (), return TRUE:

// 完成定时器的安装
int iInstallresult;
iInstallresult=SetTimer(1,1000,NULL);
if(iInstallresult==0)
{
  MessageBox("fail to install the timer!");
}
else
  CurrentTime();

7. Add in OnTimer ()

CurrentTime();

At this point, the most basic work is done, and the rest is cosmetic decoration, you can add a function for the closure of the dialog box to achieve his blurred disappearance is not cool? We can refer to the source program to try it must be!

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.