Click here to Skip to main content
15,899,935 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 282.9K   12.4K   144   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

 
GeneralRe: Demo project not updated Pin
Chris Maunder13-Apr-06 11:26
cofounderChris Maunder13-Apr-06 11:26 
GeneralRe: Demo project not updated Pin
walvdlz23-Apr-06 19:47
walvdlz23-Apr-06 19:47 
GeneralExtTextOut Pin
.dan.g.9-Mar-06 11:38
professional.dan.g.9-Mar-06 11:38 
GeneralRe: ExtTextOut Pin
Chris Maunder10-Mar-06 12:51
cofounderChris Maunder10-Mar-06 12:51 
GeneralRe: ExtTextOut Pin
Tony Bommarito13-Mar-06 2:52
Tony Bommarito13-Mar-06 2:52 
GeneralGreat Pin
lianspmark3-Nov-05 15:02
lianspmark3-Nov-05 15:02 
GeneralXP Theme Support Pin
Justin Hallet12-Nov-04 4:30
Justin Hallet12-Nov-04 4:30 
GeneralRe: XP Theme Support for you Pin
Hidde Wallaart17-Jun-05 4:28
Hidde Wallaart17-Jun-05 4:28 
Hello all,

What a pity nobody answered this question because it would have saved me some work. Never mind, I worked out how to do the Theme support myself and will share it with you. Five easy steps:

1) Study the classes CXPStyleButtonST and CButtonST very carefully. What you will find is that the OnPaint() function in ButtonST calls a virtual function OnDrawBackground() that does the actual drawing. In CXPStyleButtonST this function is overwritten to do the themed drawing. The themed drawing uses a helper class called CThemeHelperST that tabs into the windows theme DLL.

2) In order to support a themed progress bar we will create a new class, CXPStyleProgress that derives from CTextProgressCtrl.

3) In CTextProgressCtrl, split up the OnPaint() so that the actual owner-draw painting is done in

virtual void DrawProgressBar(CDC* pDC, CRect* pRect, double dFraction);

4) Add Theme support to CXPStyleProgress. The header file will include:

#include "TextProgressCtrl.h"<br />
#include "ThemeHelperST.h"


Also add a theme helper pointer to the class:

private:<br />
	CThemeHelperST*		m_pTheme;<br />
public:<br />
	void SetThemeHelper(CThemeHelperST* pTheme);


In SetThemeHelper you set the theme pointer:

m_pTheme = pTheme;

5) Now add the overloaded function to CXPStyleProgress:

virtual void DrawProgressBar(CDC* pDC, CRect* pRect, double dFraction);

The complete function body:

void CXPStyleTextProgress::DrawProgressBar(CDC* pDC, CRect* pRect, double dFraction)<br />
{<br />
	BOOL	bDefaultDraw = FALSE;<br />
<br />
	// No theme helper passed<br />
	if (m_pTheme == NULL || m_pTheme->IsAppThemed() == FALSE)<br />
	{<br />
		bDefaultDraw = TRUE;<br />
	} // if<br />
	else<br />
	{<br />
		HTHEME	hTheme = NULL;<br />
		int		iStateId = 0;<br />
<br />
		hTheme = m_pTheme->OpenThemeData(GetSafeHwnd(),   L"PROGRESS");<br />
		if (hTheme)<br />
		{<br />
			// Set up the rectangles. The left one is filled, the right one empty (white)<br />
			CRect LeftRect, RightRect, ClientRect;<br />
			ClientRect = *pRect;<br />
			ClientRect.DeflateRect( 4 /* x */, 3 /* y */); // leave space for the borders<br />
			LeftRect = RightRect = ClientRect;<br />
<br />
			//<br />
			// Draw borders and progress bar<br />
			//<br />
#ifdef PBS_VERTICAL<br />
			DWORD dwStyle = GetStyle();<br />
			if (dwStyle & PBS_VERTICAL)<br />
			{<br />
				// calculate the exact bar and chunk positions for vertical progress<br />
				LeftRect.top = LeftRect.bottom - (int)((LeftRect.bottom - LeftRect.top) * dFraction);<br />
				RightRect.bottom = LeftRect.top;<br />
				// draw the bar and chunk<br />
				m_pTheme->DrawThemeBackground(hTheme, m_hWnd, pDC->GetSafeHdc(), PP_BARVERT, iStateId, pRect, NULL);<br />
				m_pTheme->DrawThemeBackground(hTheme, m_hWnd, pDC->GetSafeHdc(), PP_CHUNKVERT, iStateId, &LeftRect, NULL);<br />
			}<br />
			else<br />
#endif<br />
			// horizontal drawing<br />
			{<br />
				// calculate the exact bar and chunk positions for horizontal progress<br />
				LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)* dFraction);<br />
				RightRect.left = LeftRect.right;<br />
				// draw the bar and chunk<br />
				m_pTheme->DrawThemeBackground(hTheme, m_hWnd, pDC->GetSafeHdc(), PP_BAR, iStateId, pRect, NULL);<br />
				m_pTheme->DrawThemeBackground(hTheme, m_hWnd, pDC->GetSafeHdc(), PP_CHUNK, iStateId, &LeftRect, NULL);<br />
			}<br />
			//<br />
			// Draw themed text (horizontal only)<br />
			//<br />
			int iTextLength = m_strText.GetLength();<br />
			if (iTextLength > 0)<br />
			{<br />
				TCHAR *pszText = new TCHAR[iTextLength+1];<br />
				_tcscpy(pszText, m_strText);<br />
				int widelen = MultiByteToWideChar(CP_ACP, 0, pszText, iTextLength+1, NULL, 0);<br />
				WCHAR *pszWideText = new WCHAR[widelen+1];<br />
				MultiByteToWideChar(CP_ACP, 0, pszText, iTextLength, pszWideText, widelen);<br />
<br />
<br />
				SetBkMode(pDC->GetSafeHdc(), TRANSPARENT);<br />
				m_pTheme->DrawThemeText(hTheme,<br />
					pDC->GetSafeHdc(),<br />
					PP_BAR,<br />
					0,<br />
					pszWideText,<br />
					iTextLength,<br />
					DT_CENTER | DT_VCENTER | DT_SINGLELINE,<br />
					NULL,<br />
					ClientRect);<br />
			}<br />
<br />
			m_pTheme->CloseThemeData(hTheme);<br />
<br />
<br />
		} // if<br />
		else<br />
		{<br />
			bDefaultDraw = TRUE;<br />
		} // else<br />
	} // else<br />
<br />
	if (bDefaultDraw)<br />
	{<br />
		return CTextProgressCtrl::DrawProgressBar(pDC, pRect, dFraction);<br />
	} // if<br />
}<br />


Some remarks:

This themed drawing only supports horizontal text drawing. I do not need vertical text drawing myself so I'll leave it as an excercise for someone else.

I use themed text drawing with something like DrawThemeText(). Alternatively you can use pDC->DrawText() but I think that it is better not to 'mix and match' themed drawing and DC drawing. I tried it and was unhappy with the results: sometimes the text did not redraw properly after 'wiping over' the progress control with another dialog.

Hope this helps.

Hidde Wallaart
software engineer
GeneralRe: XP Theme Support for you Pin
rrrado8-Feb-06 2:58
rrrado8-Feb-06 2:58 
GeneralProblem In SetRange32(0, 0) Pin
roderick25-Jan-04 23:47
roderick25-Jan-04 23:47 
GeneralRe: Problem In SetRange32(0, 0) Pin
sgllama4-Jan-07 2:03
sgllama4-Jan-07 2:03 
GeneralDoesn't compile in VC7 (.Net) Pin
JoeB28-Jul-03 13:56
JoeB28-Jul-03 13:56 
GeneralRe: Doesn't compile in VC7 (.Net) Pin
Hidde Wallaart7-Aug-03 1:32
Hidde Wallaart7-Aug-03 1:32 
GeneralRe: Doesn't compile in VC7 (.Net) Pin
Jason Troitsky (was Hattingh)27-Aug-03 6:11
Jason Troitsky (was Hattingh)27-Aug-03 6:11 
GeneralRe: Doesn't compile in VC7 (.Net) Pin
tamer130-Nov-11 2:50
tamer130-Nov-11 2:50 
GeneralNice :-) Pin
Brian Delahunty9-May-03 5:55
Brian Delahunty9-May-03 5:55 
GeneralGreat! It works! Pin
Nuehli16-Apr-03 4:44
Nuehli16-Apr-03 4:44 
GeneralFont change problem... Pin
Patrick Neuenschwander24-Mar-03 22:59
Patrick Neuenschwander24-Mar-03 22:59 
GeneralRe: Font change problem... Pin
PJ Arends21-Apr-03 17:23
professionalPJ Arends21-Apr-03 17:23 
GeneralVery useful! Pin
Hidde Wallaart24-Jan-02 0:05
Hidde Wallaart24-Jan-02 0:05 

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.