Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

C++ / MFC ProgressBar with Percentage Overlay

4.89/5 (10 votes)
5 Jan 2015CPOL1 min read 40.1K   2.5K  
Fully Customizable ProgressBar Control with Percentage shown that changes color as progress covers it

Introduction

Always wondered how to make the old-school style progress bars that had different colors and the percentage changed colors when the progress reached it? Here you go, and it's fully customizable! You also use a floating point number in order to set the percentage (although it's programmed to not show the decimal when drawing). This lets it do a lot of the calculating for you.

Background

I've been using this one I created (over a decade ago) and noticed that no one has ever posted anything like this. This is compatible of course with any Windows operating system. It was originally compiled in with Visual C++ 6.0 but should work with any Visual Studio Environment.

Using the Code

Class Documentation

See header file for the most up to date class documentation. Most of the parameters are self explanatory, otherwise a comment is placed beside it to help.

C++
CProgressBar

	BOOL Create( DWORD dwExStyle,
				DWORD dwStyle,
				const RECT& rect,
				CWnd *pParentWnd,
				COLORREF crBarColor = 0xFF, 	// progress color
				COLORREF crBkColor = 0xFFFFFF, 	// no progress color
				COLORREF crTextOverColor = 0xFFFFFF, // text color
								// the progress is overlapping
				COLORREF crTextOutColor = 0, 	// text color 
								// the progress hasn't reached yet
				BOOL bShowPercentage = 1,
				CFont *pFont = NULL ); 		// if NULL uses a default 
								// "MS Sans Serif" 14 height font if

	// get step amount
	float GetStepAmount( ) const;

	// increment the position by the step amount
	void Step();

	// set step amount
	void SetStepAmount( float fStepAmount );

	// set the position
	void SetPosition( float fPos );

	// get the current position
	float GetPosition( ) const;

	// set range (maximum and minimum)
	void SetRange( UINT nStartPos, UINT nEndPos );

Messages Implemented

	// Return values may differ from common controls implementation.
	
	PBM_STEPIT
	PBM_SETRANGE32
	PBM_GETPOS
	PBM_FLOAT_SETPOS 	// float compatible version of PBM_SETPOS, 
				// WPARAM = valid pointer to a float variable, 
				// therefore you should only use this message in 
				// SendMessage to ensure there is no access violation
	PBM_FLOAT_SETSTEP 	// float compatible version of PBM_SETSTEP, 
				// WPARAM = valid pointer to a float variable, 
				// therefore you should only use this message in 
				// SendMessage to ensure there is no access violation

To use, simply use it like you would any other window. Create a CProgressBar object in your window's class and call the CProgressBar::Create() member function in your window initialization. Example:

C++
m_wndBar.Create(WS_EX_CLIENTEDGE, WS_BORDER | 
WS_VISIBLE | WS_CHILD, rc, this, RGB(0, 100, 255), 0xFFFFFF, 0xFFFFFF);

Now you can start using the action functions such as SetPosition, SetRange, Step(), etc... Have fun. You are welcome.

History

  • v1.1 - 1/1/2015: Added float compatible message handlers
  • v1.0 - 1/1/2004: Initial release

License

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