Click here to Skip to main content
Licence CPOL
First Posted 2 Jun 2004
Views 41,008
Bookmarked 14 times

Bargraph control using WTL

By | 2 Jun 2004 | Article
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)

About the Author

Igor Vigdorchik

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalerror c2440 Pinmemberbeeny22:52 11 Jun '04  
GeneralRe: error c2440 PinmemberIgor Vigdorchik3:17 12 Jun '04  
GeneralRe: error c2440 Pinmemberbeeny5:22 12 Jun '04  
GeneralRe: error c2440 PinmemberIgor Vigdorchik11:08 12 Jun '04  
GeneralRe: error c2440 Solution Pinmemberneilck6:29 20 Dec '04  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 3 Jun 2004
Article Copyright 2004 by Igor Vigdorchik
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid