1. 程式人生 > >VC++ 下多媒體高精度定時器timeSetEvent

VC++ 下多媒體高精度定時器timeSetEvent

設定timer事件物件; uDelay  是定時器間隔; uResolution 是解析度,即定時器的精度,為0表示儘可能最大; lpTimeProc是定時器回撥; dwUser 是回撥引數; fuEvent  是定時器的屬性:       TIME_ONESHOT 表示回撥執行一次 TIME_PERIODIC表示回撥週期性執行; 該函式返回值為定時器物件id; 2》MMRESULT timeKillEvent( UINT uTimerID ); 清除定時器物件; 例子: // timeSetEvent.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" #include "mmsystem.h" #include "iostream" #pragma comment(lib,"winmm.lib") using namespace std; MMRESULT TimerId; volatile int i=0; void CALLBACK TimeProc(   UINT uID,         UINT uMsg,        DWORD dwUser,     DWORD dw1,        DWORD dw2         ) { if (uID==TimerId) { cout<<++i<<endl; if (i==10) { timeKillEvent(TimerId); } } } int main(int argc, char* argv[]) { //printf("Hello World!\n"); TimerId=timeSetEvent(1000,0,TimeProc,NULL,TIME_PERIODIC); if (TimerId==NULL) { cout<<"timeSetEvent error" <<endl; } for (int j=0;j<5000;j++) { Sleep(200); } return 0; }