高精度多媒体时钟应用类

时间:2021-07-24 20:33:22
 
///////////////////////////////////////////////High-Resolution Timer//多媒体高精度时钟应用类/////////////////////////////////////////////#include <MMSYSTEM.H> #pragma comment(lib, "WINMM.LIB")class CHighResolutionTimer{	typedef void (*TIMERCALLBACK)(DWORD);private:	DWORD m_dwUser;	MMRESULT m_nIDTimer;	UINT m_uDelay;	TIMERCALLBACK m_pfnCallback;	void KillTimer()	{		if(m_nIDTimer != 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 object				CHighResolutionTimer * 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)	{		ASSERT(dwUser);		ASSERT(lpTimeProc);				m_dwUser = dwUser;		m_pfnCallback = lpTimeProc;				BOOL ret = FALSE;		KillTimer();		m_nIDTimer = timeSetEvent (uDelay, 0, TimeProc, (DWORD)this, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);		if(m_nIDTimer != NULL)			ret = TRUE;		return(ret);	}};



//应用 .h

CHighResolutionTimer mm_Timer;
static void TimerCallback (DWORD dwUser);

//应用 .cpp
定时器回调
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);
}



//启动2毫秒定时器
void CMy_TimerDlg::OnButton1()
{
// TODO: Add your control notification handler code here
//
mm_Timer.CreateTimer((DWORD)this,2,TimerCallback);
}