Method of using timer in non-window classes (2)

zhaozj2021-02-16  45

4. Using the timer

Windows provides a timer that helps us write a program that regularly sends a message. The timer generally notifies the application interval in the application.

(1) Send a WM_TIMER message to the specified window, that is, the following to give the method used in the window class.

(2) Call an application-defined callback function, which is to use in non-window classes.

4.1 Using Timers in Window Class

It is relatively simple to use the timer in the window class. If we want to put an electronic clock on this window so that we must update each 1 second or 0.5 seconds. Follow the steps below to complete this electronic clock program, and know how to use the timer in the window class:

First do a Label control on our new project's main window to display time. then

(1) Set a timer with a function setTimer, and the function format is as follows:

Uint setTimer (uint nidevent,

Uint NELAPSE,

Void (Callback Export * LPFNTIMER) (HWND, UINT, UINT, DWORD);

This function is a member function of the CWND class, its parameters are as follows:

Nidevent: When the timer flag value specified for the set timer, when multiple timers are set, the value of each timer is different, and the message processing function is to determine which timer is judged by this parameter. Here we set to 1.

NELAPSE: Specifies the interval of sending messages, in milliseconds. Here we set to 1000, which is one second.

lpfntimer: Specify which callback function of the timer message is executed, if it is empty, WM_TIMER will join the application's message queue and processed by the CWND class. Here we set to NULL.

The final code is as follows:

SetTimer (1,1000, null);

(2) The mapping function of the WM_TIMER message is added to the main window class via Class Wizard, and the default is ONTIMER (Uint NidEvent).

(3) Then we can add our code in the implementation of the ONTIMER (Uint Nidevent). The parameter nidevent is the flag value specified when we previously set the timer, where we can distinguish between different timers, and make different processing. The added code is as follows:

Switch (nidevent)

{

Case 1:

CTIME M_SYSTIME = CTIME :: getCurrentTime ();

Setdlgitemtext (idc_static_time, m_systime.format ("% y year% M month% D day% h:% m:% s"));

Break;

}

IDC_STATIC_TIME in the code is the ID of the Label control we have previously added.

At this point, our electronic clock procedures are completed.

4.2 Using Timers in Non-window classes

Use timers in non-window classes to use all the knowledge we introduced before. Because it is no window, we cannot use the method of sending a message map in the window class to set the timer, at this time, the callback function must be used. And because the callback function is having a certain format, its parameters cannot be decided by us, so we can't use the parameters to pass this. However, the static member function can access static member variables, so we can save this in a static member variable, you can use this pointer in a static member function, and this method is still a line for only one instance. Because there is only one copy in a class, we can't use it for a class of multiple instances. The solution is to put the timer flag value as a keyword, a pointer to the class instance, saved in a static mapping table because it is the only flag value, and it can quickly retrieve the examples in the mapped table. The pointer, because it is static, so the callback function can access them. First introduce the function used to set the timing:

Uint setTimer

HWND HWND, // Handle of Window for Timer Messages

Uint nidevent, // Timer Identifier

UINT UELAPSE, / / ​​TIME-OUT VALUE

TimerProc Lptimerfunc // Address of Timer Procedure

);

The parameters are the following:

HWnd: Specifies the handle of the window associated with the timer. Here we have NULL.

NidEvent: Timer flag value, if the HWND parameter is null, it will be skip, so we also set to NULL.

UELAPSE: Specifies the time interval for sending messages, in milliseconds. Here we do not specify, with parameters.

LPTIMERFUNC: Specifies the address of the function that is ruled when the interval is time, that is, the callback function here. The format of this function must be the following format:

Void Callback TimerProc

HWND HWND, // Handle of Window for Timer Messages

UINT UMSG, // WM_TIMER MESSAGE

Uint IDEvent, // Timer Identifier

DWORD DWTIME / / CURRENT System Time

);

The parameters are the following:

HWND: The handle of the window associated with the timer.

UMSG: WM_TIMER message.

