Posts tagged timer dialog
How to set timer in mfc dialog?
2![]()
Timers are running everywhere! The moment when your machine starts, when your machine is turned off, in your heart, in your lungs, in your brain and even in your mind, its everywhere. Well, Software application are not exception to timers. We have to use timers in several situations.

But how to create timers and use it?
![]()
For creating timers you can use the api – SetTimer(). In SetTimer() you’ve to specify the time interval in milliseconds, a timer id as UINT, then TimerProc. For each tick of specified timer, windows will call the TimerProc. If you didn’t specify any TimerProc, then a WM_TIMER message will be posted to your window.
You can utilize the Timer ID to set multiple timers using the same TimeProc. You can use KillTimer() to remove the timer. Have a look at the code snippet. In the given code snippet, timer is handled by WM_TIMER message.
// Message Map
BEGIN_MESSAGE_MAP(CDlgDlg, CDialog)
...
ON_WM_TIMER()
END_MESSAGE_MAP()
...
// Timer ID constants.
const UINT ID_TIMER_MINUTE = 0x1001;
const UINT ID_TIMER_SECONDS = 0x1000;
// Start the timers.
void CDlgDlg::StartTimer()
{
// Set timer for Minutes.
SetTimer( ID_TIMER_MINUTE, 60 * 1000, 0 );
// Set timer for Seconds.
SetTimer( ID_TIMER_SECONDS, 1000, 0 );
}
// Stop the timers.
void CDlgDlg::StopTimer()
{
// Stop both timers.
KillTimer( ID_TIMER_MINUTE );
KillTimer( ID_TIMER_SECONDS );
}
// Timer Handler.
void CDlgDlg::OnTimer( UINT nIDEvent )
{
// Per minute timer ticked.
if( nIDEvent == ID_TIMER_MINUTE )
{
// Do your minute based tasks here.
}
// Per minute timer ticked.
if( nIDEvent == ID_TIMER_SECONDS )
{
// Do your seconds based tasks here.
}
}
![]()
“Today is the tomorrow you worried about yesterday” – So enjoy your day!
![]()
Targeted Audience – Beginners.