Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / MFC

Showing progress bar in a status bar pane

Rate me:
Please Sign up or sign in to vote.
4.88/5 (54 votes)
23 Aug 2010CPOL2 min read 591.7K   14.6K   194   66
An easy way to add a progress control to a status bar

Sample Image

Adding a CProgressCtrl to the status bar has already been addressed by Brad Mann. His method involved modifying the status bar and messing around with the resource editor. I developed a separate CProgressBar class in order to allow the programmer to just drop in a progress bar whereever they wanted using a single "CProgressBar Bar(...)" declaration, which would initialise and display itself and clean up after itself after it was done. The progress bar can also be created once (say as a member variable) and reused multiple times. This new version of the progress bar also resizes itself if the status bar size changes.

The progress bar lets you specify a message (displayed to the left of the bar) and a Progress Control bar in the any pane of your applications status bar (if it has one - thanks to Patty You for a bug fix on that one). The message for the progress bar can be changed at any time, as can the size and range of the bar.

Construction

C++
CProgressBar(); 
CProgressBar(LPCTSTR strMessage, int nSize=100, int MaxValue=100, BOOL bSmooth=FALSE, int nPane=0);
BOOL Create(LPCTSTR strMessage, int nSize=100, int MaxValue=100, BOOL bSmooth=FALSE, int nPane=0);

Construction is either via the constructor or a two-step process using the constructor and the "Create" function. "strMessage" is the message to be displayed, "nSize" is the percentage width of the status bar that will be occupied by the bar (including the text), and "MaxValue" is the maximum range of the bar.

"bSmooth" will only be effective if you have the header files and commctrl32.dll from IE 3.0 or above (no problems for MS VC 5.0). It specifies whether the progress bar will be smooth or chunky.

Operations

C++
BOOL Success()                      // Construction successful?

COLORREF SetBarColour(COLORREF clrBar);  // Set Bar colour, returns previous
COLORREF SetBkColour(COLORREF clrBar);   // Set background colour, returns previous

int  SetPos(int nPos);              // Same as CProgressCtrl
int  OffsetPos(int nPos);           // Same as CProgressCtrl
int  SetStep(int nStep);            // Same as CProgressCtrl
int  StepIt();                      // Same as CProgressCtrl
void Clear();                       // Clear the status bar
void SetRange(int nLower, int nUpper, int nStep = 1);
                                    // Set min, max and step size
void SetText(LPCTSTR strMessage);   // Set the message
void SetSize(int nSize);            // Set the bar size

To use the bar, just do something like:

C++
CProgressBar Bar("Testing", 40, 1000);

for (int i = 0;  i <  1000; i++)
{
    // perform operation
    Bar.StepIt();
}
or it can be done two stage as:
C++
CProgressBar bar;

bar.Create("Processing", 40, 1000);
for (int i = 0; i < 1000; i++)
{
    //    perform operation
    bar.StepIt();
}
bar.SetText("Writing");
for  (int i = 0;  i <  1000; i++)
{
    // perform operation
    bar.StepIt();
    PeekAndPump();    // Message pump
}
bar.Clear();

In the above case, PeekAndPump() is a function which simply peeks and pumps messages, allowing user interaction with the window during a lengthy process. If the window size changes during the processing, the progress bar size will alsow change.

This article was updated by Michael Martin (allowing the progress bar can now be placed in any pane of the status bar). A further update by YoSilver has fixed an issue that occurs in multithreaded apps. Yet Another Update by Alex Paterson allows you to specify which statusbar you wish the bar to appear in.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
QuestionWhy complicate ? Pin
Member 1116586715-May-16 12:54
Member 1116586715-May-16 12:54 
AnswerRe: Why complicate ? Pin
Chris Maunder15-May-16 14:20
cofounderChris Maunder15-May-16 14:20 
QuestionProgress Bar in Web Application Pin
akjha1617-Sep-14 0:33
akjha1617-Sep-14 0:33 
QuestionCan't extract the demo Pin
Ron Anders23-Jan-13 17:54
Ron Anders23-Jan-13 17:54 
QuestionCorrelating "range" with time? Pin
Vaclav_24-May-12 5:38
Vaclav_24-May-12 5:38 
QuestionAwesome Pin
Andy Bantly11-Mar-12 9:48
Andy Bantly11-Mar-12 9:48 
QuestionIt doesn't work with SetCapture() Pin
Plamen Petrov29-Dec-11 21:10
professionalPlamen Petrov29-Dec-11 21:10 
I want to update the status bar during dragging a CRectTracker, but it captures the mouse by SetCapture(). Until now I cant't find how to dynamicaly refresh the status bar text - the new text appears after releasing the capture.
Any ideas?
GeneralMy vote of 5 Pin
Mei You Qian18-Aug-11 6:23
professionalMei You Qian18-Aug-11 6:23 
GeneralMy vote of 5 Pin
koothkeeper20-Apr-11 4:19
professionalkoothkeeper20-Apr-11 4:19 
GeneralMy vote of 5 Pin
wud97181d30-Dec-10 15:31
wud97181d30-Dec-10 15:31 
GeneralAwesome! Pin
Ernest Laurentin28-Aug-10 10:51
Ernest Laurentin28-Aug-10 10:51 
GeneralDirectives/libraries Pin
wmedwardc21-Sep-09 20:42
wmedwardc21-Sep-09 20:42 
QuestionVC6? Pin
basementman15-Jul-09 10:56
basementman15-Jul-09 10:56 
GeneralThanks! Pin
ccaprani4-Aug-08 9:01
ccaprani4-Aug-08 9:01 
GeneralThank you Pin
Sundevil2-Apr-08 8:30
Sundevil2-Apr-08 8:30 
GeneralI want to use your progress bar to my directx program Pin
akira3228-Jul-07 20:13
akira3228-Jul-07 20:13 
GeneralThanks for showing me how to set the colour Pin
Owen Lawrence21-Mar-07 10:16
Owen Lawrence21-Mar-07 10:16 
GeneralRe: Thanks for showing me how to set the colour Pin
chenjieming9-Jul-10 23:03
chenjieming9-Jul-10 23:03 
NewsSerious bug! Pin
YoSilver4-Mar-07 5:06
YoSilver4-Mar-07 5:06 
GeneralRe: Serious bug! Pin
Joseph Mccornell7-Feb-08 22:47
Joseph Mccornell7-Feb-08 22:47 
GeneralRe: Serious bug! Pin
Damir Valiulin13-Jul-09 10:20
Damir Valiulin13-Jul-09 10:20 
GeneralRe: Serious bug! Pin
Chris Maunder15-Jul-09 4:16
cofounderChris Maunder15-Jul-09 4:16 
GeneralRe: Serious bug! Pin
Damir Valiulin15-Jul-09 7:37
Damir Valiulin15-Jul-09 7:37 
GeneralAbout InterAction Pin
wzh1983122110-Dec-06 15:37
wzh1983122110-Dec-06 15:37 
Generalint GetSize() Pin
mimosa30-Apr-06 12:18
mimosa30-Apr-06 12:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.