Progress Bar Control


A progress bar 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 with the system highlight color as an operation progresses. The following illustration shows a progress bar positioned along the bottom of a window.

Using Progress Bars

You create a progress bar by using the CreateWindowEx function, specifying the PROGRESS_CLASS window class. This window class is registered when the common control dynamic-link library (DLL) is loaded. To ensure that this DLL is loaded, use the InitCommonControls function first.

Range and Current Position

A progress bar's range represents the entire duration of the operation, and the current position represents the progress that 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 unsigned integers, the highest possible range or current position value is 65,535.

The minimum value in the range can be from 0 to 65,535. Likewise, the maximum value can be from 0 to 65,535. If you do not set the range values, the system sets the minimum value to 0 and the maximum value to 100. You can adjust the range to convenient integers by using the PBM_SETRANGE message.

A progress bar provides several messages that you can use to set the current position. The PBM_SETPOS message sets the position to a given value. The PBM_DELTAPOS message advances the position by adding a specified value to the current position.

The PBM_SETSTEP message allows you to specify a step increment for a progress bar. Subsequently, whenever you send the PBM_STEPIT message to the progress bar, the current position advances by the specified increment. By default, the step increment is set to 10.

Note   You use the Theme API to apply visual styles to applications. If you are using a progress bar control with the Theme API on Microsoft® Windows® XP the control must be at least 10 pixels high.

Default Progress Bar Message Processing

This section describes the messages handled by the window procedure for the PROGRESS_CLASS class.

Message Processing performed
WM_CREATE Allocates and initializes an initial structure.
WM_DESTROY Frees all resources associated with the progress bar.
WM_ERASEBKGND Draws the background and borders of the progress bar.
WM_GETFONT Returns the handle to the current font. The progress bar does not currently draw text, so sending this message has no effect on the control.
WM_PAINT Draws the progress bar. If the wParam parameter is non-NULL, the control assumes that the value is an HDC and paints using that device context.
WM_SETFONT Saves the handle to the new font and returns the handle to the previous font. The progress bar does not currently draw text, so sending this message has no effect on the control.

Progress Bar Example

The following example shows how to use a progress bar to indicate the progress of a lengthy file-parsing operation. The example creates a progress bar and positions it along the bottom of the parent window's client area. The height of the progress bar is based on the height of the arrow bitmap used in a scroll bar. The range is based on the size of the file divided by 2048, which is the size of each "chunk" of data read from the file. The example also sets an increment and advances the current position of the progress bar by the increment after parsing each chunk.

Example

Progress Bar Control Updates in Internet Explorer

Progress bar controls in Microsoft Internet Explorer support the following new features.

New Progress Bar Control Styles Progress bar controls can now display progress information vertically, using the PBS_VERTICAL style. Also, a smooth progress mode is supported using the PBS_SMOOTH style. Using the PBS_SMOOTH style causes the control to display a contiguous progress bar instead of a segmented bar.
Extended Range Values Progress bar controls now support 32-bit range values. To set range values in excess of 65,535, use the PBM_SETRANGE32 message. To retrieve 32-bit range values, use the PBM_GETRANGE message. The progress bar high limit, low limit, and position parameters are signed integers. To make full use of the 32-bit range, set the range to -0x7FFFFFFF to 0x7FFFFFFF and treat the position as a signed integer.
Programmable Colors An application can now control the colors used in a progress bar control with the PBM_SETBARCOLOR and PBM_SETBKCOLOR messages.