Click here to Skip to main content
13,355,275 members (55,485 online)
Click here to Skip to main content
Add your own
alternative version


162 bookmarked
Posted 17 May 2001


, 4 Nov 2001
Rate this:
Please Sign up or sign in to vote.
An owner-drawn button class that gives a professional look to your buttons.

Sample Image - sample.jpg


With this class you can easily give a professional look to your buttons in a few steps. No bitmap resources are needed - all bitmaps are generated at runtime. Parts of the code come from the CxSkinButton article. The goal is to replace the standard buttons, check boxes, and radio buttons with minimal modifications in the application code.

  1. Add "xShadeButton.cpp" and "xShadeButton.h" to the project.
  2. Include "xShadeButton.h" in the header file where the controls are defined.
  3. Create (or edit) a member variable for each button you want to customize as CxShadeButton. If the Class Wizard doesn't show the CxShadeButton type, select CButton and then edit the code manually.
  4. In the window initialization, add the CxShadeButton methods:
  5. BOOL CxShadeButtonDemoDlg::OnInitDialog()
        //    ...
        //    ...

CxShadeButton Class Members and Operations

CxShadeButton is derived from CButton. The BS_OWNERDRAW style is added automatically, you don't need to set the "Owner draw" property in the resource editor. You can change some styles (flat, push-like, text alignment, group,...) using the resource editor, however not all the styles are currently supported. If you change the aspect of the button at runtime, to avoid flicker, first call the functions that don't cause invalidation (like SetShade, SetIcon, or SetFont) and than invalidate the button, for example with SetWindowText, or directly with Invalidate.

void SetShade(UINT shadeID=0, BYTE granularity=8,
              BYTE highlight=10,BYTE coloring=0,COLORREF color=0);
    Generates the button bitmaps.
  • shadeID: can be one of these effects:
    • SHS_NOISE = 0
    • SHS_HSHADE = 2
    • SHS_VSHADE = 3
    • SHS_HBUMP = 4
    • SHS_VBUMP = 5
    • SHS_HARDBUMP = 7
    • SHS_METAL = 8
  • granularity: this parameter adds a uniform noise to the button bitmaps. A good value is from 5 to 20; 0 to disable the effect. The noise has a positive effect because it hides the palette steps.
  • highlight: sets the highlight level when the mouse is over the button. A good value is from 5 to 20; 0 to disable the effect.
  • coloring: sets the percentage of color to blend in the button palette. The value can range from 0 to 100; 0 to disable the effect.
  • color: if coloring is greater than zero, color is mixed with the standard button colors.
  • Remarks: the coloring and color parameters should be used carefully to guarantee a good aspect in all the situations.

void SetToolTipText(CString s, CString sDown="");
    Sets or changes the tool tip text.
  • s: string displayed in normal state.
  • sDown: (optional) specifies a second text to be displayed when a check box or radio button is checked.
COLORREF SetTextColor(COLORREF new_color);
    Sets or changes the button text color. Returns the previous button text color.
void SetIcon(UINT nIcon, UINT nIconAlign=BS_CENTER, UINT nIconDown=0, 
             UINT nIconHighLight=0);
    Similar to the BS_ICON style.
  • nIcon: ID number of the icon resource.
  • nIconAlign: icon alignment, can be one of the following values:
    • BS_LEFT
    • BS_RIGHT
  • nIconDown: (optional) ID number of the icon resource displayed when the button is checked.
  • nIconHighLight: (optional) ID number of the icon resource displayed when the mouse pointer is over the button.
  • Remarks: the button text is automatically placed so that the icon and the text don't overlap.

bool SetFont(CString sFontName, long lSize=0, long 
             lWeight=400, BYTE bItalic=0, BYTE bUnderline=0);
bool SetFont(LOGFONT* pNewStyle); / LOGFONT* GetFont();
    Changes the text font.
  • sFontName : specifies the typeface name of the font.
  • lSize : (optional) text height.
  • lWeight : (optional) text weight can range from 0 to 1000; 100=thin, 300=light, 400=normal, 700=bold.
  • bItalic : (optional) italic style.
  • bUnderline : (optional) underline style.
  • Remarks: use GetFont/SetFont with a LOGFONT structure to get/set the complete attributes of the font. GetFont returns NULL if the button is using the default system font.

void SetTextAlign(UINT nTextAlign=BS_CENTER);
    Sets the text alignment
  • nTextAlign: button text alignment, can be one of the following values:
    • BS_LEFT
    • BS_RIGHT
void SetFlat(bool bFlag);
    Sets the border style:
  • bFlag can have the following values:
    • FALSE = standard 3D border.
    • TRUE = flat border.


  • Win95, WinNT = Yes, requires IE3.0 or higher
  • Win98, WinME, W2K, WinXP = Yes


Thanks to all the CodeProject developers! Special thanks to:

  • Milan Gardian for mouse and keyboard tracking code.
  • Davide Calabro for CButtonST code snippets.
  • Rainer Mangold for radio-button and check-box code.
  • Jeremy Davis, Andre Brogli, Richard Cunday, Shanker Chandrabose, Luis, Gilad, Rui Lopes, Tom Archer, Tommy Svensson, David Scambler, Orioli Alessandro, João Filipe de Castro Ferreira, Jesper Kinnås, and Derek Lakin for suggestions, debugging, and support.

