Click here to Skip to main content
11,578,524 members (60,710 online)
Click here to Skip to main content

Progress Control with Text

, 26 Feb 2007 CPOL 189.3K 8K 135
Rate this:
Please Sign up or sign in to vote.
A smooth progress control with text

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.

License

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

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
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.

You may also be interested in...

Comments and Discussions

 
QuestionThanks you Pin
futurejo28-Jan-15 21:38
memberfuturejo28-Jan-15 21:38 
Questionprogress bar Pin
Member 1127250228-Nov-14 20:48
memberMember 1127250228-Nov-14 20:48 
QuestionExactly what I needed! Pin
lyuabe27-Mar-14 9:00
memberlyuabe27-Mar-14 9:00 
QuestionWork well with MFC 2010 Pin
spyhunter883-Jul-12 5:36
memberspyhunter883-Jul-12 5:36 
GeneralWon't compile in VS2010 Pin
cc.caprani18-Apr-11 15:49
membercc.caprani18-Apr-11 15:49 
GeneralThanks a lot. Pin
RichyMong15-Sep-09 22:26
memberRichyMong15-Sep-09 22:26 
Generalthanks Pin
jc kamaraj28-Jul-08 1:04
memberjc kamaraj28-Jul-08 1:04 
GeneralForce redraw after an overlapped dialog is closed Pin
Rivaldi12-May-08 6:18
memberRivaldi12-May-08 6:18 
GeneralAdded modified Marquee style Pin
sirfs4-Apr-08 4:22
membersirfs4-Apr-08 4:22 
GeneralAdding SetShowTimeRemaining feature [modified] Pin
ochoteau28-Aug-07 6:11
memberochoteau28-Aug-07 6:11 
Big Grin | :-D this code below add "Time Remaining" to TextProgressCtrl !!!

in .h you have to add :

inline BOOL SetShowTimeRemaining(BOOL bShow)
{ ASSERT(::IsWindow(m_hWnd)); m_dwTimer=GetTickCount(); return ((BOOL) ::SendMessage(m_hWnd, PBM_SETSHOWTIMEREMAINING, (WPARAM)bShow, 0)); }
...
afx_msg LRESULT OnSetShowTimeRemaining(WPARAM bShow, LPARAM);
...
DWORD m_dwTimer;
BOOL m_bShowTimeRemaining;


in .cpp you have to add :

m_dwTimer=GetTickCount();
m_bShowTimeRemaining = TRUE;
...
ON_MESSAGE(PBM_SETSHOWTIMEREMAINING, OnSetShowTimeRemaining)

inline CString ToHMS(DWORD dwTime)
{
CString str;
int iHours=dwTime/3600;
str.Format("%d:%02d:%02d",iHours,dwTime/60-iHours*60,dwTime%60);
return str;
}

LRESULT CTextProgressCtrl::OnSetShowTimeRemaining(WPARAM bShow, LPARAM)
{
// call a common routine to redraw window
CommonPaint();

// set new percent completed display state, returning old one
BOOL bOldShow = m_bShowTimeRemaining;
m_bShowTimeRemaining = (BOOL)bShow;
return ((LRESULT)bOldShow);
}


in OnPaint() method, below if(m_bshowPercent) str.AppendFormat(_T("%d%%"), (int)((dFraction * 100.0) + 0.5));

if (m_bShowTimeRemaining)
{
if(m_nPos>0)
{
DWORD dwElapsed = (GetTickCount()-m_dwTimer)/1000; // Time Elapsed
DWORD dwRemaining = (dwElapsed*m_nMax/m_nPos)-dwElapsed; // Time Remaining
str.AppendFormat(_T(" - EstimatedTimeRemaining: %s"),ToHMS(dwRemaining));
}
}


Regards, OCH


-- modified at 7:12 Wednesday 29th August, 2007
QuestionWindows Constant Values Pin
Amr Elsehemy®22-Jun-07 5:51
memberAmr Elsehemy®22-Jun-07 5:51 
QuestionClass CMemDC differs from it's original version Pin
IvoryBlack21-May-07 5:09
memberIvoryBlack21-May-07 5:09 
AnswerRe: Class CMemDC differs from it's original version Pin
Chris Maunder21-May-07 5:53
adminChris Maunder21-May-07 5:53 
GeneralSetMarquee Pin
Robert Pickford24-Jan-07 4:12
memberRobert Pickford24-Jan-07 4:12 
GeneralTYPO? Or am I missing something Pin
dev7504015-Oct-06 18:15
memberdev7504015-Oct-06 18:15 
GeneralRe: TYPO? Or am I missing something Pin
Tony Bommarito23-Feb-07 5:42
memberTony Bommarito23-Feb-07 5:42 
GeneralDemo project not updated Pin
walvdlz26-Mar-06 18:33
memberwalvdlz26-Mar-06 18:33 
GeneralRe: Demo project not updated Pin
Chris Maunder13-Apr-06 11:26
adminChris Maunder13-Apr-06 11:26 
GeneralRe: Demo project not updated Pin
walvdlz23-Apr-06 19:47
memberwalvdlz23-Apr-06 19:47 
GeneralExtTextOut Pin
.dan.g.9-Mar-06 11:38
member.dan.g.9-Mar-06 11:38 
GeneralRe: ExtTextOut Pin
Chris Maunder10-Mar-06 12:51
adminChris Maunder10-Mar-06 12:51 
GeneralRe: ExtTextOut Pin
Tony Bommarito13-Mar-06 2:52
memberTony Bommarito13-Mar-06 2:52 
GeneralGreat Pin
lianspmark3-Nov-05 15:02
memberlianspmark3-Nov-05 15:02 
GeneralXP Theme Support Pin
Justin Hallet12-Nov-04 4:30
memberJustin Hallet12-Nov-04 4:30 
GeneralRe: XP Theme Support for you Pin
Hidde Wallaart17-Jun-05 4:28
memberHidde Wallaart17-Jun-05 4:28 
GeneralRe: XP Theme Support for you Pin
rrrado8-Feb-06 2:58
memberrrrado8-Feb-06 2:58 
GeneralProblem In SetRange32(0, 0) Pin
Roderick25-Jan-04 23:47
memberRoderick25-Jan-04 23:47 
GeneralRe: Problem In SetRange32(0, 0) Pin
sgllama4-Jan-07 2:03
membersgllama4-Jan-07 2:03 
GeneralDoesn't compile in VC7 (.Net) Pin
JoeB28-Jul-03 13:56
sussJoeB28-Jul-03 13:56 
GeneralRe: Doesn't compile in VC7 (.Net) Pin
Hidde Wallaart7-Aug-03 1:32
memberHidde Wallaart7-Aug-03 1:32 
GeneralRe: Doesn't compile in VC7 (.Net) Pin
J.G. Hattingh27-Aug-03 6:11
memberJ.G. Hattingh27-Aug-03 6:11 
GeneralRe: Doesn't compile in VC7 (.Net) Pin
tamer130-Nov-11 2:50
membertamer130-Nov-11 2:50 
GeneralNice :-) Pin
Brian Delahunty9-May-03 5:55
memberBrian Delahunty9-May-03 5:55 
GeneralGreat! It works! Pin
Nuehli16-Apr-03 4:44
memberNuehli16-Apr-03 4:44 
GeneralFont change problem... Pin
Patrick N24-Mar-03 22:59
memberPatrick N24-Mar-03 22:59 
GeneralRe: Font change problem... Pin
PJ Arends21-Apr-03 17:23
memberPJ Arends21-Apr-03 17:23 
GeneralVery useful! Pin
Hidde Wallaart24-Jan-02 0:05
memberHidde Wallaart24-Jan-02 0: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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 26 Feb 2007
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid