Title:
Windows Progress Bar Control
Author:
Philip J. Erdelsky, http://www.efgh.com, pje@efgh.com
Language:
Turbo C/C++ 3.1 for Windows, Borland C++ Builder 1.0 for Windows 95
Platform
Windows 3.1/95/NT
Portability:
Other Windows C++ compilers, with slight changes
Restrictions:
Public domain, no restrictions on use
Date:
November 13, 1997
Keywords:
Windows Custom Control, Progress, Completion
Abstract:
A windows custom control for a progress bar or completion bar, which shows the percentage of completion of a process as a numerical value and also by a partly filled-in bar. C++ source code only; no VBX file.
Source code:
PROGRESS.CPP, PROGRESS.H, PROGTEST.CPP, PROGTEST.H and PROGTEST.RC combined into the file PROGRESS.TXT

The "PROGRESSBAR" control is a simple, common control that does not appear among the Windows standard controls. Its purpose is to show the percentage of completion of a process, both as a numerical value and by a partly filled-in bar. It is a completely passive control which changes only in response to messages sent by its parent.

You can put a "PROGRESSBAR" control into a dialog box by including a line of the following form in the appropriate place in the resource (.RC) file:

     CONTROL 0, control_id, "PROGRESSBAR", WS_CHILD | WS_VISIBLE,
       x, y, width, height

You can also put a "PROGRESSBAR" control into a dialog box with the Borland Resource Workshop, by selecting a custom control and then entering the class name "PROGRESSBAR" instead of choosing one of the predefined classes.

You can also create a "PROGRESSBAR" control dynamically with an appropriate call on CreateWindow().

The control keeps track of two values, which are called "total" and "completed". The default values are 100 and 0, respectively, but they may be changed by sending messages as follows:

     SendDlgItemMessage(handle, control_id, PB_SETTOTAL, value, 0L);
     SendDlgItemMessage(handle, control_id, PB_SETCOMPLETED, value, 0L);

The control displays the following percentage (rounded downward to the nearest integer) in the middle of the bar:

     (completed / total) * 100%

It also darkens the left side of the bar so the width of the darkened portion and the width of the bar are in the same ratio as "completed" is to "total".

The value of "total" must be positive, and the value of "completed" must lie between zero and "total", inclusive. The control will fail if this is not the case.

The values of "total" and "completed" may also be retrieved by sending messages as follows:

     value = SendDlgItemMessage(handle, control_id, PB_GETTOTAL, 0, 0L);
     value = SendDlgItemMessage(handle, control_id, PB_GETCOMPLETED, 0, 0L);

A "PROGRESSBAR" control does not process other messages, except for a few that are handled internally by Windows.

The file PROGRESS.CPP must be compiled and linked with the rest of the application. The special messages PB_SETTOTAL, PB_SETCOMPLETED, PB_GETTOTAL and PB_GETCOMPLETED are defined in the header file PROGRESS.H.

The files PROGTEST.CPP, PROGTEST.H and PROGTEST.RC contain a simple test program uses a "PROGRESSBAR" control.

It is fairly difficult to compile applications as simple as this test program with Borland C++ Builder for Windows. To prevent the automatic inclusion of irrelevant material, the compiler has to be called up by command lines executed in a DOS window, perhaps as follows:

REM Compile and Link Picture Button Test Application
SET BDIR=C:\PROGRA~1\BORLAND\CBUILDER
BCC32 -DWIN32 -I%BDIR%\INCLUDE -L%BDIR%\LIB -W progtest.cpp progress.cpp
BRC32 -32 progtest
SET BDIR=