Settimer is the code in ontimer that sets a timer and starts to execute the timer. ontimer is the code executed by the timer. Killtimer is used to stop the timer. Settimer is the setting, and ontimer is the response to the settimer message.
Settimer function usage
1) Use wm_timer to set the timer
First, see the prototype of the settimer API function.
Uint_ptr settimer (
Hwnd, // The Window associated with the timer
Uint_ptr nidevent, // a non-0 number indicates this timer
Uint uelapse, // specifies the time interval, in milliseconds
Timerproc lptimerfunc // It is generally null
);
When the MFC project is as follows:
Uint_ptr settimer (
Uint_ptr nidevent, // a non-0 number indicates this timer
Uint uelapse, // specifies the time interval, in milliseconds
Timerproc lptimerfunc // It is generally null
);
The usage is as follows:
Settimer (1,1000, null );
1: The timer name;
1000: time interval, in milliseconds;
Null: Use the ontime function.
The wm_timer message is triggered every 1 s. The message response function is as follows:
Void CXXX: ontimer (uint_ptr nidevent)
{
// Todo: add the message processing program code and/or call the default value here
// Add the following code here:
Switch ()
{
Case 1:
// Do what you should do
// Call killtimer (1) here when you do not need it );
Break;
Default:
Break;
}
Cdialog: ontimer (nidevent );
}
Call killtimer (nidevent) When no timer is required );
Example: killtimer (1 );
What if I want to add two or more timers?
Continue to use the settimer function. The last timer ID is 1. This time it can be 2, 3, 4 ....
Settimer (2,1000, null );
Settimer (3,500, null );
Well, Windows will coordinate them. Of course, the ontimer function body also needs to change. You need to add the processing code of each timer in the function body:
Ontimer (nidevent)
{
Switch (nidevent)
{
Case 1 :........;
Break;
Case 2 :.......;
Break;
Case 3 :......;
Break;
}
}