在控制臺中使用定時(shí)器不能簡單的SetTimer了事,這在控制臺里這種SetTimer的方式是有點(diǎn)麻煩的,需要自己寫消息循環(huán)投遞WM_TIMER消息。其實(shí)在控制臺里可以使用多媒體時(shí)鐘來計(jì)時(shí):
example:
//啟動(dòng)計(jì)時(shí)器 MMRESULT nIDTimerEvent = timeSetEvent( 1000,//延時(shí)1秒 0, TimeProc, 0, (UINT)TIME_PERIODIC); if( nIDTimerEvent == 0 ) cout<<"啟動(dòng)計(jì)時(shí)器失敗"<<endl;
//回調(diào)過程(時(shí)鐘到來,回調(diào)函數(shù)被系統(tǒng)自動(dòng)調(diào)用) void CALLBACK TimeProc( UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2 ) { cout<<"時(shí)鐘到來"<<endl; } 當(dāng)然了,你要是習(xí)慣于SetTimer,那就用SetTimer吧: 下面是我在Console下用SetTimer寫的一個(gè)例子: #include <windows.h> #include <iostream> using namespace std; void CALLBACK TimeProc( HWND hwnd, UINT message, UINT idTimer, DWORD dwTime); int main() { SetTimer(NULL,1,1000,TimeProc); MSG msg; while(GetMessage(&msg,NULL,0,0)) { if(msg.message==WM_TIMER) { DispatchMessage(&msg); } } return 0; } void CALLBACK TimeProc( HWND hwnd, UINT message, UINT idTimer, DWORD dwTime) { cout<<"a timer comming"<<endl; }
|