Monday 28 November 2011

How do you create a delay timer in a Windows program? in C programming

How do you create a delay timer in a Windows program?

You can create a delay timer in a Windows program by using the Windows API function SetTimer(). The SetTimer() function sets up a timer event in Windows to be triggered periodically at an interval that you specify. To create a timer, put the following code in your WinMain() function:

SetTimer(hwnd, 1, 1000, NULL);

This code sets up a timer in Windows. Now, every 1000 clock ticks (1 second), your program receives a WM_TIMER message. This message can be trapped in your WndProc (message loop) function as shown here:
switch (message)
{
case WM_TIMER :
/* this code is called in one-second intervals */
return 0 ;
}

You can put whatever you like in the WM_TIMER section of your program. For instance, FAQ XXI.23 shows how you might display the date and time in a window’s title bar every second to give your users a constant update on the current day and time. Or perhaps you would like your program to periodically remind you to save your work to a file. Whatever the case, a delay timer under Windows can be very handy.

To remove the timer, call the KillTimer() function. When you call this function, pass it the handle to the window the timer is attached to and your own timer identifier. In the preceding SetTimer() example, the number 1 is used as the timer identifier. To stop this particular timer, you would issue the following function call:

KillTimer(hwnd, 1);

Cross Reference:

None.

No comments:

Post a Comment