在基于对话框的MFC应用程序中,添加一个Edit控件,ID标号为IDC_EDIT_TIME。
在OnInitDialog()函数中添加下面语句:
SetTimer(1,1000,NULL);//1000毫秒发生一次定时器事件
为类添加WM_TIMER消息响应函数OnTimer(UINT_PTR nIDEvent):
void CTestDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnTimer(nIDEvent);
CString str;
CTime theTime = CTime::GetCurrentTime();
str.Format("%02d:%02d:%02d",theTime.GetHour(),theTime.GetMinute(),theTime.GetSecond());
SetDlgItemText(IDC_EDIT_TIME,str);
}
为类添加WM_DESTROY消息响应函数OnDestroy():
void CTestDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
KillTimer(1);
}
在OnInitDialog()函数中添加下面语句:
SetTimer(1,1000,NULL);//1000毫秒发生一次定时器事件
为类添加WM_TIMER消息响应函数OnTimer(UINT_PTR nIDEvent):
void CTestDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnTimer(nIDEvent);
CString str;
CTime theTime = CTime::GetCurrentTime();
str.Format("%02d:%02d:%02d",theTime.GetHour(),theTime.GetMinute(),theTime.GetSecond());
SetDlgItemText(IDC_EDIT_TIME,str);
}
为类添加WM_DESTROY消息响应函数OnDestroy():
void CTestDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
KillTimer(1);
}