Click here to Skip to main content
13,143,774 members (36,420 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 23 Sep 2004

XP Style CColorControl class with themed gradient buttons

, 26 Sep 2004
Rate this:
Please Sign up or sign in to vote.
Set of control derived classes for colorful presentation.



This article describes a set of control derived classes for colorful presentation. XP-styled hot-tracked controls with easy font customization and themed gradient buttons. Gives your edit boxes and buttons a nice look and feel both in XP and 2K.

This work is partly based on the articles "XP Style CBitmapButton (CHoverBitmapButton)" by Rail Jon Rogut and "Color Controls" by Paul J. Weiss. Credits to you both.

Note that the original versions of the control derived classes were written by Paul J. Weiss. Since the application I wrote this for is intended to run on touch screen systems, I focused mainly on the base class CColorControl, the CEdit derived CColorEdit, the CStatic derived CColorStatic, and most of all on the CButton derived CColorButton. These classes have been completely rewritten while the CColorComboBox class consists of the original work by Paul. I kept it here in case anyone wants to make an effort. Don't expect it to work as it is (no offence Paul =).

Using the code

  • Runs on Windows 2000 (classic style only, ses sample screen dump) and Windows XP.
  • CColorButton uses Uxtheme.h and tmschema.h (and corresponding libraries) for visual styling. Have your project include the path to these files (found in the SDK).
  • CColorButton uses GDI+ for drawing. gdiplus.dll must be present on targeted systems. Refer to SDK.
  • Add theme.cpp and ColorControl.cpp to your project.
  • Include ColorControl.h where you need it.
  • Add CButton and CEdit controls to your dialog template with desired style (disabled, readonly etc.).
  • Create control variables for each control of type CColorButton or CColorEdit.
  • Compile and run.

Note that visual styling only works for Windows XP with XP-style enabled. In Windows 2000, the CTheme class will fail to load the required DLL, and classic style will be forced.

Common functions:

  • void CColorControl::SetControlFont (int pointSize, CString fontName, bool bold = false, bool italic = false );
  • void CColorControl::SetColors (const COLORREF FGColor, const COLORREF BGColor, const COLORREF HotFGColor, const COLORREF HotBGColor);
  • void CColorControl::SetDisabledColors (const COLORREF DisabledFGColor = RGB_GRAYTEXT, const COLORREF DisabledBGColor = RGB_BTNFACE);
  • void CColorControl::SetColorBg (COLORREF clr);
  • void CColorControl::SetColorFg (COLORREF clr);
  • void CColorControl::SetColorBgHot (COLORREF clr);
  • void CColorControl::SetColorFgHot (COLORREF clr);
  • void CColorControl::SetColorBgDisabled(COLORREF clr);
  • void CColorControl::SetColorFgDisabled(COLORREF clr);
  • void CColorControl::SetRolloverDelay (UINT mSeconds);
  • void CColorControl::EnableHot (bool bEnable}; // turns hottracking on/of

CColorButton functions:

  • void CColorButton::EnableXP (bool bEnable); // turns xp-style (if present on system) on/off
  • void CColorButton::SetGradientAngle (float nAngle);
  • void CColorButton::SetGradientColors (BYTE alphaUpper, COLORREF rgbUpper, BYTE alphaLower, COLORREF rgbLower, BYTE alphaLowerHot, COLORREF rgbLowerHot);

Tip: If you still need to handle WM_CTLCOLOR for controls not derived from CColorControl, you might wind up with a lot of case: or if statements to determine whether to let the message slip (for the CColorControl derives) or to do something. To reduce the workload for this case, I added the DECLARE/IMPLEMENT_DYNAMIC macros to the classes. This lets you use IsKindOf/RUNTIME_CLASS so you can do something like this:

HBRUSH CViewOperator::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
   HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

   if(nCtlColor == CTLCOLOR_DLG || nCtlColor == CTLCOLOR_STATIC)
   // CTLCOLOR_STATIC also send for disabled or read-only CColorEdit
      if( pWnd->IsKindOf(RUNTIME_CLASS(CColorEdit))   ||
          pWnd->IsKindOf(RUNTIME_CLASS(CColorButton)) ||
          pWnd->IsKindOf(RUNTIME_CLASS(CColorStatic)) ||
          return hbr;

      static CBrush staticBrush(m_bgColor);
      return (HBRUSH)staticBrush;
    return hbr;


  • Added font support
  • Added CColorButton::SetGradientAngle(float fAngle)

Don't hesitate to improve or change whatever you feel like and post it back. I'm all for perfection!

Good luck /Anders


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

Anders Sandberg
Web Developer
Sweden Sweden
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSolid color as well? Pin
Steve Kowald8-Dec-05 5:08
memberSteve Kowald8-Dec-05 5:08 
GeneralImproved Fill for XP Buttons Pin
Steve Johnson (Sven)28-Oct-05 13:23
memberSteve Johnson (Sven)28-Oct-05 13:23 
GeneralRe: Improved Fill for XP Buttons Pin
Steve Johnson (Sven)28-Oct-05 13:39
memberSteve Johnson (Sven)28-Oct-05 13:39 
GeneralAdding an Icon Pin
Alex Evans27-Jun-05 14:00
memberAlex Evans27-Jun-05 14:00 
GeneralIt is looking great Pin
Sanjit Rath27-Oct-04 8:12
memberSanjit Rath27-Oct-04 8:12 
GeneralGreat article Pin
Yves Tkaczyk25-Sep-04 14:26
memberYves Tkaczyk25-Sep-04 14:26 
GeneralGreat stuff! Pin
Ravi Bhavnani24-Sep-04 2:44
memberRavi Bhavnani24-Sep-04 2:44 
GeneralRe: Great stuff! Pin
sudhir mangla24-Sep-04 21:32
membersudhir mangla24-Sep-04 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 27 Sep 2004
Article Copyright 2004 by Anders Sandberg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid