65,210
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <iostream>
//after call MyTimeProce MAX_TIMER_CALLED times, kill the timer and quit the application
const int MAX_TIMER_CALLED = 100;
//timer procedure
void CALLBACK MyTimerProc( HWND hwnd, UINT uMsg,UINT_PTR idEvent,DWORD dwTime );
//console application entry point
int main()
{
// Set the timer.
UINT_PTR IDT_TIMER = 1; // timer identifier
SetTimer( NULL, // handle to main window
IDT_TIMER, // timer identifier
100, // 1-second interval
(TIMERPROC) MyTimerProc); // timer callback
// we need a message loop to deal with the timer event
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// implementation of MyTimerProc
void CALLBACK MyTimerProc( HWND hwnd, UINT uMsg,UINT_PTR idEvent,DWORD dwTime )
{
static int nCalled = 0;
++nCalled;
std::cout << "MyTimerProc called " << nCalled << " times" << std::endl;
if ( nCalled >= MAX_TIMER_CALLED ) {
KillTimer(hwnd, idEvent); // kill the timer
PostMessage(NULL, WM_QUIT, 0, 0); // quit the application
}
}