How to use SetTimer MFC is specific enough

Source: Internet
Author: User
Tags message queue

Turn from: http://blog.csdn.net/ellor/article/details/1714741

A timer event, the timer event, is an event that is often used in game programming. It can produce the effect of timed operation. In this article, we will discuss with you 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) according to the form specified by the definition, so that the event can be corresponding.

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 can call 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 symbol, so that in the OnTimer () event, talent based on different timers, to do different event response. This ID is an unsigned integral type.

Nelapse

Refers to the time delay. The unit is in milliseconds. This means that every nelapse millisecond system is called once OnTimer ().

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 Han Dled by theCWnd object.

This is the timer event that specifies the address of the Timerproc callback function that the application provides. The assumption is null, which handles the definition of this timer event for the CWnd object of this timer. He passes the WM_TIMER message to this object and handles the timer event by implementing the object's OnTimer () event.

So, in general, we set this value to null and have the OnTimer () function in the object that sets the timer to handle the 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:

From 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 and add a command response to the menu:

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

Join to complete, and then add a Wm_timer event to the view class corresponding. Write the Chinese book in the OnTimer () function to correspond.

This makes it possible to animate.

How to use SetTimer MFC is 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.