Timer usage in MFC

Source: Internet
Author: User

There are three functions related to the timer in MFC:

Uint settimer (uint nidevent, uint nelapse, void (callback Export * lpfntimer) (hwnd, uint, uint, DWORD ));

The first parameter is the code of the timer. InProgramThere may be multiple settimer. In ontime response, you can determine the event caused by the first parameter.

The second parameter is the set number of milliseconds.

The third parameter is a pointer to a callback function. If null is used, the system adds the messages generated by settimer to the message queue. Most of the time we do not implement it here, but in ontimer, that is, we often set this parameter to null.

Afx_msg void ontimer (uint nidevent );

Bool killtimer (INT nidevent ); ========================================================== ======================================


Settimer is used to define the attributes of a timer, such as changing the timer ID, refresh time, and processing functions.

Ontimer is used to respond to the wm_timer message defined by the system. Here, you can use the switch statement to separately respond to each timer in multiple timers. Here we will do what you want to do when the corresponding timer reaches the point, and how powerful you can make it here!

Killtimer is used to end a timer.


Next we will use an example to illustrate the use of the Timer: This example is used to implement a simple function, that is, in a single document program, A message box prompts "timer 1" every five seconds, and a message box prompts "Timer 2" every five seconds ".

The procedure for creating a single document is omitted, all the way to next.

========================================================== ======================================

(1) define the attributes of two timers in the oncreate function of cmainframe.

Settimer (1,5000, 0 );

Settimer (2,5000, 0 );

(2) Respond to wm_timer in cmainframe.

Void cmainframe: ontimer (uint nidevent)


// Todo: add your message handler code here and/or call default

Switch (nidevent ){

Case timeid1:


Afxmessagebox ("timer 1! ");



Case timeid2:


Afxmessagebox ("Timer 2! ");






Cframewnd: ontimer (nidevent );


(3) Add

Killtimer (idtimer1 );

Killtimer (idtimer2 );


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.