Home

Windows Controls: The Timer

  

Introduction to Timers

     

Description

A timer is a non-spatial object that uses recurring lapses of time from a computer or from an application. To work, every lapse of period, the control sends a message to the operating system. The message is something to the effect of "I have counted the number of lapses you asked me to count".

As opposed to the time set on a computer, a timer is partly but greatly under your control. Users do not see nor use a timer as a control. As a programmer, you decide if, why, when, and how to use this control.

Practical LearningPractical Learning: Introducing the Timer Control

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click MFC Application and set the name to CurrentTime1
  4. Click OK
  5. In the first page of the wizard, click Next
  6. In the second page of the wizard, click Dialog based and click Next
  7. In the third page of the wizard, click Finish
  8. On the dialog box, click the TODO label
  9. In the Properties window, change the following characteristics:
    Caption: 00:00:00 AM
    ID: IDC_CURRENT_TIME
  10. Click an unoccupied area of the dialog box
  11. In the Properties window, click Font (Size) then click ellipsis button
  12. In the Font box, select Times New Roman
  13. In the Font Style, click Bold
  14. In the Size box, click 24
  15. Click OK
     
    Timer
  16. Right-click the 00:00:00 AM label and click Add Variable...
  17. Set the Category to Value
  18. Make sure the type is set to CString and set the name to m_CurrentTime
  19. Click Finish

Creating a Timer

Unlike most other controls, the MFC timer has neither a button to represent it nor a class. To create a timer, you simply call the SetTimer() member function of the CWnd class. Its syntax is:

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

This function call creates a timer for your application. Like the other controls, a timer uses an identifier. This is passed as the nIDEvent argument. As mentioned already, when it is accessed, a timer starts counting up to a set value. Once it reaches that value, it stops and starts counting again. The nElapse argument specifies the number of milliseconds that the timer must count before starting again. The lpfnTimer argument is the name of a procedure that handles the timing event of the control. This argument can be set to NULL, in which case the timing event would rest on the CWnd's responsibility.

Practical LearningPractical Learning: Using Timer Controls

  1. In the Class View, expand the CurrentTime1 project and click CCurrentTime1Dlg
  2. In the lower part of the Class View, double-click OnInitDialog
  3. Change the event as follows:
    BOOL CCurrentTime1Dlg::OnInitDialog()
    {
        CDialogEx::OnInitDialog();
    
        // Add "About..." menu item to system menu.
    
        // IDM_ABOUTBOX must be in the system command range.
        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
        ASSERT(IDM_ABOUTBOX < 0xF000);
    
        CMenu* pSysMenu = GetSystemMenu(FALSE);
        if (pSysMenu != NULL)
        {
    	BOOL bNameValid;
    	CString strAboutMenu;
    	bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
    	ASSERT(bNameValid);
    	if (!strAboutMenu.IsEmpty())
    	{
    		pSysMenu->AppendMenu(MF_SEPARATOR);
    		pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    	}
        }
    
        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE);			// Set big icon
        SetIcon(m_hIcon, FALSE);		// Set small icon
    
        // TODO: Add extra initialization here
        SetTimer(1, 200, 0);
    
        return TRUE;  // return TRUE  unless you set the focus to a control
    }

Characteristics of a Timer

 

Setting the Timer

When a timer is accessed or made available, it starts counting. Once the nElapse value of the CWnd::SetTimer() member function is reached, its sends a WM_TIMER message to the application.

Stopping the Timer

We saw that a timer is initiated with a call to SetTimer(). When you do not need the timer anymore, call the CWnd::KillTimer() member function. Its syntax is:

BOOL KillTimer(int nIDEvent);

The nIDEvent argument identifies the timer that was created with a previous call to SetTimer().

 
 
 

Practical LearningPractical Learning: Using the OnTimer Event

  1. To generate the OnTimer() event, in the Class View, click CCurrentTime1Dlg
  2. In the Properties window, click the Messages button Messages
  3. Click the WM_TIMER field and click the arrow of its box
  4. Select <Add> OnTimer and implement the event as follows:
    void CCurrentTime1Dlg::OnTimer(UINT_PTR nIDEvent)
    {
        // TODO: Add your message handler code here and/or call default
        CTime CurrentTime = CTime::GetCurrentTime();
    
        int iHours   = CurrentTime.GetHour();
        int iMinutes = CurrentTime.GetMinute();
        int iSeconds = CurrentTime.GetSecond();
        CString strHours, strMinutes, strSeconds;
    
        if( iHours < 10 )
    	strHours.Format(_T("0%d"), iHours);
        else
    	strHours.Format(_T("%d"), iHours);
    
        if( iMinutes < 10 )
    	strMinutes.Format(_T("0%d"), iMinutes);
        else
    	strMinutes.Format(_T("%d"), iMinutes);
    
        if( iSeconds < 10 )
    	strSeconds.Format(_T("0%d"), iSeconds);
        else
    	strSeconds.Format(_T("%d"), iSeconds);
    
        m_CurrentTime.Format(_T("%s:%s:%s"), strHours, strMinutes, strSeconds);
    
        UpdateData(FALSE);
        CDialogEx::OnTimer(nIDEvent);
    }
  5. To test the application, press F5
     
    Timer
     
    Timer
     
    Timer
  6. Close the dialog box and return to your programming environment
 
 
   
 

Home Copyright © 2010-2011 FunctionX, Inc.