How to use SetTimer

Source: Internet
Author: User

1. Where is the definition of SetTimer?

SetTimer represents the definition of a timer. Implements the OnTimer event in the specified window (CWnd), as specified by the definition, so that the event can be appropriate.

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
);

Where the HWND is a pointer to CWnd, the window class that handles the timer event. Speaking of the window class (CWnd), it is necessary to take a look at the inheritance of CWnd: CWnd has the following sub-categories: 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 child class 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 meaning:

Nidevent: Refers to the setting of the ID of this timer, that is, the identity flag, so that in the OnTimer () event, according to 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 is NULL, thewm_timer messages was placed in the application ' s message queue and handle D by theCWnd object.

This is the timer event that specifies the address of the Timerproc callback function that the application provides. If it is null, this timer event is processed to define 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.

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

KillTimer with SetTimer (), he also has two, one is global:: KillTimer (), and the other is a function of CWnd. His statement is as follows:

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. Its usage is the same 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.

Add, and then add a Wm_timer event to the view class accordingly. Write the Chinese book in the OnTimer () function, and do it accordingly.

This makes it possible to animate.

How to use SetTimer

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.