在控制檯中使用定時器不能簡單的SetTimer了事,這在控制檯裡這種SetTimer的方式是有點麻煩的,需要自己寫訊息迴圈投遞WM_TIMER訊息。其實在控制檯裡可以使用多媒體時鐘來計時:
example:
//啟動計時器
MMRESULT nIDTimerEvent = timeSetEvent(
1000,//延時1秒
0,
TimeProc,
0,
(UINT)TIME_PERIODIC);
if( nIDTimerEvent == 0 )
cout<<"啟動計時器失敗"<<endl;
//回撥過程(時鐘到來,回撥函式被系統自動呼叫)
void CALLBACK TimeProc(
UINT uID,
UINT uMsg,
DWORD dwUser,
DWORD dw1,
DWORD dw2
)
{
cout<<"時鐘到來"<<endl;
}
當然了,你要是習慣於SetTimer,那就用SetTimer吧:
下面是我在Console下用SetTimer寫的一個例子:
#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;
}
example:
//啟動計時器
MMRESULT nIDTimerEvent = timeSetEvent(
1000,//延時1秒
0,
TimeProc,
0,
(UINT)TIME_PERIODIC);
if( nIDTimerEvent == 0 )
cout<<"啟動計時器失敗"<<endl;
//回撥過程(時鐘到來,回撥函式被系統自動呼叫)
void CALLBACK TimeProc(
UINT uID,
UINT uMsg,
DWORD dwUser,
DWORD dw1,
DWORD dw2
)
{
cout<<"時鐘到來"<<endl;
}
當然了,你要是習慣於SetTimer,那就用SetTimer吧:
下面是我在Console下用SetTimer寫的一個例子:
#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;
}