Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C++

Progress Control with Text

Rate me:
Please Sign up or sign in to vote.
4.89/5 (50 votes)
26 Feb 2007CPOL4 min read 281.5K   12.4K   143   46
A smooth progress control with text

Image 1

This is a simple CProgressCtrl derived class that allows text to be displayed on top of the progress bar in much the same way as many "Setup" programs display the progress of long operations.

The control is extremely simple and allows the same operations as a standard CProgressCtrl, as well as:

 

 

void SetShowText(BOOL bShow); Specifies whether or not the text for the control will be displayed during updates
COLORREF SetBarColor(COLORREF crBarClr = CLR_DEFAULT); Specifies the colour of the progress control bar, and returns the previous colour. If the colour is set as CLR_DEFAULT then the Windows default colour is used.
COLORREF GetBarColor(); Returns the colour of the progress control bar, or CLR_DEFAULT if the default Windows colours are being used.
COLORREF SetBarBkColor(COLORREF crBarClr = CLR_DEFAULT); Specifies the colour for the control's background, and returns the previous background colour. If the colour is set as CLR_DEFAULT then the Windows default colour is used.
COLORREF GetBarBkColor(); Returns the colour of the control's background, or CLR_DEFAULT if the default Windows colours are being used.
COLORREF SetTextColor(COLORREF crBarClr = CLR_DEFAULT); Specifies the colour of the text, and returns the previous colour. If the colour is set as CLR_DEFAULT then the Windows default colour is used.
COLORREF GetTextColor(); Returns the colour of the text, or CLR_DEFAULT if the default Windows colours are being used.
COLORREF SetTextBkColor(COLORREF crBarClr = CLR_DEFAULT); Specifies the background colour of the text, and returns the previous background colour. If the colour is set as CLR_DEFAULT then the Windows default colour is used.
COLORREF GetTextBkColor(); Returns the background colour of the text, or CLR_DEFAULT if the default Windows colours are being used.
BOOL SetShowPercent(BOOL bShow) Sets whether or not to show the percentage value of the bar, and returns the old value
DWORD AlignText(DWORD dwAlignment = DT_CENTER) Sets the text alignment and returns the old value
BOOL SetMarquee(BOOL bOn, UINT uMsecBetweenUpdate) Sets whether or not the progress control is in marquee mode, as well as the interval in milliseconds between steps of the marquee block
int SetMarqueeOptions(int nBarSize) Sets the size of the marquee as a percentage of the total control width

 

 

To set the text to be displayed use the standard CWnd::SetWindowText. If you call SetShowText(TRUE) but do not specify any window text using CWnd::SetWindowText, then the percentage fraction of progress will be displayed as default.

To use the control, just include a CProgressCtrl in your app as per usual (either dynamically or by using a dialog template) and change the variable type from CProgressCtrl to CTextProgressCtrl. (Make sure you include TextProgressCtrl.h)

At present the progress is only displayed as a smooth bar, and colours are strictly windows default colours. (These may be changed in the future versions.)

Acknowledgements
Thanks to Keith Rule for his CMemDC class.

Updates

Pete Arends went to town on the code. His updates:

  1. Set the font used to the parent window font
  2. Added SetTextColour() and GetTextColour() functions
  3. Added a bunch of message handlers, so the control now responds to the standard progress bar PBM_* messages. It is now possible to control the text progress control by sending messages to it. (works great from a worker thread).
  4. Added 2 new messages PBM_SETSHOWTEXT and PBM_SETTEXTCOLOR.
  5. Added an OnGetPos() handler. The function GetPos() now works!!

Thanks Pete!

3 Jan 05: Kriz has also extended the code with two basic methods that allow switching between the three alignment styles LEFT, CENTER and RIGHT - even on the fly if that's needed.

Changes to the code

  • Created a protected DWORD member m_dwTextStyle
  • Disabled all dwTextStyle variables in OnPaint or replaced them by
    m_dwTextStyle
  • Added two methods called AlignText and AlignTextInvalidate

Syntax

BOOL AlignText(DWORD aligment = DT_CENTER);
BOOL AlignTextInvalidate(DWORD aligment = DT_CENTER);

Params/Return:

"alignment" can be DT_LEFT, DT_CENTER (default) or DT_RIGHT. Using the invalidating version forces the control to repaint itself automatically. Both methods will return FALSE if a wrong alignment flag was given, otherwise they will return TRUE to the caller.

Thank you Kriz!

I've also updated the code so it compiles in VC7.X.

9 mar 06: Tony Bommarito has updated the code to include more settings for colours, made corrections to vertical text mode and added a new marquee mode.

26 Feb 07: Tony Mommarito has provided further updates:

  • Fixed bug where outside edge of progress bar wasn't drawn on XP when using an application manifest.
  • Added complete turn-off of marquee mode in a slightly different manner than that suggested in Robert Pickford message.
  • Fixed Unicode compile bug reported in dev75040 message.
  • Added three SLN and VCPROJ file sets; one each for VS2002, VS2003 and VS2005. By removing the VS… extension from the proper set, any of the three IDEs can be used.

18 Dec 2023: UrbanBlues migrated the project to VS 2022

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

 
QuestionСпасибо за прекрасный прогресс бар Pin
Sapfir21-Jun-20 4:51
Sapfir21-Jun-20 4:51 
Questionbar color yellow is hiding text.any idea why? Pin
naeem_fast129-Jan-18 1:25
naeem_fast129-Jan-18 1:25 
QuestionWon't compile in VS2013 Pin
Member 1116586716-May-16 10:55
Member 1116586716-May-16 10:55 
SuggestionRe: Won't compile in VS2013 Pin
David Crow18-Dec-20 16:56
David Crow18-Dec-20 16:56 
Questionthanks you, and ~ Pin
chodadoo24-Feb-16 23:34
chodadoo24-Feb-16 23:34 
SuggestionGreat Work but few modification needed in visual studio 2013/15 Pin
Risger30-Dec-15 23:00
Risger30-Dec-15 23:00 
GeneralRe: Great Work but few modification needed in visual studio 2013/15 Pin
Ronald Wagner22-Nov-19 7:51
Ronald Wagner22-Nov-19 7:51 
PraiseGreat work, it what i need Pin
Jason.LYJ20-Nov-15 16:21
professionalJason.LYJ20-Nov-15 16:21 
QuestionThanks you Pin
futurejo28-Jan-15 21:38
futurejo28-Jan-15 21:38 
Questionprogress bar Pin
Member 1127250228-Nov-14 20:48
Member 1127250228-Nov-14 20:48 
QuestionExactly what I needed! Pin
lyuabe27-Mar-14 9:00
lyuabe27-Mar-14 9:00 
QuestionWork well with MFC 2010 Pin
spyhunter883-Jul-12 5:36
spyhunter883-Jul-12 5:36 
GeneralWon't compile in VS2010 PinPopular
cc.caprani18-Apr-11 15:49
cc.caprani18-Apr-11 15:49 
GeneralThanks a lot. Pin
RichyMong15-Sep-09 22:26
RichyMong15-Sep-09 22:26 
Generalthanks Pin
jc kamaraj28-Jul-08 1:04
jc kamaraj28-Jul-08 1:04 
GeneralForce redraw after an overlapped dialog is closed Pin
Rivaldi12-May-08 6:18
Rivaldi12-May-08 6:18 
GeneralAdded modified Marquee style Pin
sirfs4-Apr-08 4:22
sirfs4-Apr-08 4:22 
GeneralAdding SetShowTimeRemaining feature [modified] Pin
ochoteau28-Aug-07 6:11
ochoteau28-Aug-07 6:11 
QuestionWindows Constant Values Pin
Amr Elsehemy ®22-Jun-07 5:51
Amr Elsehemy ®22-Jun-07 5:51 
QuestionClass CMemDC differs from it's original version Pin
IvoryBlack21-May-07 5:09
IvoryBlack21-May-07 5:09 
AnswerRe: Class CMemDC differs from it's original version Pin
Chris Maunder21-May-07 5:53
cofounderChris Maunder21-May-07 5:53 
GeneralSetMarquee Pin
Robert Pickford24-Jan-07 4:12
Robert Pickford24-Jan-07 4:12 
GeneralTYPO? Or am I missing something Pin
dev7504015-Oct-06 18:15
dev7504015-Oct-06 18:15 
GeneralRe: TYPO? Or am I missing something Pin
Tony Bommarito23-Feb-07 5:42
Tony Bommarito23-Feb-07 5:42 
GeneralDemo project not updated Pin
walvdlz26-Mar-06 18:33
walvdlz26-Mar-06 18:33 

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.