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); }