Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 18: Progress-Based Controls


Practical Learning: Using a Progress Bar



  1. To specify the initial values of the progress controls, set their range appropriately in
    the OnInitDialog() event. Also, create a timer control


BOOL CPClockDlg::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
m_ProgressHours.SetRange(0, 23);
m_ProgressHours.SetStep(1);
m_ProgressMinutes.SetRange(0, 59);
m_ProgressMinutes.SetStep(1);
m_ProgressSeconds.SetRange(0, 59);
m_ProgressSeconds.SetStep(1);

SetTimer(1, 40, NULL);

return TRUE; // return TRUE unless you set the focus to a control
}


  1. Generate the WM_TIMER message for the dialog box and implement it as follows:


void CPClockDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// Get the current time of the computer
CTime CurTime = CTime::GetCurrentTime();

// Find the hour, the minute, and the second values of the time
int ValHours = CurTime.GetHour();
int ValMinutes = CurTime.GetMinute();
int ValSeconds = CurTime.GetSecond();

// Change each progress bar accordingly
m_ProgressHours.SetPos(ValHours);
m_ProgressMinutes.SetPos(ValMinutes);
m_ProgressSeconds.SetPos(ValSeconds);

// Display the position of the progress in the right label
m_ValueHours.Format("%d", m_ProgressHours.GetPos());
m_ValueMinutes.Format("%d", m_ProgressMinutes.GetPos());
m_ValueSeconds.Format("%d", m_ProgressSeconds.GetPos());
UpdateData(FALSE);

CDialog::OnTimer(nIDEvent);
}


  1. Test the application

Free download pdf