How to Use settimer MFC is specific

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. It can be used to produce the effect of scheduled operation. This article will discuss with you how to use the settimer () function.

1. Where is the settimer definition?

Settimer indicates defining a timer. The ontimer event is implemented in the specified form (cwnd) based on the specified form defined, so that the event can be matched.

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 Form class for processing timer events. Speaking of the Form class (cwnd), it is necessary 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.

At the same time, 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 ));

Description of the number of shards:

Nidevent: sets the ID of the timer, that is, the identity sign. In this way, in the ontimer () event, the timer can be used to respond to different events. 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 isNull,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, The cwnd object for processing the timer event is defined. 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. The method 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 very clear about how to use timer events. Suppose we draw a gradient animation on The View. First, add a menu item to 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.

After adding, add a corresponding wm_timer event to the View class. Write a Chinese book in the ontimer () function to perform the corresponding operation.

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.