Saturday, November 5, 2011

WM_TIMER and OnTimer( ) in Graphics Under Windows

If we are to perform an activity at regular intervals we have two choices:

(a) Use a loop and monitor within the loop when is it time to perform that activity.
(b)Use a Windows mechanism of timer. This mechanism when used posts a WM_TIMER message at regular intervals to our application.
The first method would seriously hamper the responsiveness of the program. If the control is within the loop and a new message arrives the message would not get processed unless the control goes out of the loop. The second choice is better because it makes the program event driven. That is, whenever WM_TIMER arrives that time its handler does the job that we want to get executed periodically. At other times the application is free to handle other messages that come to its queue.
All that we have done in the OnTimer( ) handler is erase the ball from previous position and draw it at a new position. We have also checked if the ball has hit the boundaries of the window. If so we have played a sound file using the PlaySound( ) API function and then changed the direction of the ball.

No comments:

Post a Comment