8 Years
Discussion Span
Last Post by lukename

Can you give code example? Countdown from 10 to 0 please. Thank you


First you have to write an OnTimer() event handler. That is a function that you want MFC to call every second. This example has it in CMainFrame, but you can put it in any of the CWnd derived classes, such as CMyDialog. You can have up to 10 timer events active at the same time, so nIDEvent parameter just tells which event is being called. You set that event number when you start the timer, explained below.

void CMainFrame::OnTimer(UINT nIDEvent) 
   MessageBeep(0xFFFFFFFF);   // Beep

Next, you have to turn on the timer somewhere in the code.
m_nTimer = SetTimer(1, 2000, 0);

  • The first parameter is an id number that you want MFC to use to identify the timer event. The OnTimer() function will be passed that number (see above)
  • The second number is the number of milliseconds you want the timer event handler to be called. The example shows it wants the event to be fired every two seconds.
  • The last parameter is a pointer to the function you want to be called. If that parameter is 0 (or NULL) it is assumed to be the OnTimer() method of the MFC class in which SetTimer() is called. Otherwise, if the prameter is Not called then it can be any function you want. See the SetTimer link for a more detailed explaination.
  • The return value is the MFC id of the timer event, if SetTimer() is successful. Use this value to pass to KillTimer() to stop the events.

Edited by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.