Release History

  • v1.00 - 12/05/2001
    • Basic implementation and interface.
  • v1.10 - 23/05/2001
    • Added text shift on button down.
    • Fixed many CxDib bugs.
    • Fixed SHS_HARDBUMP bug.
    • Added icon support.
    • Added text alignment.
    • Added flat style.
  • v1.20 - 23/06/2001
    • Fixed keyboard shortcut bug.
    • Check box and radio button add on.
    • Second icon and second tooltip add on.
    • Memory DC for painting operations.
  • v1.30 - 03/08/2001
    • Fixed SetIcon bug.
    • Added Font support.
  • v1.40 - 23/09/2001
    • Fixed memory leakage bug in DrawItem() and SetIcon().
    • Fixed second tooltip initialization bug.
    • Fixed OnLButtonUp() problem with drag and drop.
    • Added multiline tooltip support.
  • v1.41 - 4/11/2001
    • Fixed memory leakage bug in SetIcon() and in the destructor.
    • Added third icon for highlighted state.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: No DDX_Radio support Pin
deepakgupta11114-May-03 5:50
memberdeepakgupta11114-May-03 5:50 
QuestionReally Like The Shades..How to apply to Dialog face?? Pin
Mustafa24-May-02 8:07
memberMustafa24-May-02 8:07 
GeneralProblems!! Pin
Yasmin22-Mar-02 13:32
memberYasmin22-Mar-02 13:32 
GeneralMultiline Setting Pin
Mark Kozel8-Feb-02 9:45
memberMark Kozel8-Feb-02 9:45 
GeneralRe: Multiline Setting Pin
Anonymous24-Jul-02 11:45
sussAnonymous24-Jul-02 11:45 
QuestionTAB? Pin
Jesper Kinnås17-Jan-02 3:45
memberJesper Kinnås17-Jan-02 3:45 
AnswerRe: TAB? Pin
Davide Pizzolato17-Jan-02 11:28
memberDavide Pizzolato17-Jan-02 11:28 
GeneralRe: TAB? Pin
Ravi Bhavnani18-Feb-02 13:33
memberRavi Bhavnani18-Feb-02 13:33 
GeneralRe: TAB? Pin
Davide Pizzolato18-Feb-02 21:10
memberDavide Pizzolato18-Feb-02 21:10 
GeneralProblems using CxShadeButton with ResizableLib Pin
João Ferreira5-Nov-01 4:17
memberJoão Ferreira5-Nov-01 4:17 
GeneralRe: Problems using CxShadeButton with ResizableLib Pin
Paolo Messina5-Nov-01 12:03
memberPaolo Messina5-Nov-01 12:03 
GeneralRe: Problems using CxShadeButton with ResizableLib Pin
Davide Pizzolato5-Nov-01 13:20
memberDavide Pizzolato5-Nov-01 13:20 
GeneralDownload Pin
FilthyZombie5-Nov-01 0:23
memberFilthyZombie5-Nov-01 0:23 
GeneralRe: Download Pin
Davide Pizzolato5-Nov-01 8:48
memberDavide Pizzolato5-Nov-01 8:48 
GeneralRe: Download Pin
FilthyZombie5-Nov-01 23:23
memberFilthyZombie5-Nov-01 23:23 
GeneralProblems in CxShadeButton::DrawItem() function !! Pin
Cris22-Oct-01 9:01
memberCris22-Oct-01 9:01 
GeneralRe: Problems in CxShadeButton::DrawItem() function !! Pin
Davide Pizzolato22-Oct-01 13:03
memberDavide Pizzolato22-Oct-01 13:03 
GeneralSomething wrong with useing SetIcon()! Pin
Anonymous22-Oct-01 3:59
memberAnonymous22-Oct-01 3:59 
GeneralRe: Something wrong with useing SetIcon()! Pin
Davide Pizzolato22-Oct-01 13:02
memberDavide Pizzolato22-Oct-01 13:02 
GeneralRe: Something wrong with useing SetIcon()! Pin
Anonymous22-Oct-01 17:39
memberAnonymous22-Oct-01 17:39 
GeneralFixed SetIcon() Pin
Davide Pizzolato23-Oct-01 9:46
memberDavide Pizzolato23-Oct-01 9:46 
GeneralRe: Fixed SetIcon() Pin
Anonymous26-Oct-01 5:13
memberAnonymous26-Oct-01 5:13 
GeneralMemory allocation/free conflict Pin
Hal Director25-Sep-01 17:53
memberHal Director25-Sep-01 17:53 
QuestionHow to set the default shade effect Pin
Davide Pizzolato24-Sep-01 8:42
memberDavide Pizzolato24-Sep-01 8:42 
QuestionLooks great! How about CPropertySheet? Pin
Anonymous21-Sep-01 11:18
memberAnonymous21-Sep-01 11:18 

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.180111.1 | Last Updated 5 Nov 2001
Article Copyright 2001 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid