- Start a new MFC Application named TickCounter
- Change the design of the IDR_MAINFRAME icon to something different
- Create it as Dialog Based
Here is how we will design the dialog box:
- Add a Group Box control to the dialog box and set its Caption to Elapsed Time
- 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
- 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
- Add a Value Variable for the IDC_COMP_TIME identifier and name it m_CompTime
- Add a Value Variable for the IDC_COMP_TIME identifier and name it m_AppTime
- In the header file of the dialog box, declare an unsigned integer as follows:
public:
CString m_CompTime;
CString m_AppTime;
unsigned int CompTime;
};
|
- 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
}
|
- 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);
}
|
- Test the application
- After testing the application, close it and return to MSVC
- 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);
}
|
- Test the application:
- After testing the application, close it
|