VC timer usage: settimer and ontimer

Source: Internet
Author: User

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;

}

}

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.