VC timer settimer Function

Source: Internet
Author: User
1. settimer indicates defining a timer. Implement the ontimer event in the specified window (cwnd) according to the defined window, so that the corresponding event can be implemented.

Settimer has two functions.

① A global function: settimer ()

Uint settimer (
Hwnd, // handle of window for timer messages
Uint nidevent, // timer identifier
Uint uelapse, // time-out value
Timerproc lptimerfunc // address of timer procedure
);

Hwnd is the pointer to cwnd, that is, the window class for processing timer events. Therefore, you can define settimer events for subclass that inherits cwnd.

② Settimer () is also defined in cwnd, that is, settimer () is a member function of cwnd. The subclass of cwnd can call this function to set the trigger.

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

Parameter description:

Nidevent: sets the ID of the timer, that is, the identity sign. In this way, different event responses can be made based on different timers in the ontimer () event. This ID is an unsigned integer.

Nelapse

It refers to the time delay. The Unit is milliseconds. This means that ontimer () is called every nelapse millisecond ().

Void (callback Export * lpfntimer) (hwnd, uint, uint, DWORD)

Specifies the address of the application-supplied timerproc callback function that processes the wm_timer messages. if this parameter is null, The wm_timer messages are placed in the application's message queue and handled by the cwnd object.

Specifies the applicationProgramThe address of the provided timerproc callback function to handle this timer event. If it is null, the system will handle this timer event by ontimer.

Therefore, in general, we set this value to null, and there is an ontimer () function in the object that sets this timer to process this event.

Example: settimer (, null );

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, and 4.

Settimer (2,1000, null );

Settimer (3,500, null );

Windows will coordinate them. Of course, the ontimer function body also needs to change. You must add the processing of each timer in the function body.Code:

Ontimer (nidevent)
{
Switch (nidevent)
{
Case 1 :........;
Break;
Case 2 :.......;
Break;
Case 3 :......;
Break;
}
}

2. Let's look at the definitions of killtimer () and ontimer:

Like settimer (), killtimer has two global functions: killtimer () and cwnd. His statement is as follows:

// Global functions

Bool killtimer (
Hwnd, // handle of window that installed Timer
Uint uidevent // timer identifier
);

// Cwnd Function

Bool killtimer (INT nidevent );

These two functions mean to remove the timer with ID nidevent. So that it no longer works. Its usage is the same as settimer.

Let's look at ontimer ()

Cwnd: ontimer
Afx_msg void ontimer (uint nidevent );

Ontimer () is the wm_timer message generated in response to the cwnd object. Nidevent indicates the ID of the timer event to be responded.

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.