Introduction
The great article on subclassing by Chris Maunder inspired me
to add to his code. Changing the color of a common control is often tricky because depending on the control different functions
may need to be overridden. For example to color a CButton
you must implement:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS)
whereas for a CEdit
you must implement:
virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam,
LRESULT* pLResult)
And for a CStatic
...
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor)
I often found myself overridding the OnCtlColor
method in my dialog class and then using a case statement to switch
among the various controls. For applications with only a few controls this method is quick and easy. But when the number
of controls increases adding and removing color controls becomes cumbersome and error prone. It also places too much logic
into the dialog class. The current set of classes provide basic colorization for active and disabled states along with
adding color mouse-over's (thanks Chris!).
Interface
CColorButton, CColorEdit, CColorComboBox, CColorStatic
are derived from CButton, CEdit, CComboBox, CStatic
respectively.
They also inherit from CColorControl
which provides the same interface for setting the colors.
void SetColors(const COLORREF FGColor,const COLORREF BGColor,
const COLORREF HotFGColor, const COLORREF HotBGColor);
void SetDisabledColors(const COLORREF DisabledFGColor,
const COLORREF DisabledBGColor);
void SetBgColor(COLORREF cr);
void SetFgColor(COLORREF cr);
void SetHotBgColor(COLORREF cr);
void SetHotFgColor(COLORREF cr);
void SetDisabledFgColor(COLORREF cr);
void SetDisabledBgColor(COLORREF cr);
void SetRolloverDelay(UINT mSeconds );
Usage
After the control(s) are put into the dialog editor.
Simply declare member variables in the class wizard. Make sure to include the control subclasses and rebuild or else the new class
might not appear in the variable type combo.
Now in the OnInitDialog
function of you main dialog or the OnCreate
function of your
CWnd
set the desired colors
for the controls.
m_btnColor.SetColors(RGB_GREEN, RGB_BLACK, RGB_WHITE, RGB_RED);
m_btnDisabled.SetDisabledColors(RGB_WHITE, RGB_BLACK);
m_btnDisabled.SetColors(RGB_RED, RGB_BLACK, RGB_RED, RGB_WHITE);
m_edtColor.SetColors(RGB_GREEN, RGB_BLUE, RGB_WHITE, RGB_RED);
m_cbColor.SetColors(RGB_BLUE, RGB_GREEN, RGB_WHITE, RGB_RED);
m_stcColor.SetColors(RGB_GREEN, RGB_BLUE, RGB_WHITE, RGB_RED);
Conclusion
That's all there is to it. These set of classes should provide you with an easy way to add color to your controls without having to implement any drawing or
message callback functions. If you just want the color controls without the mouse overs you could make the
HotFGColor
the same as
the FGColor
and the HotBGColor
the same as the BGColor
.
History
15 Mar 2002 - updated downloads.