Welcome: Hunan Intelligent Applications Tecgnology CO.,ltd.-HNIAT.com
Language: Chinese ∷  English

Basic knowledge

Summary of MFC timer SetTimer function usage

The SetTimer member function of the CWnd class can only be called in the CWnd class or its derived classes, while the API function SetTimer does not have this limitation, which is an important difference.

1. Start the timer.

To start the timer, you need to use the CWnd member function SetTimer. The prototype of CWnd :: SetTimer is as follows:



The parameter nIDEvent specifies a non-zero timer ID;

The parameter nElapse specifies the interval time in milliseconds;

The parameter lpfnTimer specifies the address of a callback function. If the parameter is NULL, the WM_TIMER message is sent to the application's message queue and processed by the CWnd object. If the function succeeds, it returns a new timer ID. We can use this ID to destroy the timer through the KillTimer member function. If the function fails, it returns 0.

Through the SetTimer member function, we can see that there are two ways to handle timing events, one is through the message response function of the WM_TIMER message, and the other is through the callback function.

If you want to start multiple timers, call the SetTimer member function multiple times. In addition, there can be timers with the same ID in different CWnds, and they do not conflict.

2. Add a message processing function for the WM_TIMER message, or define a callback function.

If the last parameter of the CWnd :: SetTimer function is NULL, the timer event is processed by the WM_TIMER message processing function. The method for adding a WM_TIMER message processing function is to select the class to add a timer in the VS2010 project class wizard, find the WM_TIMER message in the message list, and add a message processing function. After adding, the following content will appear in the cpp file:

C ++ code

BEGIN_MESSAGE_MAP (CExample44Dlg, CDialogEx)

......

ON ON_WM_TIMER ()

END_MESSAGE_MAP ()

Zh

voidCExample44Dlg :: OnTimer (UINT_PTR nIDEvent)

{Min

// TODO: Add your message handler code hereand / or call default

Zh

CDialogEx :: OnTimer (nIDEvent);

}

After that, the corresponding processing can be performed in the OnTimer function. OnTimer parameter nIDEvent is the timer ID, that is, the timer ID specified in the SetTimer member function. If there are multiple timers, we can handle it as follows:

C ++ code

voidCExample44Dlg :: OnTimer (UINT_PTR nIDEvent)

{// TODO: Add your message handler code here and / or call default

Switch (nIDEvent)

{Noun

case 1:

// Add the task of executing timer 1 here;

Break; break

Case2: case2:

// Add the task of executing timer 2 here;

Break; break;

...... ......

Default: default

Break; break

}}

CDialogEx :: OnTimer (nIDEvent);

}

Zh

If the last parameter of the CWnd :: SetTimer function is not NULL, you need to define a callback function. The form of the callback function is as follows:

C ++ code





The parameter hWnd is the handle of the CWnd object that calls the SetTimer member function, that is, the handle of the window that owns this timer; the parameter nMsg is WM_TIMER, and it is always WM_TIMER; the parameter nIDEvent is the timer ID; the parameter dwTime is the number of milliseconds since the system was started That is, the return value of the GetTickCount function.

In this way, the last parameter of the CWnd :: SetTimer function can be TimerProc.

Note: The name of the callback function is not necessarily TimerProc, it can be other names, but the return value type, parameter type and number cannot be changed.

Example of callback function:

C ++ code

VOIDCALLBACK TimerProc (HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)

{Noun

Switch (nTimerid)

Ruthless

Case 1: case 1:

// Handle the event of the timer with ID 1

Break; break;

Case 2: case 2:

// Handle the event of the timer with ID 2

Break; break;

......

Default:

Break; break

}}

}

Zh

The callback function is a global function, which needs to be written in front of where it is used, or written later and then declared before use.

3. Destroy the timer.

When you no longer use the timer, you can destroy it. To kill a timer, use the KillTimer member function of the CWnd class. The prototype of the CWnd :: KillTimer function is as follows:

C ++ code

BOOLKillTimer (UINT_PTR nIDEvent);

Zh

The parameter nIDEvent is the ID of the timer to be destroyed, and is the timer ID set when the CWnd :: SetTimer function is called. Returns TRUE if the timer is destroyed, or FALSE if the specified timer is not found.

If multiple timers are to be destroyed, the KillTimer function is called multiple times and the IDs of the timers to be destroyed are passed in.


4.MFC timer classic example

Implementation function: Click the "Start Counting" button in the dialog box, and the numbers in the edit box are displayed in increments of 1s.

As shown below:



Production steps:

1. Create a new project in VS2010, named "Timer", and select MFC Wizard to create a dialog based;

2. In the automatically generated dialog template, delete all static text controls and button controls. Add an edit box control and a button control, use the default IDC_EDIT1 and IDC_BUTTON1, and set the ReadOnly property of the edit box to True.

3. In the class wizard of the CTimerDlg class, add an int type member variable m_CountTime and a CEdit type m_Edit1 object to the edit box IDC_EDIT1.

① Found in CTimerDlg class TimerDlg.h header file

int m_CountTime; modify it to

static int m_CountTime; // static integral member variable;

② Delete the constructor of the TimerDlg.cpp source file of the CTimerDlg class:

m_CountTime = 0;

③ Add the CTimerDlg class below the header file of the TimerDlg.cpp source file:

int CTimerDlg :: m_CountTime = 0; // Initialization of static integer member variables globally;

int Num = 0; // Global variables;

4. Find the BOOLCTimerDlg :: OnInitDialog () function in the CTimerDlg class TimerDlg.cpp // TODO: Add additional initialization code here, add below it:



Note: The size of the edit box should be adjusted to fit the set text font size;

5. Add the callback function used in the SetTimer function to the TimerDlg.cpp source file of the CTimerDlg class:



6. Double-click the "Start Timing" button to add its message handler:





7. In the Class Wizard of the VS2010 project, select the CTimerDlg class, find the WM_CLOSE message in the message list, and add a message processing function for the "Close" button in the upper right corner of the dialog box:



8. Summary:

① In the callback function of SetTimer function, if you want to use the member variable m_CountTime in the class, you need to not only modify m_CountTime to a static member variable, but also initialize and assign a value to m_CountTime globally. However, although this compiles without errors, it cannot achieve the self-increment of the m_CountTime value every 1s.

② Globally defined int Num = 0; variable, in the message processing function of the "start timer" button, assign Num to m_CountTime, and then display the m_CountTime value in the edit box. In the callback function of SetTimer function, let Num auto-increase.

________________________________________________________________________________________________________________________________________________________________________


Appendix:

Using timers through Windows API functions

1. Start the timer.

Using the API function SetTimer to start the timer, the prototype of the SetTimer function is as follows:

C ++ code

UINT_PTRSetTimer (

HWND

HWnd, hWnd,

UINT_PTR

NIDEvent, nIDEvent,

UINT UINT

UElapse,

TIMERPROC

LpTimerFunc

);

Zh

The parameter hWnd is the handle of the window associated with the timer; the parameter nIDEvent is a non-zero timer ID. If hWnd is equal to NULL and there is no timer with the ID nIDEvent, then the nIDEvent parameter is ignored and a new ID is generated. Timer, and if hWnd is not NULL, and the window specified by hWnd already has a timer with ID nIDEvent, then this existing timer is replaced by a new timer. Parameters uElapse and lpTimerFunc are the same as the CWnd :: SetTimer function.

Add a message processing function for the WM_TIMER message, or define a callback function.

If the last parameter is NULL when calling the SetTimer function, we need to add a processing function for the WM_TIMER message by ourselves. It should be noted that the additional data wParam of the WM_TIMER message is the timer ID, lParam is a pointer to the callback function. Is NULL, then lParam is also NULL.

If the last parameter of the SetTimer function is not NULL, we need to define a callback function. The definition of the callback function is the same as the MFC timer.

3. Destroy the timer.

The KillTimer API function is used to destroy the timer. The prototype is as follows:

C ++ code

BOOLKillTimer (HWND hWnd, UINT_PTR uIDEvent);

The parameter hWnd is the handle of the window associated with the timer, and has the same value as the hWnd parameter of the SetTimer function when the timer is started; the parameter uIDEvent is the ID of the timer to be destroyed. If the parameter hWnd passed to SetTimer is valid, uIDEvent should be the same as the passed The parameter nIDEvent given to SetTimer is the same, and if the parameter hWnd of SetTimer is NULL, uIDEvent should be the timer ID returned by SetTimer. The function returns TRUE on success, otherwise FALSE.
————————————————
Copyright statement: This article is an original article of the CSDN blogger "Haoyue Yingjiang", which follows the CC 4.0 BY-SA copyright agreement. Please reprint the original source link and this statement.
Original link: https://blog.csdn.net/u013541325/article/details/41143495

CONTACT US

Contact: Manager Xu

Phone: 13907330718

Tel: 0731-22222718

Email: hniatcom@163.com

Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province

Scan the qr codeClose
the qr code