The use of the SetTimer function
1) Use Wm_timer to set the timer
First look at SetTimer, the prototype of this API function.
Uint_ptr SetTimer (HWND hwnd,//and timer associated Windows Uint_ptr nidevent,//a non-0 digital flag This timer UINT uelapse,//Specifies the time interval, in milliseconds Timerproc lptimerfunc//is generally specified as null);
When in the MFC project, the following:
Uint_ptr SetTimer (uint_ptr nidevent,//a non-0 digital flag This timer UINT uelapse,//the specified time interval, in milliseconds Timerproc lptimerfunc//is generally specified as null);
Use the following:
SetTimer (1,1000,null);
Parameters:
1: The name of the timer;
1000: time interval, unit is milliseconds;
NULL: Use the OnTime function.
The WM_TIMER message is triggered at each time to 1s, and its message response function is as follows:
Void cxxx::ontimer (uint_ptr nidevent) { // TODO: Add the message handler code here and/or call the default value // Add the following code here: switch () { case 1: //doing what should be done //call KillTimer (1) Here when it is not needed; break; default: break; } cdialog::ontimer (nidevent); }
Call KillTimer (nidevent) when no timer is required;
KillTimer (1);
2) What if I want to join two or more than two timer?
Continue with the SetTimer function bar, the last timer ID is 1, this time can be 2,3,4 ....
SetTimer (2,1000,null); SetTimer (3,500,null);
Well, windows will coordinate with them. Of course, the OnTimer function body is also to be changed, to add each timer's processing code in the function body:
OnTimer (nidevent) {switch (nidevent) {Case 1: ...; Break Case 2: ...; Break Case 3: ...; Break }}
Usage of C + + timers: SetTimer and OnTimer