CProgressCtrl Class

Provides the functionality of the Windows common progress bar control.

Syntax

class CProgressCtrl : public CWnd

Members

Public Constructors

Name Description
CProgressCtrl::CProgressCtrl Constructs a CProgressCtrl object.

Public Methods

Name Description
CProgressCtrl::Create Creates a progress bar control and attaches it to a CProgressCtrl object.
CProgressCtrl::CreateEx Creates a progress control with the specified Windows extended styles and attaches it to a CProgressCtrl object.
CProgressCtrl::GetBarColor Gets the color of the progress indicator bar for the current progress bar control.
CProgressCtrl::GetBkColor Gets the background color of the current progress bar.
CProgressCtrl::GetPos Gets the current position of the progress bar.
CProgressCtrl::GetRange Gets the lower and upper limits of the range of the progress bar control.
CProgressCtrl::GetState Gets the state of the current progress bar control.
CProgressCtrl::GetStep Retrieves the step increment for the progress bar of the current progress bar control.
CProgressCtrl::OffsetPos Advances the current position of a progress bar control by a specified increment and redraws the bar to reflect the new position.
CProgressCtrl::SetBarColor Sets the color of the progress indicator bar in the current progress bar control.
CProgressCtrl::SetBkColor Sets the background color for the progress bar.
CProgressCtrl::SetMarquee Turns marquee mode on or off for the current progress bar control.
CProgressCtrl::SetPos Sets the current position for a progress bar control and redraws the bar to reflect the new position.
CProgressCtrl::SetRange Sets the minimum and maximum ranges for a progress bar control and redraws the bar to reflect the new ranges.
CProgressCtrl::SetState Sets the state of the current progress bar control.
CProgressCtrl::SetStep Specifies the step increment for a progress bar control.
CProgressCtrl::StepIt Advances the current position for a progress bar control by the step increment (see SetStep) and redraws the bar to reflect the new position.

Remarks

A progress bar control is a window that an application can use to indicate the progress of a lengthy operation. It consists of a rectangle that is gradually filled, from left to right, with the system highlight color as an operation progresses.

A progress bar control has a range and a current position. The range represents the total duration of the operation, and the current position represents the progress the application has made toward completing the operation. The window procedure uses the range and the current position to determine the percentage of the progress bar to fill with the highlight color. Because the range and current position values are expressed as signed integers, the possible range of current position values is from -2,147,483,648 to 2,147,483,647 inclusive.

For more information on using CProgressCtrl, see Controls and Using CProgressCtrl.

Inheritance Hierarchy

CObject

CCmdTarget

CWnd

CProgressCtrl

Requirements

Header: afxcmn.h

CProgressCtrl::CProgressCtrl

Constructs a CProgressCtrl object.

CProgressCtrl();

Remarks

After constructing the CProgressCtrl object, call CProgressCtrl::Create to create the progress bar control.

Example

// Create a progress control object on the stack.
CProgressCtrl myCtrl;

// Create a progress control object on the heap.
CProgressCtrl* pmyCtrl = new CProgressCtrl;   

CProgressCtrl::Create

Creates a progress bar control and attaches it to a CProgressCtrl object.

virtual BOOL Create(
    DWORD dwStyle,
    const RECT& rect,
    CWnd* pParentWnd,
    UINT nID);

Parameters

dwStyle
Specifies the progress bar control's style. Apply any combination of window stylesdescribed in CreateWindow in the Windows SDK, in addition to the following progress bar control styles, to the control:

  • PBS_VERTICAL Displays progress information vertically, top to bottom. Without this flag, the progress bar control displays horizontally, left to right.

  • PBS_SMOOTH Displays gradual, smooth filling in the progress bar control. Without this flag, the control will fill with blocks.

rect
Specifies the progress bar control's size and position. It can be either a CRect object or a RECT structure. Because the control must be a child window, the specified coordinates are relative to the client area of the pParentWnd.

pParentWnd
Specifies the progress bar control's parent window, usually a CDialog. It must not be NULL.

nID
Specifies the progress bar control's ID.

Return Value

TRUE if the CProgressCtrl object is successfully created; otherwise FALSE.

Remarks

You construct a CProgressCtrl object in two steps. First, call the constructor, which creates the CProgressCtrl object, and then call Create, which creates the progress bar control.

