Click here to Skip to main content
Click here to Skip to main content
Go to top

XGradientZoneBar - an MFC color gradient indicator bar

, 19 May 2008
Rate this:
Please Sign up or sign in to vote.
XGradientZoneBar displays an indicator bar that allows you to specify multiple zones that are filled with a color gradient, and includes APIs to set the bar orientation, font, and tick marks.
screenshot
Winner April 2008
Monthly Competition

Introduction

Let me start by saying that XGradientZoneBar is not a progress control. It is an indicator control, like those used in real-time displays of levels, etc.:

screenshot

The basic idea is that of a bar control with multiple zones, each of which can be colored with gradient fill. Optionally, you can display text and tick marks (the image above shows example of four-zone XGradientZoneBar with box tick marks).

XGradientZoneBar Features and Behaviors

Except for tooltips, there is no user interaction with XGradientZoneBar control. The demo app reveals most of the XGradientZoneBar features:

screenshot

The Color Schemes shown in screenshot are for demo purposes only, and are not built into XGradientZoneBar. To try demo, first select either the Horizontal or the Vertical bar, and then change the settings. You can always get back to the original demo settings with Reset button.

To get an idea of what XGradientZoneBar can be used for, click on Demo button:

screenshot

XGradientZoneBar API

Function Description
COLORREF GetBackgroundColor() Retrieves background color
CXGradientZoneBar& SetBackgroundColor(COLORREF cr, BOOL bRedraw = TRUE) Sets background color
void GetBarInfo(XGRADIENT_ZONE_BAR_INFO *pXZBI); Retrieves XGRADIENT_ZONE_BAR_INFO struct for bar
CXGradientZoneBar& SetBarInfo(XGRADIENT_ZONE_BAR_INFO *pXZBI); Sets bar options via XGRADIENT_ZONE_BAR_INFO struct
ORIENTATION GetBarOrientation() Retrieves bar orientation (HORIZONTAL or VERTICAL)
CXGradientZoneBar& SetBarOrientation(ORIENTATION eOrientation, BOOL bRedraw = TRUE) Sets bar orientation (HORIZONTAL or VERTICAL)
BOOL GetBold(); Retrieves bold setting
CXGradientZoneBar& SetBold(BOOL bBold, BOOL bRedraw = TRUE); Sets bold font
CString GetFontFaceName(); Retrieves font face name
int GetFontPointSize(); Retrieves font point size
CXGradientZoneBar& SetFont(LPCTSTR lpszFaceName, int nPointSize, BOOL bRedraw = TRUE); Sets font face name and point size
BOOL GetFont(LOGFONT *pLF); Retrieves LOGFONT struct for font
CXGradientZoneBar& SetFont(LOGFONT * pLogFont, BOOL bRedraw = TRUE); Sets font via LOGFONT struct
CFont* GetFont() Retrieves pointer to CFont member variable
CXGradientZoneBar& SetFont(CFont *pFont, BOOL bRedraw = TRUE); Sets font via CFont object
int GetMaxPercent() Retrieves max percent fill
CXGradientZoneBar& SetMaxPercent(int nMaxPercent, BOOL bRedraw = TRUE) Set percent of gradient fill to show; remainder will be filled with background color
CString GetText() Retrieves bar text
CXGradientZoneBar& SetText(LPCTSTR lpszText, BOOL bRedraw = TRUE) Sets bar text
TEXTALIGNHORIZONTAL GetTextAlignHorizontal() Retrieves bar text horizontal alignment (LEFT, RIGHT, CENTER)
CXGradientZoneBar& SetTextAlignHorizontal(TEXTALIGNHORIZONTAL eAlign, BOOL bRedraw = TRUE) Sets bar text horizontal alignment (LEFT, RIGHT, CENTER)
TEXTALIGNVERTICAL GetTextAlignVertical() Retrieves bar text vertical alignment (TOP, BOTTOM, VCENTER)
CXGradientZoneBar& SetTextAlignVertical(TEXTALIGNVERTICAL eAlign, BOOL bRedraw = TRUE) Sets bar text vertical alignment (TOP, BOTTOM, VCENTER)
COLORREF GetTextColor() Retrieves text color
CXGradientZoneBar& SetTextColor(COLORREF cr, BOOL bRedraw = TRUE) Set text color
UINT GetTextOrientation() Retrieves text orientation (0, 90 or 270)
CXGradientZoneBar& SetTextOrientation(UINT nTextOrientation, BOOL bRedraw = TRUE); Sets text orientation (0, 90 or 270)
COLORREF GetTickColor() Retrieves tick color
CXGradientZoneBar& SetTickColor(COLORREF cr, BOOL bRedraw = TRUE) Sets tick color
UINT GetTicksPerZone() Retrives number of ticks per zone
CXGradientZoneBar& SetTicksPerZone(UINT nTicksPerZone, BOOL bRedraw = TRUE) Sets number of ticks per zone
void GetTickSizes(UINT& nUnitTick, UINT& n10thTick) Retrieves tick sizes
CXGradientZoneBar& SetTickSizes(UINT nUnitTick, UINT n10thTick, BOOL bRedraw = TRUE) Sets tick sizes
TICKMARKTYPE GetTickType() Retrieves tick type (NOTICKMARKS, TOP_LEFT, BOTTOM_RIGHT, BOTH, BOX)
CXGradientZoneBar& SetTickType(TICKMARKTYPE eTickMarkType, BOOL bRedraw = TRUE) Sets tick type (NOTICKMARKS, TOP_LEFT, BOTTOM_RIGHT, BOTH, BOX)
CString GetToolTipText() Retrieves tooltip text
CXGradientZoneBar& SetToolTipText(LPCTSTR lpszText, BOOL bRedraw = TRUE) Sets tooltip text
TOOLTIPTYPE GetToolTipType() Retrieves tooltip type (NOTOOLTIP, TEXT_TOOLTIP, PERCENT_TOOLTIP, TEXT_PERCENT_TOOLTIP, PERCENT_TEXT_TOOLTIP)
CXGradientZoneBar& SetToolTipType(TOOLTIPTYPE eType, BOOL bRedraw = TRUE) Sets tooltip type (NOTOOLTIP, TEXT_TOOLTIP, PERCENT_TOOLTIP, TEXT_PERCENT_TOOLTIP, PERCENT_TEXT_TOOLTIP)
CXGradientZoneBar& SetZones(UINT nZones, XGRADIENT_ZONE *pZones, BOOL bRedraw = TRUE); Sets gradient bar zones

