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

Bargraph control using WTL

, 2 Jun 2004
Rate this:
Please Sign up or sign in to vote.
Simple implementation of a bargraph control for graph plotting

Introduction

WTL comes with many control classes that provide standard elements of the user interface. You can subclass or superclass any of these controls to modify their default appearance and behavior. But there may be occasions when none of the supplied controls has the features that your application requires. The solution is to implement your own custom controls.

This article presents two custom controls. The first control is a legend designed to be used in the legend of the map, graph or chart to explain the meaning of each part of the diagram. The legend control is simply a small box drawn to the left of a text string. The box is filled with a brush and surrounded by a 1-pixel-wide frame.

The second control is a bargraph control. It shows two axes on its left and bottom sides, between which a number of vertical bars are shown, each filled with a particular brush and each representing a particular value.

Using the code

In order to use the controls the following files should be included in your project:

  • Bargraph.h
  • Bargraph.cpp
  • Legend.h
  • Legend.cpp
  • Colors.h
The programmer must tell the bargraph control what brush to use by defining a LOGBRUSH structure and what value to use for each bar in the graph. The programmer must also tell the bargraph control how high the whole graph is in units to determine the scale of the bars in the graph.

The following code snippet shows how to use the legend and bargraph controls:

// Declare the variables

CBargraph m_wndBargraph;    // bargraph control
CLegend m_arLegend[NUM_BARS];  // array of legend controls
LOGBRUSH arLB[] = 
{
  { BS_HATCHED, RGB(0, 255, 255), HS_HORIZONTAL },
  { BS_HATCHED, RGB(255, 0, 255), HS_FDIAGONAL },
  { BS_HATCHED, RGB(255, 0, 0), HS_DIAGCROSS },
  { BS_HATCHED, RGB(0, 255, 0), HS_CROSS },
  { BS_HATCHED, RGB(0, 0, 255), HS_BDIAGONAL }
};

TCHAR * arLegendText[] = 
{
  _T("First legend box"),
  _T("Second legend box"),
  _T("Third legend box"),
  _T("Fouth legend box"),
  _T("Fifth legend box"),
  _T('\0')
};
    
// Subclass the static control and attach it to the CBargraph object
BOOL bRet = m_wndBargraph.SubclassWindow(GetDlgItem(IDC_BARGRAPH));
ATLASSERT(bRet);
m_wndBargraph.SetBargraphHeight(BARGRAPH_HEIGHT);

for (int i = 0; i < NUM_BARS; i++)
{
  // Subclass the static controls and attach it to the CLegend objects
  bRet = m_arLegend[i].SubclassWindow(GetDlgItem(FIRST_LEGEND + i));
  ATLASSERT(bRet);
  m_arLegend[i].SetLegendBoxHeight(20);
  m_arLegend[i].SetLegendBoxWidth(20);
  m_arLegend[i].SetLegendLogBrush(arLB[i]);
  m_arLegend[i].SetLegendText(arLegendText[i]);

  m_wndBargraph.AddBar(i, FIRST_BAR_HEIGHT + (i * 10), arLB[i]);
}    

Credits

Some ideas for this control were taken from J. Richter's book "Windows 95: A Developer's Guide"

History

  • 05/30/2004 - Initial release.

Disclaimer

THIS SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO RESPONSIBILITIES FOR POSSIBLE DAMAGES CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

License

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

Share

About the Author

Igor Vigdorchik
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Generalerror c2440 Pinmemberbeeny11-Jun-04 22:52 
GeneralRe: error c2440 PinmemberIgor Vigdorchik12-Jun-04 3:17 
GeneralRe: error c2440 Pinmemberbeeny12-Jun-04 5:22 
GeneralRe: error c2440 PinmemberIgor Vigdorchik12-Jun-04 11:08 
GeneralRe: error c2440 Solution Pinmemberneilck20-Dec-04 6:29 

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
Web02 | 2.8.140916.1 | Last Updated 3 Jun 2004
Article Copyright 2004 by Igor Vigdorchik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid