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.
bool Start(UINT Delay, UINT Resolution);
void Stop();
void Reset();
bool IsRunning() const { return m_RunningFlag; }
void AttachListener(CMMTimerListener &Listener);
void DetachListener(CMMTimerListener &Listener);
void NotifyListeners();
DWORD GetTotalMilliseconds() const;
DWORD GetTotalSeconds() const;
DWORD GetTotalMinutes() const;
DWORD GetTotalHours() const;
DWORD GetCurrentMilliseconds() const;
DWORD GetCurrentSeconds() const;
DWORD GetCurrentMinutes() const;
DWORD GetCurrentHours() const;
DWORD GetCount() const;
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.
Aside from dabbling in BASIC on his old Atari 1040ST years ago, Leslie's programming experience didn't really begin until he discovered the Internet in the late 90s. There he found a treasure trove of information about two of his favorite interests: MIDI and sound synthesis.
After spending a good deal of time calculating formulas he found on the Internet for creating new sounds by hand, he decided that an easier way would be to program the computer to do the work for him. This led him to learn C. He discovered that beyond using programming as a tool for synthesizing sound, he loved programming in and of itself.
Eventually he taught himself C++ and C#, and along the way he immersed himself in the ideas of object oriented programming. Like many of us, he gotten bitten by the design patterns bug and a copy of GOF is never far from his hands.
Now his primary interest is in creating a complete MIDI toolkit using the C# language. He hopes to create something that will become an indispensable tool for those wanting to write MIDI applications for the .NET framework.
Besides programming, his other interests are photography and playing his Les Paul guitars.