做定时器前,要添加WM_TIMER消息的响应函数
void Ctest04Dlg::OnTimer(UINT nIDEvent)
{
//判断定时器是不是我们的定时器
if (nIDEvent == 0) {
//静态变量 n 初始值为 5 表示 5 秒
static int n = 5;
//每次调用 n 的值就减 1
n--;
//根据描述设置按钮的文本已知道还有几秒
CString Text;
Text.Format("确定(%d)", n);
GetDlgItem(IDOK)->SetWindowText(Text);
//如果时间到了
if (n == 0) {
//激活按钮
GetDlgItem(IDOK)->EnableWindow(TRUE);
//销毁定时器,这个函数的参数就是定时器编号
KillTimer(0);
//设置按钮的标题
Text.Format("确定", n);
GetDlgItem(IDOK)->SetWindowText(Text);
}
}
CDialog::OnTimer(nIDEvent);
}
下面是一个实例
void CSimpleClockDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==0)
{
static Second=0;
static Minute=0;
Second++;
if(Second==60)
{
Second=0;
Minute++;
}
CString Text;
Text.Format("%d:%d",Minute,Second);
GetDlgItem(IDOK)->SetWindowText(Text);
}
CDialog::OnTimer(nIDEvent);
}
void CSimpleClockDlg::OnButtonPause()
{
// TODO: Add your control notification handler code here
KillTimer(0);
}
void CSimpleClockDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
SetTimer(0,1000,NULL);
}