当前位置:文档之家› VC中使用定时器的方法

VC中使用定时器的方法

1.启用一个定时器直接调用函数:
SetTimer(1,500,NULL);//定义时钟1,时间间隔为500ms SetTimer(2,1000,NULL);//定义时钟2,时间间隔为1000ms 可以在按钮按下时启用定时器:
void CTimeDlg::OnButton1()
{
// TODO: Add your control notification handler code here
SetTimer(1,500,NULL);//定义时钟1,时间间隔为500ms SetTimer(2,1000,NULL);//定义时钟2,时间间隔为1000ms }
2.关闭定时器:可以在按钮中调用如下函数关闭某定时器: void CTimeDlg::OnButton2()
{
// TODO: Add your control notification handler code here
KillTimer(1); //关闭1号定时器
KillTimer(2); //关闭2号定时器
}
3.添加定时器时间到的处理代码:
1)在开发界面中Ctrl+W 进入MFCclass wizard页面2)选择Message Maps选项卡
3)在Project中选择你的工程
4)在object Ids:中选择C…..Dlg
5)在Messages:中选择WM_TIMER,此时,Member functions中自动定位到: W OnTimer ON_WM_TIMER,
6) 单击EDIT code(或双击W OnTimer ON_WM_TIMER)自动进入如下函数:void CTimeDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case 1: //1号定时器应该处理的事情
//…..
break;
case 2: //2号定时器应该处理的事情
//…..
break;
}
CDialog::OnTimer(nIDEvent); //此句VC自动生成
}。

相关主题