Click here to Skip to main content
Licence CPOL
First Posted 20 Nov 1999
Views 274,093
Downloads 9,468
Bookmarked 170 times

Showing progress bar in a status bar pane

By | 23 Aug 2010 | Article
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

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

    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:

    CProgressBar Bar("Testing", 40, 1000);
    
    for (int i = 0;  i <  1000; i++) 
    {
        // perform operation
        Bar.StepIt();
    }
or it can be done two stage as:
    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)

About the Author

Chris Maunder

Founder
The Code Project
Canada Canada

Member

Follow on Twitter Follow on Twitter
Google+
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCorrelating "range" with time? PinmemberVaclav_Sal5:38 24 May '12  
QuestionAwesome PinmemberAndy Bantly9:48 11 Mar '12  
QuestionIt doesn't work with SetCapture() PinmemberPlamen Petrov21:10 29 Dec '11  
GeneralMy vote of 5 PinmemberMei You Qian6:23 18 Aug '11  
GeneralMy vote of 5 Pinmemberkoothkeeper4:19 20 Apr '11  
GeneralMy vote of 5 Pinmemberwud97181d15:31 30 Dec '10  
GeneralAwesome! PinmemberErnest Laurentin10:51 28 Aug '10  
GeneralDirectives/libraries Pinmemberwmedwardc20:42 21 Sep '09  
QuestionVC6? Pinmemberbasementman10:56 15 Jul '09  
GeneralThanks! Pinmemberccaprani9:01 4 Aug '08  
GeneralThank you PinmemberMember 30926888:30 2 Apr '08  
GeneralI want to use your progress bar to my directx program Pinmemberakira3220:13 28 Jul '07  
GeneralThanks for showing me how to set the colour PinmemberOwen Lawrence10:16 21 Mar '07  
GeneralRe: Thanks for showing me how to set the colour Pinmemberchenjieming23:03 9 Jul '10  
NewsSerious bug! PinmemberYoSilver5:06 4 Mar '07  
GeneralRe: Serious bug! PinmemberJoseph Mccornell22:47 7 Feb '08  
GeneralRe: Serious bug! PinmemberDamir Valiulin10:20 13 Jul '09  
GeneralRe: Serious bug! PinadminChris Maunder4:16 15 Jul '09  
GeneralRe: Serious bug! PinmemberDamir Valiulin7:37 15 Jul '09  
GeneralAbout InterAction Pinmemberwzh1983122115:37 10 Dec '06  
Generalint GetSize() Pinmembermimosa12:18 30 Apr '06  
QuestionAdding snake feature to the control ? Pinmemberjongallegraud22:03 19 Mar '06  
GeneralDoesn't seem to work for dialog status bar PinmemberTobyNorris4:56 2 Feb '06  
GeneralThanks PinmemberDavid.Kelly8:08 7 Nov '05  
GeneralRe: Thanks PinadminChris Maunder8:09 7 Nov '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 23 Aug 2010
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid