(i) The main function of the progress bar
Progress bar control is mainly used for data read and write, file copy and disk format, such as the work of the progress of the Progress, such as installation procedures, along with the progress of work progress, the progress bar of the rectangular area from left to right using the current active window title bar color to continuously fill.
The progress bar controls the encapsulation class in the MFC class library as CProgressCtrl, usually only as an output class, so its operation is primarily to set the range and current position of the progress bar and keep updating the current position. The range of the progress bar is used to indicate the length of the entire operation, and the current position represents the current time of completion. The SetRange () function is used to set the scope, the initial range is the 0-100,setpos () function to set the current position, the initial value is the 0,setstep () function to set the step, the initial step is the 10,stepit () function to update the position according to the current step. The OffsetPos () function is used to move the current position directly to a distance. If the range or position changes, the progress bar will automatically redraw the progress area to reflect the progress of the current work.
Object structure of the progress bar
Method of establishing progress bar control
CProgressCtrl &progressctrl Establish progress bar control object structure
Create a progress bar control objects and bind objects
The progress bar control class Cprogressctrl::create call format is as follows:
BOOL Create (DWORD dwstyle, const rect& RECT, cwnd* pParentWnd, UINT NID);
The parameter dwstyle is used to determine the control style of the progress bar control, the parameter rect is used to determine the size and position of the progress bar control, and the parameter pParentWnd determines the progress bar parent window pointer, and the parameter NID the control ID value used to determine the progress bar control.
2, the progress bar control class attribute
The class properties controlled by the progress bar include setting the maximum minimum control range for the progress bar, setting the current position of the progress bar SetPos, setting the current position offset of the progress bar OffsetPos, and setting the progress bar to control the increment value SetStep setrange.
3, the progress bar control operation method
The progress bar control method is mainly to make progress bar control and redraw progress bar stepit function.
Examples of application techniques for progress bar control
1, using the Application Wizard AppWizard generate the object-frame based application Cprogdlg;
2, in the dialog box to set the progress bar and static text control, the ID is Idc_prog and idcpercent respectively;
Increase the scope and location of the control in the initial code of the dialog box:
Set two data members in ProgDlg.h to represent the maximum and step size of the progress bar:
ProgDlg.h
Class Cprogdlg:public Cdialog
{ ...... Other code
Public
int m_nmax,m_nstep;
...... Other code
} |
(2) Setting the initial state in the ProgDlg.cpp
BOOL Cprogdlg::oninitdialog ()
{CDialog::OnInitDialog ();
...... Other code
Todo:add Extra initialization here
CProgressCtrl *pprogctrl= (cprogressctrl*) GetDlgItem (Idc_prog);
Pprogctrl->setrange (0,200);/Set progress bar range
...... Other code
m_nmax=200;
m_nstep=10;
SetTimer (1,1000,null)//Set progress bar Update clock
return TRUE;
} |
(3) Improve the WM_TIMER message processing, so that the progress bar in accordance with the current step of the update, while completing the percentage of progress bar display:
void Cprogdlg::ontimer (UINT nidevent) {//todo:add Your message handler? CProgressCtrl *pprogctrl= (cprogressctrl*) GetDlgItem (Idc_prog); int Nprepos=pprogctrl->stepit ();//before update position Char test[10]; int npercent= (int) ((nprepos+m_nstep)/m_nmax*100+0.5); wsprintf (test,?%d%%?,npercent); GetDlgItem (idc_percent)->setwindowtext (text); Cdialog::ontimer (nidevent); } |