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

Color Controls

0.00/5 (No votes)
14 Mar 2002 1  
Adding color to MFC controls

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.

// FG is foreground color or text color

// BG is background color of control

// Hot refers to the on mouse over 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);

//

// Time is in milliseconds and dictates the delay after the mouse

// leaves the rect of the control. The default is 10 milliseconds.

//

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.

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