Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

XGradientZoneBar - an MFC color gradient indicator bar

0.00/5 (No votes)
19 May 2008 1  
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 "XGradientZoneBar.h"
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here