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

Bargraph control using WTL

0.00/5 (No votes)
2 Jun 2004 1  
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 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