Application of OnTimer in MFC

Source: Internet
Author: User

The OnTimer () function is used to implement the timing control function, the timing control function is mainly implemented by the following three functions: SetTimer, KillTimer () and OnTimer (). Roughly speaking, SetTimer is the code that sets a timer and starts executing the timer ontimer, OnTimer is the code that the timer executes. The KillTimer is used to stop the timer. Or SetTimer is set timer, OnTimer is in response to SetTimer message. The OnTimer () function is automatically transferred when the time of SetTimer is set. First understand the next SetTimer this API function prototype UINT_PTR SetTimer (HWND hwnd,//Window handle uint_ptr nidevent,//timer ID, multiple timers, which can be determined by the ID of which timer UINT Uelap SE,//time interval, in milliseconds timerproc lptimerfunc//callback function); For example SetTimer (m_hwnd,1,1000,null); A timer that triggers once in 1 seconds is encapsulated in the SetTimer in the MFC program, and the call does not have to specify a window handle, so the prototype of the SetTimer function becomes: uint SetTimer (UINT nidevent,uint nelapse, void (CALLBACK EXPORT *lpfntimer) (Hwnd,uint, Yint, DWORD)) when the SetTimer function is used, a timer is generated. The nidevent in the SetTimer function refers to the identifier of the timer, which is the name. Nelapse refers to the time interval, which is how often an event is triggered. The third parameter is a callback function, in which you put the code for the thing you want to do, you can set it to null, that is, by using the default callback function of the system, the 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 automatically generate the OnTime function. Then add code to the function and let the code implement the function. Every once in a while (the time set by SetTimer) is automatically executed. Example: SetTimer (1,1000,null); 1: The name of the timer, 1000: The time interval, in milliseconds, and NULL: Use the OnTime function. Call KillTimer (nidevent) when no timer is required;: KillTimer (1);   The ID number "1" Here should match the ID number in SetTimer () if you need to add 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); ID 2, timed 1000ms SetTimer (3,500,null); With an ID of 3, timed 500ms windows will coordinate their.    Of course the OnTimer function body is also to be changed, 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 }} The use of timers in the console can not be simple settimer, which in the console this SetTimer way is a bit cumbersome, need to write their own message loop Wm_timer message delivery. In fact, in the console can use the multimedia clock to time: Example://start timer Mmresult nidtimerevent = timesetevent (1000,//delay 1 seconds 0, Timeproc, 0, (UINT) Time_perio DIC); if (nidtimerevent = = 0) cout<< "Start timer failed" < #include using namespace std;void CALLBACK Timeproc (HWND hwnd, UINT message, uint Idtimer, DWORD dwtime); int main () {SetTimer (NULL,1,1000,TIMEPROC); MSG msg; while (GetMessage (&msg,null,0,0)) {if (Msg.message==wm_timer) {dispatchmessage (&msg);}} return 0;} void CALLBACK Timeproc (HWND hwnd, UINT message, uint Idtimer, DWORD dwtime) {cout<< "a timer comming" < Application of OnTimer in MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.