-
大小: 14KB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-06-09
- 語言: 其他
- 標(biāo)簽:
資源簡(jiǎn)介
多媒體定時(shí)器的簡(jiǎn)單使用示例,更精確的定時(shí)器使用mmtimer

代碼片段和文件信息
#include?“stdafx.h“
#ifndef?__REALTIME
#ifndef _MMTIMER_
#include?“MMTimer.h“
#endif
//Allow?non?MFC?asserting
#ifdef?_AFX
#define?MM_ASSERT ASSERT
#else
#include?
#define?MM_ASSERT _ASSERT
#endif
////////////////////////////////////////////////////////////////
//CMultimediaTimer?support
////////////////////////////////////////////////////////////////
void?CALLBACK?mmTimerProc(UINT?idUINT?msgDWORD?dwUserDWORD?dw1DWORD?dw2)
{
CMultimediaTimer* pTimer?=?(CMultimediaTimer*)dwUser;
bool?bValidTimer=pTimer!=NULL?&&?pTimer->CheckMarker();
MM_ASSERT(bValidTimer);
//Don‘t?call?callback?unless?a?valid?CMultimediaTimer?object?
if(bValidTimer)
pTimer->DoLoop();
}
////////////////////////////////////////////////////////////////
//?CMultimediaTimer
////////////////////////////////////////////////////////////////
CMultimediaTimer::CMultimediaTimer(CMultimediaTimerCallback&?callback)?
:m_Callback(callback)m_Marker(cMMTimerMarker)
m_TimerResolution(0)m_TimerPeriod(0)m_TimerId(0)m_EndTimer(false)
{
}
///////////////////////////////////////////////////////////////////////////////////
//
CMultimediaTimer::~CMultimediaTimer()
{
Stop();
m_Marker=0;//invalidate?marker
}
///////////////////////////////////////////////////////////////////////////////////
//
void?CMultimediaTimer::DoLoop()
{
if(m_EndTimer)//Previous?loop?marked?as?ending
{
Stop();//Try?to?stop
}
//?Check?Active()?is?true?-?before?calling?Loop()
//?This?may?be?false?if?the?overridden?onstarted()?takes?longer?than?1?period
//?and?onstarted()?must?complete?before?first?loop?call.
//
if(!m_EndTimer?&&?Active())
{
m_EndTimer=!m_Callback.OnLoop();
}
}
///////////////////////////////////////////////////////////////////////////////////
//
bool CMultimediaTimer::CheckMarker()const
{
return?m_Marker==cMMTimerMarker;
}
////////////////////////////////////////////////////////////////
//
bool?CMultimediaTimer::Start(UINT?nPeriodMsMMRESULT&?nErrorUINT?nResolutionMs)
{
nError=TIMERR_NOERROR;
//1.?Clean?up?any?current?timer
Stop();
//2?Get?available?timer?resolution
TIMECAPS tc;
MMRESULT?result=::timeGetDevCaps(&tcsizeof(TIMECAPS));
if(result!=TIMERR_NOERROR)
{
nError=result;//Should?be?TIMERR_STRUCT
return?false;
}
MM_ASSERT(nPeriodMs>=tc.wPeriodMin?&&?nPeriodMs<=tc.wPeriodMax);
MM_ASSERT(nResolutionMs>=tc.wPeriodMin?&&?nResolutionMs<=tc.wPeriodMax);
//Clip?required?resolution?to?available?resolution
m_TimerResolution=min(max(tc.wPeriodMinnResolutionMs)tc.wPeriodMax);
//Set?global?min?resolution?value
result=::timeBeginPeriod(m_TimerResolution);
if(result!=TIMERR_NOERROR)
{
m_TimerResolution=0;
nError=result;//Should?be?TIMERR_NOCANDO
return?false;
}
//Clip?required?period?to?available?resolution
m_TimerPeriod=min(max(tc.wPeriodMinnPeriodMs)tc.wPeriodMax);
//Set?event?callback?passing?this?as?user?dat
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2002-03-28?13:41??MMTimerTest\
?????文件????????1973??2002-03-28?11:06??MMTimerTest\FLCriticalSection.h
?????文件????????4382??2002-03-28?11:15??MMTimerTest\MMTimer.cpp
?????文件????????3678??2002-03-28?13:40??MMTimerTest\MMTimer.h
?????文件????????1303??2002-03-28?13:40??MMTimerTest\MMTimerTest.clw
?????文件????????1325??2002-03-28?10:52??MMTimerTest\MMTimerTest.cpp
?????文件????????4360??2002-03-28?11:08??MMTimerTest\MMTimerTest.dsp
?????文件?????????545??2002-03-28?10:52??MMTimerTest\MMTimerTest.dsw
?????文件????????1237??2002-03-28?10:52??MMTimerTest\MMTimerTest.h
?????文件????????5539??2002-03-28?11:01??MMTimerTest\MMTimerTest.rc
?????文件????????3991??2002-03-28?11:31??MMTimerTest\MMTimerTestDlg.cpp
?????文件????????3594??2002-03-28?12:46??MMTimerTest\MMTimerTestDlg.h
?????目錄???????????0??2002-03-28?10:52??MMTimerTest\res\
?????文件????????1078??2002-03-28?10:52??MMTimerTest\res\MMTimerTest.ico
?????文件?????????403??2002-03-28?10:52??MMTimerTest\res\MMTimerTest.rc2
?????文件????????1057??2002-03-28?11:01??MMTimerTest\resource.h
?????文件?????????213??2002-03-28?10:52??MMTimerTest\StdAfx.cpp
?????文件?????????999??2002-03-28?10:52??MMTimerTest\StdAfx.h
評(píng)論
共有 條評(píng)論