Bargraph control
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.
How to use it
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.
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]);
}