VC 定时器函数SetTimer

This method installs a system timer. A time-out value is specified, and every time a time-out occurs, the system posts a WM_TIMER message to the message queue of the installing application or passes the message to an application-defined TimerProc callback function.

UINT SetTimer( 
UINT nIDEvent

UINT nElapse

void (CALLBACK EXPORT* lpfnTimer
)( 
HWND, UINT, UINT, DWORD) );

Parameters

nIDEvent
Specifies a nonzero timer identifier.
nElapse
Specifies the time-out value, in milliseconds.
lpfnTimer
Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the WM_TIMER messages are placed in the application message queue and handled by the CWnd object.

Return Value

The timer identifier of the new timer if the function is successful. An application passes this value to the CWnd::KillTimer method to kill the timer. Nonzero if it is successful; otherwise, it is zero.

Remarks

The lpfnTimer callback function need not be named TimerProc, but it must be defined as follows:

void CALLBACK EXPORT TimerProc( HWND hWnd, // handle of CWnd that called SetTimer UINT nMsg, // WM_TIMER UINT nIDEvent // timer identification DWORD dwTime // system time);

Timers are a limited global resource; therefore it is important that an application check the value returned by the SetTimer method to verify that a timer is actually available.

Example

This example uses CWnd::SetTimer, CWnd::OnTimer, and CWnd::KillTimer to handle WM_TIMER messages. A timer is set up to send a WM_TIMER message to the main frame window every 2 seconds in OnStartTimer. OnStopTimer will stop the timer by calling CWnd::KillTimer. OnTimer was set up through ClassWizard to handle WM_TIMER messages for the main frame window. In this example, the PC speaker will beep every 2 seconds.

void CMainFrame::OnStartTimer()

 {  m_nTimer = SetTimer(1, 2000, 0);}

 

void CMainFrame::OnStopTimer()

{  KillTimer(m_nTimer);  }

 

void CMainFrame::OnTimer(UINT nIDEvent)

 {  MessageBeep(0xFFFFFFFF);  // Beep  // Call base class handler.  CMDIFrameWnd::OnTimer(nIDEvent);}

 

Requirements

  Windows CE versions: 1.0 and later

  Header file: Declared in Afxwin.h

 

 

什么时候我们需要用到SetTimer函数呢?当你需要每个一段时间执行一件事的的时候就需要使用SetTimer函数了。 使用定时器的方法比较简单,通常告诉WINDOWS一个时间间隔,然后WINDOWS以此时间间隔周期性触发程序。通常有两种方法来实现:发送WM_TIMER消息和调用应用程序定义的回调函数。 

1.1 用WM_TIMER来设置定时器

先请看SetTimer这个API函数的原型 

UINT_PTR SetTimer(
  HWND hWnd,              // 窗口句柄
  UINT_PTR nIDEvent,      // 定时器ID,多个定时器时,可以通过该ID判断是哪个定时器
  UINT uElapse,           // 时间间隔,单位为毫秒
  TIMERPROC lpTimerFunc   // 回调函数
);

例如 
SetTimer(m_hWnd,1,1000,NULL); //一个1秒触发一次的定时器
在MFC程序中SetTimer被封装在CWnd类中,调用就不用指定窗口句柄了

于是SetTimer函数的原型变为: 

UINT SetTimer(UINT nIDEvent,UINT nElapse,void(CALLBACK EXPORT *lpfnTimer)(HWND,UINT ,YINT ,DWORD)) 

当使用SetTimer函数的时候,就会生成一个计时器。函数中nIDEvent指的是计时器的标识,也就是名字。nElapse指的是时间间隔,也就是每隔多长时间触发一次事件。第三个参数是一个回调函数,在这个函数里,放入你想要做的事情的代码,你可以将它设定为NULL,也就是使用系统默认的回调函数,系统默认认的是onTime函数。这个函数怎么生成的呢?你需要在需要计时器的类的生成onTime函数:在ClassWizard里,选择需要计时器的类,添加WM_TIME消息映射,就自动生成onTime函数了。然后在函数里添加代码,让代码实现功能。每隔一段时间就会自动执行一次。 

例: 

SetTimer(1,1000,NULL); 

1:计时器的名称; 

1000:时间间隔,单位是毫秒; 

NULL:使用onTime函数。 

当不需要计时器的时候调用KillTimer(nIDEvent); 

例如:KillTimer(1); 

1.2 调用回调函数

此方法首先写一个如下格式的回调函数

void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime);
然后再用SetTimer(1,100,TimerProc)函数来建一个定时器,第三个参数就是回调函数地址。

二. 或许你会问,如果我要加入两个或者两个以上的 timer怎么办? 

继续用SetTimer函数吧,上次的timer的ID是1,这次可以是2,3,4。。。。 

SetTimer(2,1000,NULL); 

SetTimer(3,500,NULL); 

嗯,WINDOWS会协调他们的。当然onTimer函数体也要发生变化,要在函数体内添加每一个timer的处理代码: 

onTimer(nIDEvent) 


switch(nIDEvent) 


case 1:........; 

break; 

case 2:.......; 

break; 

case 3:......; 

break; 


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值