Tooltip Formats

There are five options for XGradientZoneBar tooltip format (including no tooltip):

Text
screenshot
Percent
screenshot
Text + Percent
screenshot
Percent + Text
screenshot

How to use

The following steps assume you want to add XGradientZoneBar to a dialog. Steps would be similar for CFormView or CPropertyPage.

Step 1 - Add Files

To integrate CXGradientZoneBar into your app, you first need to add following files to your project:

  • XGradientZoneBar.cpp
  • XGradientZoneBar.h

Step 2 - Add Placeholder Rect to Dialog Resource

Next add a STATIC or other control to dialog resource, where you want the XGradientZoneBar to be displayed. The dialog for demo app looks like this:

screenshot

Note that this step is not required, if you have some other way to specify where XGradientZoneBar should be displayed.

Step 3 - Create the Control

You need to do two things here: first, add #include statement to dialog class header file:
#include <span class="code-string">"XGradientZoneBar.h"</span>
and insert variable that looks like:
    CXGradientZoneBar  m_GradientZoneBar;
Second, add code to OnInitDialog() function:
    CRect rect;
    GetDlgItem(IDC_STATIC_BAR1)->GetWindowRect(&rect);
    ScreenToClient(&rect);
    GetDlgItem(IDC_STATIC_BAR1)->ShowWindow(SW_HIDE);
    VERIFY(m_GradientZoneBar.Create(AfxGetInstanceHandle(), 0, 
        WS_CHILD | WS_VISIBLE, rect, this, IDC_BAR1));

Step 4 - Set XGradientZoneBar Options

According to your app's requirements, you will probably want to set one or more XGradientZoneBar options. Here are options set for Bar1 in demo app:
    m_Bar1.SetBarOrientation(CXGradientZoneBar::VERTICAL, FALSE)
          .SetText(_T("Bar1"), FALSE)
          .SetTextAlignVertical(CXGradientZoneBar::TOP)
          .SetTextColor(RGB(0,255,0))
          .SetFont(_T("Times New Roman"), 18, FALSE)
          .SetTextOrientation(90, FALSE)
          .SetZones(_countof(GreenToRed), GreenToRed, FALSE)
          .SetTickType(CXGradientZoneBar::TOP_LEFT, FALSE)
          .SetToolTipText(_T("This is Bar1"), FALSE)
          .SetToolTipType(CXGradientZoneBar::TEXT_TOOLTIP, FALSE)
          .SetBackgroundColor(RGB(0,0,0))
          .SetMaxPercent(50);

References

Here are links to my articles on CodeProject, which I have used in demo app:

Revision History

Version 1.0 - 2008 April 24

  • Initial public release

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


License

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

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
GeneralWish~ Pinmemberdnest13-Apr-09 19:05 
GeneralGood work as usual PinmemberHatem Mostafa4-May-08 20:31 
GeneralVS6 Compilation problem PinmemberIvan Krasko29-Apr-08 7:32 
GeneralRe: VS6 Compilation problem PinmvpHans Dietrich29-Apr-08 8:43 
GeneralRe: VS6 Compilation problem PinmemberIvan Krasko29-Apr-08 10:17 
GeneralExcellent! PinmemberJohn Schroedl29-Apr-08 4:44 
GeneralEase up on the other Authors PinmvpJeffrey Walton25-Apr-08 23:41 
GeneralRe: Ease up on the other Authors PinmvpHans Dietrich26-Apr-08 4:03 
GeneralRe: Ease up on the other Authors [modified] PinmemberDarren Sessions30-Jun-08 8:04 
GeneralPage not found PinmemberSergioPDT24-Apr-08 23:11 
GeneralRe: Page not found PinmemberJohn C25-Apr-08 6:56 
GeneralRe: Page not found PinmemberSergioPDT25-Apr-08 7:08 
GeneralRe: Page not found PinmemberJohn C25-Apr-08 7:25 
GeneralWow that is so cool! PinmemberReynolds Glisner24-Apr-08 18:34 
GeneralAgreed! 5! PinprotectorMarc Clifton24-Apr-08 9:23 
GeneralRe: Agreed! 5! PinmemberJohnny J.19-May-08 21:05 
General5 PinmvpJohn Simmons / outlaw programmer24-Apr-08 7:35 

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 | Mobile
Web03 | 2.8.140905.1 | Last Updated 20 May 2008
Article Copyright 2008 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid