Chapter 18: Progress-Based Controls Visual C++ and MFC Fundamentals
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);