//////////////////////////////////////// ////// High-resolution timer // multimedia high-precision clock application class /////////////////////// /// // # include <mmsystem. h> # pragma comment (Lib, "winmm. lib ") Class invocation {typedef void (* timercallback) (DWORD); Private: DWORD m_dwuser; mmresult m_nidtimer; uint m_udelay; timercallback m_pfncallback; void killtimer () {If (callback! = NULL) {timekillevent (m_nidtimer); m_nidtimer = NULL;} public: chighresolutiontimer () {m_nidtimer = NULL; m_udelay = 0; m_pfncallback = 0;} virtual ~ Chighresolutiontimer () {killtimer ();} static void callback timeproc (uint uid, uint umsg, DWORD dwuser, DWORD dw1, DWORD dw2) {// dwuser contains PTR to timer objectchighresolutiontimer * ptimer = (chighresolutiontimer *) dwuser; // call user-specified callback and pass back user specified data (ptimer-> m_pfncallback) (ptimer-> m_dwuser);} bool createtimer (DWORD dwuser, uint udelay, timercallback lptimeproc) {SERT (dwuser); Assert (lptimeproc); m_dwuser = dwuser; m_pfncallback = lptimeproc; bool ret = false; killtimer (); delimiter = timesetevent (udelay, 0, timeproc, (DWORD) this, time_periodic | time_callback_function); If (m_nidtimer! = NULL) ret = true; Return (RET );}};
// Application. h chighresolutiontimer mm_timer; static void timercallback (DWORD dwuser); // application. CPP timer callback void cmy_timerdlg: timercallback (DWORD dwuser) {cmy_timerdlg * pthis = (cmy_timerdlg *) dwuser; // static Long Count = 0; cstring STR; Str. format ("% d", Count ++); pthis-> getdlgitem (idc_button1)-> setwindowtext (STR); BEEP (1000,1 );} // start the 2-millisecond timer void cmy_timerdlg: onbutton1 () {// todo: add your control notification handler code here // mm_timer.createtimer (DWORD) This, 2, timercallback );}