mfc下定时器使用

mfc 使用定时器的话,一般用的是SetTimer函数,Cwnd::SetTimer.函数的作用创建一个系统时钟。

函数原型:UINT_PTR SetTimer(UINT_PTR nIDEvent, UINT nElapse,void (CALLBACK* lpfnTimer)(HWND, UINT, UINT_PTR, DWORD));

nIDEvent :非零定时器标志 如果这个定时器是唯一的,那么函数返回值就是定时器的标志。否则,定时器返回一个新的标志,返回值表示该新标志。[如果是windows定时器(callback函数设置成NULL),和当前窗口定时器有关的其他窗口定时器的定时器标志值要唯一。对于有callback函数的定时器,这个定时器标志要与所有进程中的所有定时标志都不一样。] 这句话比较不好理解

nElapse:这个比较好理解,定时器溢出时间设置。精确到1毫秒。

lpfnTimer:WM_Timer消息的回调函数。

C++ 例子

void CMainFrame::OnStartTimer() 
{
	// This timer uses a WM_TIMER message, not a callback. 
	// Therefore, the timer is specific to this window. 
	// m_nWindowTimer is a UINT_PTR field.
    m_nWindowTimer = SetTimer(1, 2000, NULL);
	
	// For this demo, we specify an interval that won't overlap 
	// with the window timer.
	m_nCallbackTimer = SetTimer(2, 3750, &CMainFrame::MyTimerProc);
	
	// See whether we got the ID we requested in the first parameter.
#ifdef _DEBUG
	CString str;
	str.Format(_T("m_ncallbackTImer ID = %d"), m_nCallbackTimer);
	TRACE(str);
#endif

}

 void CALLBACK CMainFrame::MyTimerProc(
   HWND hWnd,      // handle of CWnd that called SetTimer
   UINT nMsg,      // WM_TIMER
   UINT_PTR nIDEvent,   // timer identification
   DWORD dwTime    // system time
)
{
	 MessageBeep(0x00000030L);   // Windows question sound.
}

void CMainFrame::OnStopTimer() 
{
   KillTimer(m_nWindowTimer);   
   KillTimer(m_nCallbackTimer);   
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
   MessageBeep(0xFFFFFFFF);   // Beep 


   // Call base class handler.
   CMDIFrameWnd::OnTimer(nIDEvent);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值