The use of Timer OnTimer function in MFC

Source: Internet
Author: User
in MFC we often need to read and write data regularly, or periodically refresh the interface, update the data and state, which requires the use of timers, the implementation of the function is OnTimer, following its usage steps (based on VS2010) for a brief description:

1. In Class View, click the dialog class that needs to use the timer, select the message in the Properties window, select Wm_timer in the following message function, and then select Add OnTimer function, as shown in the following figure:

2. Write OnTimer function

void Cld_powerdlg::ontimer (uint_ptr nidevent)           //timed read data
{
    switch (nidevent)   
    {case  
        1:   // Timer 1 processing function, periodically send data to update
        {
            char *a=null;
            A = new char[12];
            a[0]=0x55;  A[1]=0XAA;  A[2]=0XDC;             Data at the beginning
            a[3]=0x06;
            a[4]=0x00;  
            a[5]=0x03;  
            a[6]=0x44;
            a[7]=0x44;
            A[8]=0XCC;  a[9]=0x33;  A[10]=0xc3; a[11]=0x3c;//end of data
            M_serialport.writedata (a,12);   
            A=null;
            Delete A;
        break; 
        }

        Case 2:   //Timer 2 bits for status bar time information
        {
            CTime t1;
            T1=ctime::getcurrenttime ();//Gets the current system time
            M_statusbar.setpanetext (2,t1. Format ("%y-%m-%d  %h:%m:%s")); Status bar display time break   
        ;   
        }
    }

    Cdialogex::ontimer (nidevent);
}

3. Start Timer

SetTimer (1,1000,null);      Parameters: Timer designator, timing time (ms). Start Timer 1, refresh every 1s

4. Turn off the timer

    KillTimer (1);                               Off timer 1
welcome all of you master great God criticism, also welcome you fellow people comment discussion.

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.