Click here to Skip to main content
15,885,890 members
Articles / Desktop Programming / MFC
Article

XP Style CColorControl class with themed gradient buttons

Rate me:
Please Sign up or sign in to vote.
4.85/5 (11 votes)
26 Sep 20043 min read 57.8K   3K   29   8
Set of control derived classes for colorful presentation.

Screendump

Introduction

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)) ||
          pWnd->IsKindOf(RUNTIME_CLASS(CColorComboBox)))
          return hbr;

      pDC->SetBkColor(m_bgColor);
      static CBrush staticBrush(m_bgColor);
      return (HBRUSH)staticBrush;
    }
    return hbr;
}

History

  • Added font support
  • Added DECLARE/IMPLEMENT_DYNAMIC macros
  • 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

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


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSolid color as well? Pin
Steve Kowald8-Dec-05 5:08
Steve Kowald8-Dec-05 5:08 
GeneralImproved Fill for XP Buttons Pin
Steve Johnson (Sven)28-Oct-05 13:23
Steve Johnson (Sven)28-Oct-05 13:23 
GeneralRe: Improved Fill for XP Buttons Pin
Steve Johnson (Sven)28-Oct-05 13:39
Steve Johnson (Sven)28-Oct-05 13:39 
GeneralAdding an Icon Pin
Alex Evans27-Jun-05 14:00
Alex Evans27-Jun-05 14:00 
GeneralIt is looking great Pin
sanjit_rath27-Oct-04 8:12
sanjit_rath27-Oct-04 8:12 
GeneralGreat article Pin
Yves Tkaczyk25-Sep-04 14:26
Yves Tkaczyk25-Sep-04 14:26 
GeneralGreat stuff! Pin
Ravi Bhavnani24-Sep-04 2:44
professionalRavi Bhavnani24-Sep-04 2:44 
GeneralRe: Great stuff! Pin
Sudhir Mangla24-Sep-04 21:32
professionalSudhir 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.