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()
COLORREF SetBarColour(COLORREF clrBar); COLORREF SetBkColour(COLORREF clrBar);
int SetPos(int nPos); int OffsetPos(int nPos); int SetStep(int nStep); int StepIt(); void Clear(); void SetRange(int nLower, int nUpper, int nStep = 1);
void SetText(LPCTSTR strMessage); void SetSize(int nSize);
To use the bar, just do something like:
CProgressBar Bar("Testing", 40, 1000);
for (int i = 0; i < 1000; i++)
{
Bar.StepIt();
}
or it can be done two stage as:
CProgressBar bar;
bar.Create("Processing", 40, 1000);
for (int i = 0; i < 1000; i++)
{
bar.StepIt();
}
bar.SetText("Writing");
for (int i = 0; i < 1000; i++)
{
bar.StepIt();
PeekAndPump(); }
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.
Chris Maunder is the co-founder of
CodeProject, DeveloperMedia and ContentLab, 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. While at CodeProject, Chris' roles included Architecture and coding, Product Development, Content Creation, Community Growth, Client Satisfaction and Systems Automation, and many, many sales meetings. All while keeping his sense of humour.