SetTimer is an API function that is located in User32.dll. You want to do one thing at a time, and you can use it every once in a while. The method of using timers is simple, usually tells Windows a time interval, and then Windows periodically triggers the program at that interval. There are usually two ways to do this: sending Wm_timer messages and invoking application-defined callback functions. When you do not need to specify a timer, you can call the corresponding KillTimer function to destroy the specified clock.
1 How the function is used 1.1 Use Wm_timer to set the timer SetTimer function prototype UINT_PTR SetTimer (HWND hwnd,//Window handle uint_ptr nidevent,//timer ID, multiple timers, the ID can be used to determine which timing Nelapse,//time interval, in milliseconds timerproc lptimerfunc//callback function); Return value: Type: uint_ptr If the function succeeds, the HWND parameter is 0, the newly established clock number is returned, and the clock number can be passed to KillTimer to destroy the clock. If the function succeeds and the HWND parameter is not 0, a nonzero integer is returned, which can be passed to KillTimer to destroy the clock. If the function fails, the return value is 0. If you want to get more error messages, call the GetLastError function. For example SetTimer (m_hwnd,1,1000,null); A timer that triggers once in 1 seconds is encapsulated in the SetTimer in the MFC program, and the call does not have to specify a window handle, so the prototype of the SetTimer function becomes: uint SetTimer (UINT nidevent,uint nelapse, void (CALLBACK EXPORT *lpfntimer) (Hwnd,uint, Yint, DWORD)) when the SetTimer function is used, a timer is generated, and nidevent in the function refers to the identifier of the timer, which is the name. Nelapse refers to the time interval, which is how often an event is triggered. The third parameter is a callback function, in which you put the code for the thing you want to do, you can set it to null, that is, by using the default callback function of the system, the default is the OnTimer function. How is this function generated? You need to generate the OnTimer function in the class that needs the timer: In ClassWizard, select the class that needs the timer, add the WM_TIMER message map, and automatically generate the OnTimer function. Then add code to the function and let the code implement the function. It will be executed automatically every once in a while. Example: SetTimer (Null,1,1000,null); Null defaults to the main process call 1: The name of the timer, 1000: The time interval, in milliseconds, and NULL: Use the OnTimer function. Call KillTimer (nidevent) when no timer is required; For example: KillTimer (1);1.2 Call callback function This method first writes a callback function of the following format void CALLBACK timerproc (HWND hwnd,uint nmsg,uint ntimerid,dword dwtime); Then use the SetTimer (1,100, timerproc) function to build a timer, the third parameter is the callback function address.2 Add two or more than two timer to continue with the SetTimer function bar, the last timer ID is 1, this time can be 2,3,4 .... SetTimer (2,1000,null); SetTimer (3,500,null); Well, windows will coordinate with them. Of course the OnTimer function body also has to change, to add each timer's processing code in the function body: OnTimer (nidevent) {switch (nidevent) {Case 1: ...; break; case 2: .... ; Break Case 3: ...; Break } }3 Oncemore Timer event, or timer event, is an event that is frequently used in game programming. It can produce the effect of timed execution action 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 U INT 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 set 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 cal Lback function that processes the WM_TIMER messages. If This parameter was NULL, the wm_timer messages is placed In the application ' s message queue and handled by the CWnd 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, like SetTimer (), he has two, one is global: KillTimer (), and the other is a function of CWnd. His statement is as follows://global function 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 (). Then 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.2 Timer Event Usage: 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 and add a command response to the menu: Pview->settimer (1,1000,null);//pview is a pointer to the view class, here is a timer set in the View class. Add, and then add a response to the Wm_timer event to the view class. Write a function in the OnTimer () function to respond. Timer accuracy: Timer uses time interrupt response timing, Windows Time interrupt every 1/18 seconds trigger once, so the timer minimum accuracy of about 55ms, below this time is not enough precision.
How to play the timer in Winodws