Click here to Skip to main content
15,881,938 members
Articles / Web Development / HTML
Tip/Trick

C++ / MFC ProgressBar with Percentage Overlay

Rate me:
Please Sign up or sign in to vote.
4.89/5 (10 votes)
5 Jan 2015CPOL1 min read 38.8K   2.5K   22   4
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs there a example? Pin
Member 1037417424-Jan-15 19:18
Member 1037417424-Jan-15 19:18 
Questionnothing new.i did it 20 years ago with pure c Pin
avisal6-Jan-15 6:48
professionalavisal6-Jan-15 6:48 
AnswerRe: nothing new.i did it 20 years ago with pure c Pin
zdanman8-Nov-15 13:49
zdanman8-Nov-15 13:49 
GeneralNot an article Pin
phil.o6-Jan-15 5:40
professionalphil.o6-Jan-15 5:40 

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.