The usage example of VC timer is explained in detail

  • 2020-04-02 02:55:36
  • OfStack

This article is an example of the use of timer in VC, to share for your reference. Specific usage analysis is as follows:

Timer is frequently used in VC, and its prototype is:

WINUSERAPI UINT WINAPI SetTimer ( HWND hWnd , UINT nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc);

The parameter usage is as follows:

hWnd             Is the window handle to set the timer. The WM_TIMER message is sent to the form when the timer is timed.
nIDEvent         Timer identifier. Multiple timers can be used within a form, and different timers are distinguished by nidevents.
uElapse       Timing time, in milliseconds.
lpTimerFunc     The timer callback function. If the value is NULL, the message WM_TIMER sent by the timer will be processed by the function of the form image. Otherwise, it's handled by the callback function, which is essentially the handler that replaces OnTimer.

In general, when we use a timer, we only use three parameters, namely

UINT CWnd::SetTimer( 
UINT nIDEvent,
UINT nElapse,
void (CALLBACK EXPORT* lpfnTimer)(
HWND, UINT, UINT, DWORD) );

In fact, this function is just 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);
}

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 and create a new project based on the dialog box. The project is called Test. Add a button to the dialog box, changing its ID to IDC_BUTTON_START, Caption to start.image the BN_CLICKED message of the button, void CTestDlg::OnButtonStart();
2. Add another button to the dialog with ID ID_BUTTON_STOP,Caption changed to Stop, image message void CTestDlg::OnButtonStop();
3. Add a Lable,ID changed to IDC_STATIC_TIME, used for counting, indicating the execution of the timer function.
4. Image dialog WM_TIMER message, void CTestDlg::OnTimer(UINT nIDEvent);
BEGIN_MESSAGE_MAP (CAssistantDlg CDialog)
      ON_WM_TIMER ()
END_MESSAGE_MAP ()

The above time-order function is shown as follows:

void CTestDlg::OnButtonStart() 
{
       SetTimer(1,1000,NULL);//Start timer 1, the timing time is 1 second
} void CTestDlg::OnButtonStop()
{
       KillTimer(1);        //Turn off timer 1. < br / > } 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);   will Lable Set a new value to indicate that the timer is working.
       CDialog::OnTimer(nIDEvent);
}

The use of callback functions.

If you do not want to use the WM_TIMER message function of the form, you can use the callback function instead, and you can add a callback function to the above example to confirm the previous discussion.

First, define a callback function, which must be defined in the following format.

void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime );

My implementation function is as follows:

void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime )
{
AfxMessageBox("Timer is running!");//Timer time, strong out a dialog box, indicating that the timer has run. < br / > }

Modify the above startup function slightly
void CTestDlg::OnButtonStart() 
{
// SetTimer(1,1000,NULL);// Start timer 1, The timing time is 1 seconds
   SetTimer(1,1000,(TIMERPROC)TimerProc);//Handled by the callback function, at which point the dialog's message handler is no longer processed. < br / > }

Hope that this article described the VC programming for you to help.


Related articles: