Click here to Skip to main content
12,890,181 members (42,317 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 11 Mar 2002


, 20 Mar 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
An easy control to display and to modify percentages
<!-- Download Links --> <!-- Article image -->

Sample Image - PercentageCtrl.gif

<!-- Add the rest of your HTML here -->


CPercentageCtrl is an easy control to display the ratio between many entities, as the used space and the free space, or the composition of a cocktail :)

Obviously user can modify ratios dragging the blocks' edges.

Using the control

Add an instance of the control as member data in your dialog;
class CMyDialog : public CDialog
    CPercentageCtrl m_percentage;

Then initialize the control in OnInitDialog() using the Create(...) function

BOOL CMyDialog::OnInitDialog()
    CRect r(10,10, 200, 50);
    m_percentage.Create(NULL, NULL, WS_VISIBLE | WS_BORDER, r, 
                           this, ID_PERCENTAGE);

To add a block is really easy, you must only specify its weight:


In any block you can select the color and a text to be displayed: the text can be formatted using CString::Format(...) (or printf) style, the color is the usual RGB value

m_percentage.AddBlock(250, "%0.1f%%", RGB(200,255,255));

Blocks' dimension must be an integer value, but you can set a double multiplier that is used to display the text. For example, if you want to show the value as "25.1" you should insert a 251 dimension and set the multiplier to 0.1


Some options are available:

  • PC_ALWAYSSHOWTEXT: the control will show the text even if it is not completely contained in the block; otherwise the control display text only if it is completely contained in his block.
  • PC_TEXTELLIPSIS: if it's needed, the control does a text ellipsis to shorten the text as it could be contained in the block.
You can set these options using the command SetOptions(int):

Any time that the user modifies the dimension of a block, the control sends a message. You can intercept this message inserting in the messages map the macro:


For example:

BEGIN_MESSAGE_MAP(CPercentageCtrlDemoDlg, CDialog)
In this case remember to declare the message PERCENTAGE_CHANGED in this way:
The called function will receive in WPARAM the handle of the calling control, and in LPARAM the index of the first modified block (when a user modifies a block, he modifies also that one at its right). The OnPercentageChanged function could be:
LRESULT MyDialog::OnPercentageChanged(WPARAM WParam, LPARAM LParam)
    if ((HWND)WParam == m_percentage.m_hWnd)
        int data1 = m_percentage.GetData((int)LParam);
        int data2 = m_percentage.GetData((int)LParam+1);

Developing notes

I chose to allow the insertion of entire values with a multiplier in order to avoid errors of approximation in the visualization of the text
(e.g. 99 = 24 + 24 + 24 + 27 = 24.4 + 24.4 + 24.4 + 26.8 = 100 = 24.6 + 24.6 + 24.6 + 26.2 = 25 + 25 + 25 + 26 = 101)

I used Keith Rule's CMemDC to avoid flickering. You can find his class and an article here at <a href=>codeproject :)

Obviously the code compiles cleanly under the warning level 4 :)


21 Mar 2002 - serious GDI resources leaks fixed.


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


About the Author

Paolo Vernazza
Software Developer (Senior) Ermit
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
memberMURALEE KRISHNAN N28-Nov-12 8:26 
GeneralThank you very much for this workspace posting Pin
memberMURALEE KRISHNAN N28-Nov-12 8:25 
GeneralVery cool! Suggestion though: Pin
ajh2-May-03 17:32
memberajh2-May-03 17:32 
GeneralGreat Control ! Pin
DizzyMissLizzy11-Dec-02 23:00
sussDizzyMissLizzy11-Dec-02 23:00 
GeneralThis control rules Pin
Mark Cooper23-May-02 15:46
memberMark Cooper23-May-02 15:46 
GeneralThank you! Pin
sport26-Apr-02 0:01
membersport26-Apr-02 0:01 
Generalnice! Pin
Mazdak13-Mar-02 3:05
memberMazdak13-Mar-02 3:05 
GeneralVery clever control, but it crashes Pin
Eric Nitzsche12-Mar-02 12:53
memberEric Nitzsche12-Mar-02 12:53 
GeneralRe: Very clever control, but it crashes Pin
PJ Arends12-Mar-02 16:31
memberPJ Arends12-Mar-02 16:31 
GeneralRe: Very clever control, but it crashes Pin
ilinov12-Mar-02 21:10
memberilinov12-Mar-02 21:10 
GeneralRe: Very clever control, but it crashes Pin
p1x22-Mar-02 1:39
memberp1x22-Mar-02 1:39 
GeneralGreat job! Pin
Alexander Wiseman12-Mar-02 10:06
memberAlexander Wiseman12-Mar-02 10:06 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 21 Mar 2002
Article Copyright 2002 by Paolo Vernazza
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid