Click here to Skip to main content
Licence 
First Posted 3 Nov 2001
Views 78,498
Bookmarked 28 times

Color Controls

By | 14 Mar 2002 | Article
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

About the Author

Paul J. Weiss

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIs there a fix for Read-Only, yet? Pinmemberweirdog5:38 2 Jan '09  
QuestionMemory Leak??? Pinsussalan herr19:49 7 Dec '07  
Questionif combox is disable,it's color is not change Pinmembertommy790729@126.com22:13 24 Dec '06  
GeneralNewlines on button Pinmemberjojo7223:16 2 Sep '03  
GeneralRadio Button not available PinmemberGary Hasman4:30 18 Sep '02  
GeneralFlicker fix PinmemberJack Handy23:43 27 Mar '02  
GeneralMultiLine Edit doesn't work(-) PinmemberYury Goltsman23:39 16 Mar '02  
GeneralRead Only problem PinmemberMarcus200117:08 9 Mar '02  
GeneralRe: Read Only problem PinmemberPaul J. Weiss3:24 12 Mar '02  
GeneralRe: Read Only problem PinmemberMarcus20013:25 2 Apr '02  
GeneralNice Job but i got one comment PinmemberChris Maundour7:28 24 Feb '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Mar 2002
Article Copyright 2001 by Paul J. Weiss
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid