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.