|
We mentioned already, an MFC progress bar is based on the
CProgressCtrl class. Therefore, to dynamically create a progress bar, you can declare a variable or a pointer to
CProgressCtrl using its constructor. To initialize it, call its Create() method and set the necessary characteristics. Here is an example:
BOOL CDlgProgress1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CProgressCtrl *Progress = new CProgressCtrl;
Progress->Create(WS_CHILD | WS_VISIBLE, CRect(10, 10, 288, 35), this,0x16);
return TRUE; // return TRUE unless you set the focus to a control
}
To express its evolution, a progress bar uses values from a minimum to a maximum. These limit values are set using the
CProgressCtrl::SetRange() or the CProgressCtrl::SetRange32() methods. Their syntaxes are:
void SetRange(short nLower, short nUpper);
void SetRange32(int nLower, int nUpper);
The nLower argument sets the minimum value for the control. The nUpper argument is the maximum value of the control. Here is an example:
BOOL CDlgProgress1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CProgressCtrl *Progress = new CProgressCtrl;
Progress->Create(WS_CHILD | WS_VISIBLE, CRect(10, 10, 288, 35), this,0x16);
Progress->SetRange(1, 100);
return TRUE; // return TRUE unless you set the focus to a control
}
If the range of values has already been set and you want to find it out, you can call the
CProgressCtrl::GetRange() method. Its syntax is:
void GetRange(int& nLower, int& nUpper);
This member function returns two values: nLower is the current minimum value of the control while
nUpper is returned as its maximum value.
Once a progress control has been created and when it comes up, it assumes its minimum value. While the control is working, it keeps changing its value by stepping to the next value. By default, its next value is set to 10. If you want to specify a different incremental value, call the
CProgressCtrl::SetStep() method whose syntax is:
int SetStep(int nStep);
The nStep argument is the value set to increment the position of the progress control. Once this value is set, when the control is progressing, it increments its value by that value to get to the next step. If you want to explicitly ask it to step to the next incremental value, call the CProgressCtrl::StepIt() method. Its syntax is:
int StepIt( );
Although the user cannot directly modify it, you can change the value of a progress bar by calling the
CProgressCtrl::SetPos() method whose syntax is:
int SetPos(int nPos);
This method can be called either to set the initial value of the control or to simply change it at any time. Here is an example:
BOOL CDlgProgress1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CProgressCtrl *Progress = new CProgressCtrl;
Progress->Create(WS_CHILD | WS_VISIBLE, CRect(10, 10, 288, 35), this,0x16);
Progress->SetRange(1, 100);
Progress->SetPos(38);
return TRUE; // return TRUE unless you set the focus to a control
}
While the control is working, if you want to find out the value it is holding, you can call the
CProgressCtrl::GetPos() method. Its syntax is:
int GetPos( );
This method returns the current position of the progress control.
|