How to use SetTimer MFC is not specific enough

Source: Internet
Author: User
Tags message queue

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

The timer event, the timer event, is in game programming. An event that is often used. It can produce the effect of timed operation. This article. Let's explore how to use the SetTimer () function.

1. Where is the definition of SetTimer?

SetTimer represents the definition of a timer. Implements the OnTimer event in the specified form (CWND), based on the specified form of the definition. Such will be able to correspond to the event.

SetTimer has two functions. One is the global function:: SetTimer ()

UINT SetTimer (
HWND
hwnd, //Handle of window for timer messages
UINT nidevent , //Timer identifier
UINT Uelapse , //time-out value
Timerproc Lptimerfunc /Address of the timer procedure
);

The HWND is a pointer to CWnd, the form class that handles the timer event.

Say the Form class (CWnd), we need to look at the inheritance of CWnd: CWnd has the next face class: Cframewnd,cdialog,cview,ccontrolbar class. This also means that the SetTimer event 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 subclasses of CWnd are able to invoke the function. To set the trigger.

UINT SetTimer (UINT nidevent, uintnelapse, void (CALLBACK export* Lpfntimer) (HWND, uint, uint, DWORD));

The meaning of the parameters:

Nidevent: Refers to setting the ID of this timer. That is, the identity flag, so in the OnTimer () event. Talent is based on different timers. To do different event responses. This ID is an unsigned integral type.

Nelapse

Refers to the time delay.

The unit is in milliseconds.

This means that. The system calls once OnTimer () every nelapse milliseconds.

void (CALLBACK export*lpfntimer) (HWND, uint, uint, DWORD)

Specifies the address of the Application-supplied TimerProc callback function that processes thewm_timer messages. If This parameter isNULL, thewm_timer messages was placed in the application ' s message queue and handled By theCWnd object.

It means. Specifies the address of the TIMERPROC callback function provided by the application. Come in here for this timer event.

The assumption is null. Handle the definition of this timer event as a CWnd object for this timer. He passed the WM_TIMER message to the object. This timer event is handled by implementing the OnTimer () event of this object.

So, under normal circumstances, we'll set this value to NULL. There is a ontimer () function in the object that sets the timer to handle this event.

The same, let's look at the definition of KillTimer () and OnTimer ():

KillTimer with SetTimer (), he also has two, one is global:: KillTimer (), and one is a function of CWnd.

His statements are for example the following:

Global functions

BOOL KillTimer (
HWND
hwnd, //Handle of window that installed timer
UINT uidevent //Timer identifier
);

CWnd function

BOOL killtimer (int nidevent);

These two functions mean that the timer with ID nidevent is removed.

Make it no longer useful. It uses the same method as SetTimer ().

And look at OnTimer ()

Cwnd::ontimer

afx_msg void OnTimer (UINT nidevent);

OnTimer () is the WM_TIMER message that is generated in response to a CWnd object. Nidevent indicates the ID of the timer event to respond to.

Second, the use of the timer event:

By the above analysis. We should be very clear about how to use the Timer event. Suppose we animate a gradient on the view.

We first add a menu item to the menu bar. To add a command response to this menu:

Pview->settimer (1,1000,null);//pview is a pointer to the view class. Here is the setting of a timer in the view class.

To complete the registration, add a class for the view wm_timer corresponding to the event.

The OnTimer () feature is written in Korea. corresponding behavior.

So. You can make animations.

How to use SetTimer MFC is not specific enough

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.