W
Für den Multimedia-Timer hab ich mal ne Klasse gemacht. Aber ich weiß auch nicht genau, welche Hardware-Voraussetzungen da gegeben sind.
Der Header:
//---------------------------------------------------------------------------
#ifndef CMMTimerH
#define CMMTimerH
//---------------------------------------------------------------------------
#include <windows.h>
#include <mmsystem.h>
//---------------------------------------------------------------------------
class CMMTimer;
VOID CALLBACK TimerProc(UINT, UINT, DWORD, DWORD, DWORD);
typedef void (__stdcall* TimerNotification)(CMMTimer* mmTimer);
class CMMTimer
{
private:
int FInterval;
bool FEnabled;
UINT FId;
UINT FMinResolution;
UINT FMaxResolution;
protected:
void Enable();
void Disable();
void GetResolution();
void ErrorMessage(LPCTSTR);
public:
TimerNotification OnTimer;
CMMTimer();
~CMMTimer();
void SetEnabled(bool);
bool IsEnabled();
void SetInterval(int);
int GetInterval();
};
//---------------------------------------------------------------------------
#endif
Die cpp:
#include "CMMTimer.h"
//---------------------------------------------------------------------------
CMMTimer::CMMTimer()
{
FEnabled = false;
FInterval = 1000;
FId = 0;
}
//---------------------------------------------------------------------------
CMMTimer::~CMMTimer()
{
if(FEnabled) Disable();
}
//---------------------------------------------------------------------------
bool CMMTimer::IsEnabled()
{
return FEnabled;
}
//---------------------------------------------------------------------------
void CMMTimer::SetEnabled(bool enabled)
{
if(enabled != FEnabled)
{
FEnabled = enabled;
if(enabled)
Enable();
else
Disable();
}
}
//---------------------------------------------------------------------------
void CMMTimer::SetInterval(int interval)
{
FInterval = interval;
}
//---------------------------------------------------------------------------
int CMMTimer::GetInterval()
{
return FInterval;
}
//---------------------------------------------------------------------------
void CMMTimer::GetResolution()
{
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
FMinResolution = tc.wPeriodMin;
FMaxResolution = tc.wPeriodMax;
}
//---------------------------------------------------------------------------
void CMMTimer::Enable()
{
GetResolution();
if(FInterval < (int)FMinResolution || FInterval > (int)FMaxResolution)
{
if(FInterval < (int)FMinResolution)
ErrorMessage("Interval is too small");
if(FInterval > (int)FMaxResolution)
ErrorMessage("Interval is too high");
}
else
{
timeBeginPeriod(FMinResolution);
FId = timeSetEvent(FInterval, FInterval, TimerProc, (DWORD)this, TIME_PERIODIC);
}
}
//---------------------------------------------------------------------------
void CMMTimer::Disable()
{
timeKillEvent(FId);
timeEndPeriod(FMinResolution);
}
//---------------------------------------------------------------------------
void CMMTimer::ErrorMessage(LPCTSTR msg)
{
::MessageBox(NULL, msg, "ERROR", MB_OK | MB_ICONERROR);
}
//---------------------------------------------------------------------------
VOID CALLBACK TimerProc(UINT, UINT, DWORD dwUser, DWORD, DWORD)
{
CMMTimer* mm_timer = (CMMTimer*)dwUser;
if(mm_timer)
if(mm_timer->OnTimer)
mm_timer->OnTimer(mm_timer); // *g*
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------