viksoe.dk

Coloured controls

Coloured controls


This article was submitted .


No, the large quantities of Coca-Cola is not making your eyes see groovy colours... it's a test of my Coloured Controls.

This is a bunch of subclassed controls, which paint the components in colours of your choice. Some of the classes stoop down to owner-drawn and custom-drawn techniques to achieve their goal, so even if you don't need cheery colours in your application, it might be worth a check.

Bringing on your own colours to the standard Windows controls seems terrible awkward. Almost all of the controls support some form or another of changing its default colouring, but it's only with the latest Windows Common Controls that we actually see a simple API emerging, such as the ListView_SetTextColor.
The original Windows controls (the Edit, Button, ListBox etc.) support changing colours by reflecting on the WM_CTLCOLORxxx range of messages.
The new Windows Common Controls support complex colour-schemes as part of their custom drawing behaviour. Unfortunately, this is buried deeply enough in the documentation to cause most developers to miss it completely.

The list of controls included in this package:

CColoredDialog
CColoredStaticCtrl
CColoredEditCtrl
CColoredButtonCtrl
CColoredCheckboxCtrl
CColoredOptionCtrl
CColoredComboBoxCtrl
CColoredTabCtrl
CColoredListViewCtrl
CColoredTreeViewCtrl

How they work

It's straight-ahead. Just add them as any other subclassed WTL control.
Add a member variable to your dialog implementation file; pick the class that extends your dialog element.
CColoredEditCtrl m_edit
In the OnInitDialog() event handler, add the following lines:
  LRESULT OnInitDialog(UINT /*uMsg*/, 
                       WPARAM /*wParam*/, 
                       LPARAM /*lParam*/, 
                       BOOL& /*bHandled*/)
  {
    ...
    m_edit.SubclassWindow(GetDlgItem(IDC_EDIT1));
    m_edit.SetWindowText("bla bla bla");
    ...
  }
Add the following reflection macro to your dialog's message map:
  BEGIN_MSG_MAP(CMainDlg)
    ...
    REFLECT_NOTIFICATIONS()
  END_MSG_MAP()
Then use the SetXXXColor() methods to change the colours.
Construct a COLORREF colour using the RGB(r,g,b) macro. To use the default (Windows system) colour, specify -1 (or CLR_INVALID).
If the method takes two arguments, then the first is usually the text colour and the second is the background.
Each control supports different kinds of colouring. Some have colours specific for a disabled state, a few allow you to specify the colours when an item is selected, the Edit control has a special Read Only state and so on...
    m_edit.SetNormalColors(RGB(255,0,0), RGB(0,255,0));
    m_edit.SetReadOnlyColors(RGB(255,0,0), RGB(0,235,0));
    ...

None of the Windows controls seems to allow overriding the disabled text colour. Perhaps because Bill Gates prefers to use the GrayString() API internally. Some of the newer Common Controls will not allow you to change the colour of the selected item box either. This is a pity.

I'm not sure how these controls will look in Windows XP. With the new theme painting and all, perhaps they are not looking so cool.

Notes

Note the consistent use of the English spelling of colours in this article just to annoy my American friends. Smiley

Source Code Dependencies

Microsoft Visual C++ 6.0
Microsoft WTL 3.1 Library

Download Files

DownloadSource Code (8 Kb)

To the top