The Tick Counter

 

Overview

The Win32 library provides a special function used to count a specific number of lapses that have occurred since you started your computer. This information or counter is available through the GetTickCount() function. Its syntax is:

DWORD GetTickCount(VOID);

This function takes no argument. If it succeeds in performing its operation, which it usually does, it provides the number of milliseconds that have elapsed since you started your computer. Just like the timer control, what you do with the result of this function is up to you and it can be used in various circumstances. For example, computer games and simulations make great use of this function.

After retrieving the value that this function provides, you can display it in a text-based control.

 

 
  1. Start a new MFC Application named TickCounter
  2. Change the design of the IDR_MAINFRAME icon to something different
  3. Create it as Dialog Based
    Here is how we will design the dialog box:
     
  4. Add a Group Box control to the dialog box and set its Caption to Elapsed Time
  5. Add a long Static Text control to the group box. Change its ID to IDC_COMP_TIME and its Caption to
    This computer has been ON for XXXXXXXXXXXXXXXXXXXXX
  6. Add another Static Text control to the group box. Change its ID to IDC_APP_TIME and its Caption to
    This application has been running for XXXXXXXXXXXXXXXXXXXX
  7. Add a Value Variable for the IDC_COMP_TIME identifier and name it m_CompTime
  8. Add a Value Variable for the IDC_COMP_TIME identifier and name it m_AppTime
  9. In the header file of the dialog box, declare an unsigned integer as follows:
     
    public:
    	CString m_CompTime;
    	CString m_AppTime;
    	unsigned int CompTime;
    };
  10. To create a timer control, in the OnInitDialog() event, call the SetTimer() method and initialize the CompTime variable as follows:
     
    BOOL CTickCounterDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// 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
    	CompTime = GetTickCount();
    	SetTimer(1, 100, NULL);
    
    	return TRUE; // return TRUE unless you set the focus to a control
    }
  11. Generate the WM_TIMER message for the dialog class and implement it as follows:
     
    void CTickCounterDlg::OnTimer(UINT nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
    	unsigned long CurTickValue = GetTickCount();
    	unsigned int Difference = CurTickValue - CompTime;
    
    	m_CompTime.Format("This computer has been ON for %d", CurTickValue);
    	m_AppTime.Format("This application has been running for %d", Difference);
    	UpdateData(FALSE);
    
    	CDialog::OnTimer(nIDEvent);
    }
  12. Test the application
     
  13. After testing the application, close it and return to MSVC
  14. To make the values easier to read, change the code of the OnTimer event as follows:
     
    void CTickCounterDlg::OnTimer(UINT nIDEvent)
    {
        // TODO: Add your message handler code here and/or call default
        unsigned long CurTickValue = GetTickCount();
        unsigned int Difference = CurTickValue - CompTime;
    
        unsigned int ComputerHours, ComputerMinutes, ComputerSeconds;
        unsigned int ApplicationHours, ApplicationMinutes, ApplicationSeconds;
    
        ComputerHours = (CurTickValue / (3600 * 999)) % 24;
        ComputerMinutes = (CurTickValue / (60 * 999)) % 60;
        ComputerSeconds = (CurTickValue / 999) % 60;
        ApplicationHours = (Difference / (3600 * 999)) % 24;
        ApplicationMinutes = (Difference / (60 * 999)) % 60;
        ApplicationSeconds = (Difference / 999) % 60;
    
        m_CompTime.Format("This computer has been ON for %d hours, %d minutes %d seconds",
        		      ComputerHours, ComputerMinutes, ComputerSeconds);
        m_AppTime.Format("This application has been running for %d hours, %d minutes %d seconds",
        		   ApplicationHours, ApplicationMinutes, ApplicationSeconds);
    
        UpdateData(FALSE);
    
        CDialog::OnTimer(nIDEvent);
    }
  15. Test the application:
     
  16. After testing the application, close it
 

 

 

Copyright © 2003-2015, FunctionX, Inc.