65.9K
CodeProject is changing. Read more.
Home

Wrapper Class for the Multimedia Timer Functions

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.81/5 (19 votes)

Nov 4, 2002

2 min read

viewsIcon

104252

downloadIcon

4578

A class that makes using the Multimedia Timer functions easy and painless.

Sample Image - mult_media_timer.jpg

Introduction

In many of the programs we write, we need to know when a certain amount of time has passed before doing something; we need timing information. In a Windows program, that often means using the WM_TIMER message. Sometimes, however, we need something a little more accurate, something a little more finely grained.

One option is to use the multimedia timer. This timer can give us greater accuracy and resolution than WM_TIMER messages. The CMMTimer class encapsulates the Windows multimedia functions to make using the multimedia timer easy and painless.

The CMMTimer and CMMTimerListener classes

The CMMTimer provides a set of methods for using the multimedia timer. Listed below are each method and a brief description of how to use it.

// Starts the timer. Delay is the interval in milliseconds between each 
// timing event. Resolution is the timing resolution of this timer. A value
// of zero gives the greatest timing resolution. Returns true if the 
// operation was successful.
bool Start(UINT Delay, UINT Resolution);

// Stops this timer. Timer can be restarted by calling Start.
void Stop();

// Resets this timer back to zero.
void Reset();

// Returns true if this timer is running.
bool IsRunning() const { return m_RunningFlag; }

// Functions for attaching, detaching, and notifying CMMTimerListeners
void AttachListener(CMMTimerListener &Listener);
void DetachListener(CMMTimerListener &Listener);
void NotifyListeners();

// Gets the total timing values since this timer was started.
DWORD GetTotalMilliseconds() const; 
DWORD GetTotalSeconds() const;
DWORD GetTotalMinutes() const;
DWORD GetTotalHours() const;

//
// The following accessor functions return relative timing values. For 
// example, the GetCurrentMilliseconds function returns how many 
// milliseconds have passed since the last second has occurred. Let's say  
// that 3.5 seconds have passed since the timer was started. 
// GetCurrentMilliseconds would return 500 because that is how many 
// milliseconds have passed since the last second began. 
//

DWORD GetCurrentMilliseconds() const;
DWORD GetCurrentSeconds() const;
DWORD GetCurrentMinutes() const;
DWORD GetCurrentHours() const;

// Gets the number of times a timing event has occurred.
DWORD GetCount() const;

// Gets the device capabilities of this timer. Results are stored in the
// TIMECAPS structure.
static void GetDevCaps(LPTIMECAPS TimeCap);

In order to receive timing events from a CMMTimer object, you will need to implement the CMMTimerListener class and attach your class to the CMMTimer object with the AttachListener method. For every timing event, the CMMTimer will notify all of its listeners that a timing event has occurred by calling the Update method in the CMMTimerListener objects.

Also, it's important to note that you must link to the winmm.lib before you can use the CMMTimer class.

The CMMTimer Demo App

I've included a toy application to demonstrate using the CMMTimer and CMMTimerListener classes. This program has a simple animation sequence. The speed of the animation is determined by a CMMTimer object. To start the animation, simply press the Start button, and to stop the animation press Stop. The speed can be adjusted by choosing from the settings listed in the Speed combo box.

Conclusion

Well, that's about it. I hope you find this class helpful when you need timing notification that's a little more accurate than what WM_TIMER messages can give you. Please let me know if any improvements can be made to the design or if there are additional features that would make the class more useful.

Wrapper Class for the Multimedia Timer Functions - CodeProject