How to use OnTimer () function to implement timing control in MFC

Source: Internet
Author: User
Tags time interval

Recently to implement a sniffer-like network capture program, which requires a button has the following functions: When the button starts to grab the bag, and then press once to stop grabbing the bag. Start thinking about using multi-process implementations, but this is more cumbersome. In fact, this function can be implemented with a timer.   Described below: In general, there are three functions in total: SetTimer, KillTimer () and OnTimer (). First look at SetTimer, the prototype of this API function Uint_ptr SetTimer (
HWND hwnd,//Window handle
Uint_ptr nidevent,//timer ID, multiple timers, this ID can be used to determine which timer
UINT Uelapse,//time interval, in milliseconds
Timerproc Lptimerfunc//callback function
); For example
SetTimer (M_hwnd,1,1000,null); A timer that triggers once in 1 seconds
In an MFC program, SetTimer is encapsulated in a CWnd class, and the invocation does not 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. The nidevent in the function refers to the identifier of the timer, which is the name. Nelapse refers to the time interval,
That is, the event is triggered every few hours. 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, using the system default callback function, the system defaults to the OnTime function. How is this function generated? You need to generate the OnTime function in the class that needs the timer:
In ClassWizard, select the class that needs the timer, add the Wm_time message map, and generate the OnTime function automatically. 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 (1,1000,null); 1: The name of the timer, 1000: The time interval, in milliseconds, and NULL: Use the OnTime function. Call KillTimer (nidevent) when no timer is required;   For example: KillTimer (1);    Well, let's talk about OnTimer.   In MFC, using OnTimer is very simple, select the dialog box in the Dialog editor interface, view the event, find the Wm_timer event, and edit the event handler. Then use a simple example to achieve the function I said above: This is the interface after the test program is completed in the "Start" button inside the edit click event handler void Ctimertestdlg::onbnclickedbtnstart ()
{
TODO: Add control notification Handler code here
M_btn_start =!m_btn_start;
cwnd* Mywnd = GetDlgItem (Idc_btn_start);
if (M_btn_start = = true)//button is pressed
{
Mywnd->setwindowtexta ("Stop");
SetTimer (1,1000,null); }
Else
{
Mywnd->setwindowtexta ("Start");
KillTimer (1);
}
Then edit OnTimer function void Ctimertestdlg::ontimer (Uint_ptr nidevent)
{
TODO: Add Message Handler code and/or call default values here
Cdialog::ontimer (nidevent);
M_list_ctrl.addstring ("I am a Timer");
} That's it, get it done.

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.