This article describes the use of VC timer, share for everyone to reference. The specific usage analysis is as follows:
The timer is used frequently in VC, its prototype is:
Copy Code code as follows:
Winuserapi UINT WINAPI SetTimer (HWND hwnd, UINT nidevent, UINT uelapse, Timerproc lptimerfunc);
The parameter usage is as follows:
An hWnd is a form handle that wants to set a timer. The system sends a WM_TIMER message to the form when it is timed out.
Nidevent Timer identifier. Multiple timers can be used in a single window, and different timers are differentiated according to Nidevent.
Uelapse time, in milliseconds.
The callback function for the Lptimerfunc timer. If the value is NULL, the timer sends a message wm_timer the function of the message by the form image, otherwise handled by the callback function, in other word, the callback function is the replacement of the OnTimer handler function.
In general, we use only three parameters when using timers, that is,
Copy Code code as follows:
UINT Cwnd::settimer (
UINT Nidevent,
UINT Nelapse,
void (CALLBACK export* lpfntimer) (
HWND, UINT, UINT, DWORD));
In fact, this function is only MFC's encapsulation of the API, its implementation function is:
Copy Code code as follows:
_afxwin_inline UINT Cwnd::settimer (UINT nidevent, UINT nelapse,
void (callback* Lpfntimer) (HWND, UINT, UINT, DWORD))
{
ASSERT (:: IsWindow (m_hwnd));
Return:: SetTimer (M_hwnd, Nidevent, Nelapse, (Timerproc) lpfntimer);
}
Thus, Cwnd::settimer simply sets the first parameter of the API function SetTimer to its own handle.
With the above understanding, the use of the timer is clear, the following examples illustrate the specific use of the timer.
1. Open VC, a new project based on the dialog box, the project name is test. Add a button to the dialog box, change its ID to idc_button_start,caption, and change to START. Image the bn_clicked message for the button, void Ctestdlg::onbuttonstart ();
2. Then add a button on the dialog box, the ID is id_button_stop,caption to STOP, and the image message is void Ctestdlg::onbuttonstop ();
3. Add a lable,id change to Idc_static_time, for the count, indicating the execution of the timer function.
4. Image dialog box Wm_timer message, void Ctestdlg::ontimer (UINT nidevent);
Begin_message_map (Cassistantdlg, CDialog)
On_wm_timer ()
End_message_map ()
The following function is shown here:
Copy Code code as follows:
void Ctestdlg::onbuttonstart ()
{
SetTimer (1,1000,null)/Start timer 1, timer time is 1 seconds
}
void Ctestdlg::onbuttonstop ()
{
KillTimer (1); Turn off timer 1.
}
void Ctestdlg::ontimer (UINT nidevent)
{
static int ntimer=0;
CString strtmp= "";
Strtmp.format ("Timer:%d", ntimer++);
CWnd *pwnd=getdlgitem (idc_static_time);
Pwnd->setwindowtext (strtmp); Set the new value in the lable to indicate that the timer is already working.
Cdialog::ontimer (nidevent);
}
The use of callback functions.
If you do not want to use the form's Wm_timer message function, you can replace it with a callback function, which, based on the example above, adds a callback function to confirm the previous discussion.
First, define a callback function, and the definition of the callback function must be in the following format.
Copy Code code as follows:
void CALLBACK Timerproc (HWND hwnd,uint umsg,uint idevent,dword dwtime);
My implementation function is as follows:
Copy Code code as follows:
void CALLBACK Timerproc (HWND hwnd,uint umsg,uint idevent,dword dwtime)
{
AfxMessageBox ("Timer is running!"); /timer time to, strong out a dialog box, indicating that the timer has run.
}
Modify the above startup function slightly
Copy Code code as follows:
void Ctestdlg::onbuttonstart ()
{
SetTimer (1,1000,null)/Start timer 1, timer time is 1 seconds
SetTimer (1,1000, (Timerproc) timerproc);//With a callback function, at which point the message handler function of the dialog box is no longer processed.
}
I hope this article will help you with your VC programming.