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