Example

CProgressCtrl myCtrl;

// Create a smooth child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH, CRect(10,10,200,30), 
   pParentWnd, IDC_PROGRESSCTRL);   

CProgressCtrl::CreateEx

Creates a control (a child window) and associates it with the CProgressCtrl object.

virtual BOOL CreateEx(
    DWORD dwExStyle,
    DWORD dwStyle,
    const RECT& rect,
    CWnd* pParentWnd,
    UINT nID);

Parameters

dwExStyle
Specifies the extended style of the control being created. For a list of extended Windows styles, see the dwExStyle parameter for CreateWindowEx in the Windows SDK.

dwStyle
Specifies the progress bar control's style. Apply any combination of window styles described in CreateWindow in the Windows SDK.

rect
A reference to a RECT structure describing the size and position of the window to be created, in client coordinates of pParentWnd.

pParentWnd
A pointer to the window that is the control's parent.

nID
The control's child-window ID.

Return Value

Nonzero if successful; otherwise 0.

Remarks

Use CreateEx instead of Create to apply extended Windows styles, specified by the Windows extended style preface WS_EX_.

CProgressCtrl::GetBarColor

Gets the color of the progress indicator bar for the current progress bar control.

COLORREF GetBarColor() const;

Return Value

The color of the current progress bar, represented as a COLORREF value, or CLR_DEFAULT if the progress indicator bar color is the default color.

Remarks

This method sends the PBM_GETBARCOLOR message, which is described in the Windows SDK.

CProgressCtrl::GetBkColor

Gets the background color of the current progress bar.

COLORREF GetBkColor() const;

Return Value

The background color of the current progress bar, represented as a COLORREF value.

Remarks

This method sends the PBM_GETBKCOLOR message, which is described in the Windows SDK.

CProgressCtrl::GetPos

Retrieves the current position of the progress bar.

int GetPos();

Return Value

The position of the progress bar control.

Remarks

The position of the progress bar control is not the physical location on the screen, but rather is between the upper and lower range indicated in SetRange.

Example

CProgressCtrl myCtrl;

// Create a child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,30), pParentWnd, 
   IDC_PROGRESSCTRL);

// Set the new position to half of the current position.
myCtrl.SetPos(myCtrl.GetPos()/2);   

CProgressCtrl::GetRange

Gets the current lower and upper limits, or range, of the progress bar control.

void GetRange(
    int& nLower,
    int& nUpper);

Parameters

nLower
A reference to an integer receiving the lower limit of the progress bar control.

nUpper
A reference to an integer receiving the upper limit of the progress bar control.

Remarks

This function copies the values of the lower and upper limits to the integers referenced by nLower and nUpper, respectively.

Example

CProgressCtrl myCtrl;

// Create a child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,30), pParentWnd, 
   IDC_PROGRESSCTRL);

// Set the position to be one-fourth of the total range.
int nLower, nUpper;
myCtrl.GetRange(nLower, nUpper);
myCtrl.SetPos((nUpper-nLower)/4);   

CProgressCtrl::GetState

Gets the state of the current progress bar control.

int GetState() const;

Return Value

The state of the current progress bar control, which is one of the following values:

Value State
PBST_NORMAL In progress
PBST_ERROR Error
PBST_PAUSED Paused

Remarks

This method sends the PBM_GETSTATE message, which is described in the Windows SDK.

Example

The following code example defines the variable, m_progressCtrl, that is used to programmatically access the progress bar control. This variable is used in the next example.

// Variable to access the progress control
CProgressCtrl m_progressCtrl;

Example

The following code example retrieves the state of the current progress bar control.

// Display the current state of the progress control.
    CString str = _T("The progress control state is ");
    int progState = m_progressCtrl.GetState();
    if (progState == PBST_NORMAL)
        str += _T("NORMAL");
    else if (progState == PBST_PAUSED)
        str += _T("PAUSED");
    else if (progState == PBST_ERROR)
        str += _T("ERROR");
    else 
        str += _T("unknown");
    AfxMessageBox(str, MB_ICONEXCLAMATION);

CProgressCtrl::GetStep

Retrieves the step increment for the progress bar of the current progress bar control.

int GetStep() const;

Return Value

The step increment of the progress bar.

Remarks