IDEvent: Timer flag value.

Detime: The time after the system is started, the unit milliseconds.

Finally set the code of the timer:

M_ntimerid = setTimer (NULL, NULL, NELAPSE, MyTimerProc);

Create a window class with Class Wizard, select the generic class class, class name CMYTIMER, the role of this class is to remind us to do something every other period, then create three instances, each instance Different time intervals remind us to do different things.

Mytimer.h

#include

Class CMYTIMER;

// Define a data type with mapping table class in a template class

Typedef CMAP ctimermap;

Class CMYTIMER

{

PUBLIC:

/ / Set the timer, the NLAPSE indicates the interval, SZ indicates the content you want to prompt.

Void setMytimer (uint NELAPSE, CSTRING SZ); / / Destroy the timer of this instance

Void killmytimer ();

/ / Save the timer flag value of this instance

Uint m_ntimerid;

/ / Static data member to be prompted

Cstring szcontent;

// Declare a static data member, mapping the table class, to save all timer information

Static CTimermap M_stimeMap;

// Static member function, message for processing timers

Static Void Callback MyTimerProc (HWND HWND, UINT UMSG, UINT Idevent, DWORD DWTIME);

CMYTIMER ();

Virtual ~ cmytimer ();

}

Mytimer.cpp

#include "stdafx.h"

#include "mytimer.h"

/ / Must define a static data member outside

CTimermap CMYTIMER :: m_stimemap;

CMYTIMER :: CMYTIMER ()

{

m_ntimerid = 0;

}

CMYTIMER :: ~ cmytimer ()

{

}

Void Callback CMYTIMER :: MyTIMERPROC (HWND HWND, UINT UMSG, UINT Idevent, DWORD DWTIME)

{

Cstring sz;

Sz.Format ("% D Timer:% S",

IDEVENT,

m_stimemap [idevent] -> szcontent);

AfxMessageBox (SZ);

}

Void CMYTIMER :: SETMYTIMER (uint NELAPSE, CSTRING SZ)

{

Szcontent = SZ;

M_ntimerid = setTimer (NULL, NULL, NELAPSE, MyTimerProc);

m_stimemap [m_ntimerid] = this;

}

Void CMYTIMER :: KillmyTimer ()

{

Killtimer (NULL, M_NTIMERID);

m_stimemap.removekey (m_ntimerid);

}

This completes the method of using the timer in a non-window class. These codes are compiled in Windwos 2000 Professional and Visual C 6.0.

5 Conclusion

Through the above introduction, you should know how to access non-static data members and non-static member functions in a static member function and learn how to use timers in non-window classes. Of course, this is just a way to solve this problem, I believe there is a better solution. This approach has certain flexibility, which can be used in many places, such as the connection timeout in the network program, and the timing refresh, etc., you can use this method.

Appendix: A Demo Running Interface and Download Address

1. Run interface:

2. download link:

references:

1 Pan Aimin Zhang Li.c primer Chinese version. (Third Edition). Beijing: China Electric Press. 2002

2 Jeff Prosise.mfc Windows program design. (Second Edition). Beijing: Tsinghua University Press. 2001

3 Wang Suntue, Liu Baohong. Principle and Application of Multirled Programming in Windows Environment. Beijing: Tsinghua University Press. 2002

4 Hou Junjie. In-depth, Shallow MFC. Taiwan: Songgang Computer Damage Co., Ltd.1998

About the Author:

Last name: Liu Hui

Written name: I am in the world

Work unit: Net into Technology Kunshan Co., Ltd.

Contact: email: jemmyliu@163.com Professional Titles: Software Engineers

Research direction: software development using Visula C

I Note: This is the first technical article I wrote about programming. The purpose of writing this article is just to feel comparison methods in the process of doing projects and learning, and share it with you. Among them, there are many mistakes, please tell me, everyone can post this article, but please keep the author's name, thank you!

转载请注明原文地址:https://www.9cbs.com/read-26192.html

New Post(0)