How to use a Windows Timer

Source: Internet
Author: User

Timer is frequently used in VC. The following describes how to use the definer.
The timer prototype is:
Winuserapi uint winapi settimer (hwnd, uint nidevent, uint uelapse, timerproc lptimerfunc );
Hwnd is the form handle to set the timer. When the scheduled time is reached, the system will send the wm_timer message to the form.
Nidevent timer identifier. Multiple timers can be used in a form. Different timers are differentiated by nidevent.
The scheduled time of uelapse, in milliseconds.
The callback function of the lptimerfunc timer. If the value is null, when the scheduled time is reached, the message wm_timer sent by the timer is processed by the function of the form image for the message. Otherwise, the callback function processes the message, the callback function replaces the ontimer processing function.

Generally, when using a timer, we only use three parameters, that is
Uint cwnd: settimer (
Uint nidevent,
Uint nelapse,
Void (callback Export * lpfntimer )(
Hwnd, uint, uint, DWORD ));
In fact, this function is only the encapsulation of the API by MFC, and its implementation function is:
_ 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 );
}
It can be seen that cwnd: settimer only 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 example shows the specific use of the timer.
1. Open VC and create a dialog-based project named test. In the dialog box, add a button, change its ID to idc_button_start, and change caption to start. Image of the bn_clicked message of the button, void ctestdlg: onbuttonstart ();
2. Add a button in the dialog box with the ID id_button_stop, caption changed to stop, and image message void ctestdlg: onbuttonstop ();
3. Add a lable and change the ID to idc_static_time, which is used for counting, indicating the execution of the timer function.
4. The wm_timer message in the image dialog box, void ctestdlg: ontimer (uint nidevent );
Begin_message_map (cassistantdlg, cdialog)
On_wm_timer ()
End_message_map ()

The preceding fixed functions are as follows:
Void ctestdlg: onbuttonstart ()
{
Settimer (, null); // start timer 1. The timer time is 1 second.
}

Void ctestdlg: onbuttonstop ()
{
Killtimer (1); // disable 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 a new value in lable, indicating that the timer has been working.
Cdialog: ontimer (nidevent );
}

The use of callback functions.
If you do not want to use the form's wm_timer message function processing, you can use the callback function. You can add a callback function based on the above example to confirm the previous discussion.
First, define a callback function. The callback function must be defined in the following format.
Void callback timerproc (hwnd, uint umsg, uint idevent, DWORD dwtime );

My implementation functions are as follows:
Void callback timerproc (hwnd, uint umsg, uint idevent, DWORD dwtime)
{
Afxmessagebox ("timer is running! "); // When the timer time reaches, a dialog box is displayed, indicating that the timer has been run.
}
Slightly modify the above startup Function
Void ctestdlg: onbuttonstart ()
{
// Settimer (, null); // start timer 1. The timer time is 1 second.
Settimer (, (timerproc); // use the callback function for processing. At this time, the message processing function of the dialog box is no longer processed.
}

 

Original article:

Http://hi.baidu.com/zhoutianyang/blog/item/97dd935050ccef6185352492.html.

 

Related Article

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.