The step increment is the amount by which a call to CProgressCtrl::StepIt increases the current position of the progress bar.

This method sends the PBM_GETSTEP message, which is described in the Windows SDK.

Example

The following code example defines the variable, m_progressCtrl, that is used to programmatically access the progress bar control. This variable is used in the next example.

// Variable to access the progress control
CProgressCtrl m_progressCtrl;

Example

The following code example retrieves the step increment of the current progress bar control.

// Get the step increment for the progress control.
    CString str;
    int incr = m_progressCtrl.GetStep();
    str.Format(_T("The step increment is %d."), incr);
    AfxMessageBox(str, MB_ICONEXCLAMATION);

CProgressCtrl::OffsetPos

Advances the progress bar control's current position by the increment specified by nPos and redraws the bar to reflect the new position.

int OffsetPos(int nPos);

Parameters

nPos
Amount to advance the position.

Return Value

The previous position of the progress bar control.

Example

CProgressCtrl myCtrl;

// Create a child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,30), pParentWnd, 
   IDC_PROGRESSCTRL);

// Offset the position by one-fourth of the total range.
int nLower, nUpper;
myCtrl.GetRange(nLower, nUpper);
myCtrl.OffsetPos((nUpper-nLower)/4);   

CProgressCtrl::SetBarColor

Sets the color of the progress indicator bar in the current progress bar control.

COLORREF SetBarColor(COLORREF clrBar);

Parameters

Parameter Description
clrBar [in] A COLORREF value that specifies the new color of the progress indicator bar. Specify CLR_DEFAULT to cause the progress bar to use its default color.

Return Value

The previous color of the progress indicator bar, represented as a COLORREF value, or CLR_DEFAULT if the color of the progress indicator bar is the default color.

Remarks

The SetBarColor method sets the progress bar color only if a Windows Vista theme is not in effect.

This method sends the PBM_SETBARCOLOR message, which is described in the Windows SDK.

Example

The following code example defines the variable, m_progressCtrl, that is used to programmatically access the progress bar control. This variable is used in the next example.

// Variable to access the progress control
CProgressCtrl m_progressCtrl;

Example

The following code example changes the color of the progress bar to red, green, blue, or the default.

// Set the progress bar color to red, green, blue, or 
// the system default. The SetBarColor method has an 
// effect only if the Windows system theme is Classic.
void CCProgressCtrl_s1Dlg::OnSetbarcolorRed()
{
    m_progressCtrl.SetBarColor(RGB(255,0,0));
}

void CCProgressCtrl_s1Dlg::OnSetbarcolorGreen()
{
    m_progressCtrl.SetBarColor(RGB(0,255,0));
}

void CCProgressCtrl_s1Dlg::OnSetbarcolorBlue()
{
    m_progressCtrl.SetBarColor(RGB(0,0,255));
}

void CCProgressCtrl_s1Dlg::OnSetbarcolorOri()
{
    m_progressCtrl.SetBarColor(CLR_DEFAULT);
}

CProgressCtrl::SetBkColor

Sets the background color for the progress bar.

COLORREF SetBkColor(COLORREF clrNew);

Parameters

clrNew
A COLORREF value that specifies the new background color. Specify the CLR_DEFAULT value to use the default background color for the progress bar.

Return Value

The COLORREF value indicating the previous background color, or CLR_DEFAULT if the background color is the default color.

Example

CProgressCtrl myCtrl;

// Create a smooth child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH, CRect(10,10,200,30), 
   pParentWnd, IDC_PROGRESSCTRL);

// Set the background color to red.
myCtrl.SetBkColor(RGB(255, 0, 0));   

CProgressCtrl::SetMarquee

Turns marquee mode on or off for the current progress bar control.

BOOL SetMarquee(
    BOOL fMarqueeMode,
    int nInterval);

Parameters

Parameter Description
fMarqueeMode [in] TRUE to turn marquee mode on, or FALSE to turn marquee mode off.
nInterval [in] Time in milliseconds between updates of the marquee animation.

Return Value

This method always returns TRUE.

Remarks

When marquee mode is turned on, the progress bar is animated and scrolls like a sign on a theater marquee.

This method sends the PBM_SETMARQUEE message, which is described in the Windows SDK.

Example

