#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <iostream>
using namespace std;
void CALLBACK fun(HWND hDlg, UINT uMsg, UINT iTimerID, DWORD dwTime)
{
cout <<2<< endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int c=SetTimer(NULL, NULL,100 ,fun);
cout <<c<< endl;
Sleep(1000);
//SetTimer(NULL,1,100,fun);
return 0;
}
时钟需要消息循环,而控制台程序没有消息循环,要在控制台中使用时钟需要添加消息循环
完整实例
- #include "windows.h"
- #include "stdio.h"
- VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
- int main(int argc, char* argv[])
- {
- UINT_PTR nTimerID = SetTimer(NULL, WM_TIMER, 5000, (TIMERPROC)TimerProc);
- printf("Timer created. ID = %d\n", nTimerID);
- MSG msg;
- while(GetMessage(&msg, 0, 0, 0)) {
- DispatchMessage(&msg);
- }
- return 0;
- }
- VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
- {
- printf("%08x, %d, %d, %u\n", uMsg, idEvent, dwTime, GetTickCount());
- }
复制代码
|