How to Use settimer MFC is detailed

Source: Internet
Author: User

From: http://blog.csdn.net/ellor/article/details/1714741

Timer events, that is, Timer events, are often used in game programming. With this function, you can perform scheduled actions. This article will discuss with you how to use the settimer () function.

1. Where is the settimer definition?

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. One is a global function: settimer ()

Uint settimer (
Hwnd
Hwnd,// Handle of window for timer messages
UintNidevent,// Timer identifier
UintUelapse,// Time-out value
TimerprocLptimerfunc// Address of timer procedure
);

 

Hwnd is the pointer to cwnd, that is, the window class for processing timer events. Speaking of the window class (cwnd), we need to look at the inheritance of cwnd: cwnd has the following sub-classes: cframewnd, cdialog, cview, ccontrolbar, and so on. This also means that settimer events can be defined in these classes.

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, UintNelapse, 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-suppliedTimerProcCallback function that processesWm_timerMessages. If this parameter is
Null,Wm_timerMessages are placed in the application's message queue and handled byCwndObject.

It means to specify the address of the timerproc callback function provided by the application to handle this timer event. If it is null, define the cwnd object of the timer for processing the timer event. It passes the wm_timer message to this object and processes this timer event by implementing the ontimer () event of this object.

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.

Similarly, 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
Hwnd,// Handle of window that installed Timer
UintUidevent// 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.

 

Ii. Use of timer events:

From the above analysis, we should be clear about how to use timer events. Suppose we draw a gradient animation on The View. First, add a menu item on the menu bar and add a command response to the menu:

Pview-> settimer (, null); // pview is a pointer to the View class, where a timer is set in the View class.

Add a wm_timer event to the View class. Write the Chinese books in the ontimer () function and perform the corresponding operations.

In this way, animation can be made.

 

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.