The following code example defines the variable, m_progressCtrl, that is used to programmatically access the progress bar control. This variable is used in the next example.

// Variable to access the progress control
CProgressCtrl m_progressCtrl;

Example

The following code example starts and stops the marquee scrolling animation.

// Turn the marquee animation on or off.
void CCProgressCtrl_s1Dlg::OnSetmarqueeOn()
{
    m_progressCtrl.SetMarquee(TRUE, nMarqueeInterval);
}

void CCProgressCtrl_s1Dlg::OnSetmarqueeOff()
{
    m_progressCtrl.SetMarquee(FALSE, nMarqueeInterval);
}

CProgressCtrl::SetPos

Sets the progress bar control's current position as specified by nPos and redraws the bar to reflect the new position.

int SetPos(int nPos);

Parameters

nPos
New position of the progress bar control.

Return Value

The previous position of the progress bar control.

Remarks

The position of the progress bar control is not the physical location on the screen, but rather is between the upper and lower range indicated in SetRange.

Example

CProgressCtrl myCtrl;

// Create a child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,30), pParentWnd, 
   IDC_PROGRESSCTRL);

// Set the range to be 0 to 100.
myCtrl.SetRange(0, 100);

// Set the position to be half, 50.
myCtrl.SetPos(50);   

CProgressCtrl::SetRange

Sets the upper and lower limits of the progress bar control's range and redraws the bar to reflect the new ranges.

void SetRange(
    short nLower,
    short nUpper);

void SetRange32(
    int nLower,
    int nUpper);

Parameters

nLower
Specifies the lower limit of the range (default is zero).

nUpper
Specifies the upper limit of the range (default is 100).

Remarks

The member function SetRange32 sets the 32-bit range for the progress control.

Example

CProgressCtrl myCtrl;

// Create a smooth child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH, CRect(10,10,200,30), 
   pParentWnd, IDC_PROGRESSCTRL);

// Set the range to be 0 to 100.
myCtrl.SetRange(0, 100);   

CProgressCtrl::SetState

Sets the state of the current progress bar control.

int SetState(int iState);

Parameters

Parameter Description
iState [in] The state to set the progress bar. Use one of the following values:

- PBST_NORMAL - In progress
- PBST_ERROR - Error
- PBST_PAUSED - Paused

Return Value

The previous state of the current progress bar control.

Remarks

This method sends the PBM_SETSTATE message, which is described in the Windows SDK.

Example

The following code example defines the variable, m_progressCtrl, that is used to programmatically access the progress bar control. This variable is used in the next example.

// Variable to access the progress control
CProgressCtrl m_progressCtrl;

Example

The following code example sets the state of the current progress bar control to Paused or In Progress.

// Set the progrees control to normal or paused state.
void CCProgressCtrl_s1Dlg::OnSetstateNormal()
{
    m_progressCtrl.SetState(PBST_NORMAL);
}

void CCProgressCtrl_s1Dlg::OnSetstatePaused()
{
    m_progressCtrl.SetState(PBST_PAUSED);
}

CProgressCtrl::SetStep

Specifies the step increment for a progress bar control.

int SetStep(int nStep);

Parameters

nStep
New step increment.

Return Value

The previous step increment.

Remarks

The step increment is the amount by which a call to CProgressCtrl::StepIt increases the progress bar's current position.

The default step increment is 10.

Example

CProgressCtrl myCtrl;

// Create a child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,30), pParentWnd, 
   IDC_PROGRESSCTRL);

// Set the size to be 1/10 of the total range.
int nLower, nUpper;
myCtrl.GetRange(nLower, nUpper);
myCtrl.SetStep((nUpper-nLower)/10);   

CProgressCtrl::StepIt

Advances the current position for a progress bar control by the step increment and redraws the bar to reflect the new position.

int StepIt();

Return Value

The previous position of the progress bar control.

Remarks

The step increment is set by the CProgressCtrl::SetStep member function.

Example

CProgressCtrl myCtrl;

// Create a child progress control.
myCtrl.Create(WS_CHILD|WS_VISIBLE, CRect(10,10,200,30), pParentWnd, 
   IDC_PROGRESSCTRL);

// Advance the position to the next step.
myCtrl.StepIt();   

See Also

MFC Sample CMNCTRL2
CWnd Class
Hierarchy Chart