Hello. How can i make count-down timer on MFC program?
Thank you

Create a timer event -- see MSDN for SetTimer() and OnTimer() methods.

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

I tried that. But i couldn't understand :(

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.

I tried that but timer didn't started or didn't called OnTimer... Any advice?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.