Basic knowledge
Two timers in MFC
There are two types of timers in MFC:
First, the timer of a custom callback function, such as setting the timer function prototype:
UINT_PTR SetTimer (
HWND hWnd, // window handle
UINT_PTR nIDEvent, // timer ID, when there are multiple timers, you can use this ID to determine which timer
UINT uElapse, // time interval in milliseconds
TIMERPROC lpTimerFunc // callback function
);
When using the SetTimer function, a timer is generated. The nIDEvent in the function refers to the identifier of the timer, which is the name. nElapse refers to the time interval, which is how often the event is triggered. The third parameter is a callback function. In this function, put the code of the thing you want to do. You can set it to NULL, that is, use the system default callback function. The system default is onTime function. How is this function generated? You need to generate the onTime function in the class that needs the timer: In ClassWizard, select the class that needs the timer, add the WM_TIME message map, and the onTime function is automatically generated. Then add code to the function to make the code implement the function. Automatically every once in a while.
The callback function function format is:
void CALLBACK TimerProc (HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime);
Then use SetTimer (1,100, TimerProc) function to build a timer, the third parameter is the callback function address.
example:
Create a dialog project, and then add two buttons in the dialog, the start timing and end timing (the following is the code you added)
void CALLBACK TimerProc (HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
{
TRACE ("Start timing \ n");
}
void CSetTimeDlg :: OnButton1 () // Button start timing
{
// TODO: Add your control notification handler code here
SetTimer (1,1000, TimerProc);
}
void CSetTimeDlg :: OnButton2 () // End timer
{
// TODO: Add your control notification handler code here
MessageBox ("Timer is over");
KillTimer (1);
}
Because TimerProc defines a global function, an error will occur when using MessageBox. I will use TRACE instead. Then you need to debug to see the effect.
Second, do not use callback functions, such as setting the timer function prototype:
UINT_PTR SetTimer (
UINT_PTR nIDEvent, // timer ID, when there are multiple timers, you can use this ID to determine which timer
UINT uElapse, // time interval in milliseconds
TIMERPROC lpTimerFunc // callback function
);
The third parameter can be set to NULL, that is, the system default callback function is used, and the system default is the onTime function. How is this function generated? You need to generate the onTime function in the class that needs the timer: In ClassWizard, select the class that needs the timer, add the WM_TIME message map, and the onTime function is automatically generated. Then add code to the function to make the code implement the function. Automatically every once in a while.
Such as:
SetTimer (1,1000, NULL);
1: the name of the timer;
1000: time interval in milliseconds;
NULL: Use the onTime function.
Call KillTimer (nIDEvent) when a timer is not needed;
For example: KillTimer (1);
example:
Create a dialog project, and then add two buttons in the dialog, the start timing and end timing (the following is the code you added)
void CSetTimeDlg :: OnButton1 () // Button start timing
{
// TODO: Add your control notification handler code here
SetTimer (1,2000, NULL);
}
void CSetTimeDlg :: OnTimer (UINT nIDEvent) // Timer
{
// TODO: Add your message handler code here and / or call default
MessageBox ("Start Timer");
CDialog :: OnTimer (nIDEvent);
}
void CSetTimeDlg :: OnButton2 () // End timer
{
// TODO: Add your control notification handler code here
MessageBox ("Timer is over");
KillTimer (1);
}