Click here to Skip to main content
Click here to Skip to main content


, 9 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A skinned button class with clipping region, tooltip and mouse tracking

Sample Image - sample.gif

With this class you can easily give a skin to your buttons in few steps:

  1. Add xSkinButton.cpp and xSkinButton.h to the project.
  2. Include xSkinButton.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 CxSkinButton. If the Class Wizard doesn't show the CxSkinButton type, select CButton and then edit the code manually.
  4. Add the bitmap resource for the buttons:
    • NORMAL bitmap: default button image.
    • DOWN bitmap: pushed button image.
    • OVER bitmap:(optional) image to shown when the mouse is over the button. If NULL, NORMAL bitmap will be used.
    • DISABLED bitmap: (optional) image for the disabled state. If NULL, NORMAL bitmap will be used.
    • FOCUS bitmap: (optional) image for the focused state. If NULL, NORMAL bitmap will be used.
    • MASK bitmap: (optional) clipping region. If you don't use the MASK, the button will be a rectangular owner-draw control. The default transparent color is RGB(255,255,255).
  5. In the window initialization add the CxSkinButton methods:
    BOOL CxSkinButtonDemoDlg::OnInitDialog()
    //    ...
    //    ...

    The SetTextColor and SetToolTipText are self explaining; SetSkin is:

    void CxSkinButton::SetSkin(UINT normal,UINT down,UINT over, 
                               UINT disabled, UINT focus,UINT mask, 
                               short drawmode, short border, 
                               short margin)
    • normal, down, over, disabled, focus, mask = bitmap resource IDs
    • drawmode = if mask is not NULL, this should be 0 (normal); else you can try 1 (stretch) or 2 (tiled).
    • border = if mask is not NULL or is not rectangular, this should be 0; else you can try 1 to draw the standard 3D border around the button.
    • margin = if mask is not NULL or is not rectangular, this should be 0; else you can try to change this value to draw a dotted rectangle over the button when the control has the focus.


  • The BS_OWNERDRAW style is added automatically, you don't need to set the "Owner draw" property in the resource editor.
  • only NORMAL and DOWN bitmaps are essential; OVER bitmap is a plus.
  • DISABLED bitmap is not necessary if the button is always enabled. You can also use the button text (automatically etched) to show the disabled state.
  • MASK bitmap is not necessary if the button is rectangular.
  • for rectangular buttons you should set the margin parameter greater than 0 (4 is a good choice), or use the FOCUS bitmap.
  • for rectangular buttons you can use 'flat' bitmaps and set the border parameter to 1. In this situation, NORMAL and DOWN bitmaps can also share the same resource.

Release History

v1.00 - 15/03/2001
- basic implementation and interface.
v1.10 - 28/03/2001
enhanced mouse tracking implementation (by Milan Gardian).
- SetCapture removed: accelerators now are dispatched.
- Double-click handling.
- Better behavior during "button-pressed" (mouse button down & holding) phase
v1.20 - 02/04/2001
- New CreateRgnFromBitmap() implementation. Mask bitmap now works in any display color mode, any mask color depth and any mask size.
v1.21 - 14/04/2001
- OnMouseLeave cast error fixed.
- Fixed FocusRect defect when OVER bitmap was NULL.
v1.30 - 25/04/2001
- Fixed CreateRgnFromBitmap failure under WinNT4.0
v1.40 - 29/06/2001
- check & radio button add on.
- added "focus" bitmap.
- fixed CreateRgnFromBitmap bug.
- fixed shortcut bug.
- fixed text drawing code.
v1.41 - 27/10/2001
- Fixed memory leakage in CreateRgnFromBitmap
v1.50 - 07/07/2008
- fixed memory leaks using SelectObject and GetDC (thanks to sachelis and Bernd Giesen)
- added SetToolTipColor (thanks to Mykel)


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

Thanks to all the Code Project developers!
Special thanks to:
Milan Gardian for mouse and keyboard tracking code.
Fable(at)aramszu(dot)net for ExtCreateRegion replacement code.
Rainer Mangold for radio-button and check-box code.
Andy Green and Cameron Epp for suggestions & debugging.


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

Comments and Discussions

QuestionTab order of Button is not working PinmemberKUMARRaju15-Nov-13 6:01 
Questionhow transform the button in a Dialog with bitmap background Pinmemberwjcsj6-Jul-12 8:27 
QuestionBackground is not transparent Pinmemberbillqu_developer6-Jul-11 18:49 
GeneralButtons dont work properly in stretch drawing mode Pinmemberquyps27-Apr-11 21:39 
GeneralMemory leak in CxSkinButton::CreateRgnFromBitmap PinmembertHeWiZaRdOfDoS3-Dec-10 23:22 
QuestionCreating a CxSkinButton on the fly? Pinmemberanuj.saluja3-Sep-10 22:48 
AnswerRe: Creating a CxSkinButton on the fly? Pinmemberanuj.saluja10-Sep-10 10:47 
GeneralThe dialog does not display PinmemberJRCooper25-Jun-10 19:08 
QuestionCan only add one button. PLS HELP PinmemberAlexGvG8-May-10 8:32 
GeneralSetSkin BUG Pinmembershaino20-Mar-09 20:58 
General[Message Removed] PinmemberKatekortez25-Oct-08 10:12 
Generalwelcome back PinmemberTeashirt29-Jul-08 18:33 
Questionwhen using xSkinButton in MFC regular dll can not auto change state ! Pinmemberarler1-Nov-07 22:20 
Questionhow can add icon in bitmap? Pinmembershenhua8829-May-07 16:07 
QuestionLicense to use? PinmemberSRKarthik21-May-07 1:20 
GeneralMask Not Working Pinmembersiddharthfunda29-Mar-07 22:26 
GeneralNeed nIDCtl Pinmemberblackbox797-Jan-06 20:44 
GeneralCxSkinButton does not draw text PinsussStanislav Khatsko10-Nov-04 17:27 
GeneralCxSkinButton does not draw text Pinmemberpremiumclock10-Nov-04 17:27 
GeneralLeak in CreateRgnFromBitmap Pinmembersachelis4-May-04 5:24 
GeneralRe: Leak in CreateRgnFromBitmap Pinmembersachelis4-May-04 5:46 
GeneralRe: Leak in CreateRgnFromBitmap [modified] PinmemberBernd Giesen2-Jul-04 2:12 
GeneralFlaw in DrawItem() method PinmemberBernd Giesen7-Apr-04 1:50 
GeneralLoading image from disk PinmemberWolfSupernova12-Nov-03 7:02 
GeneralRe: Loading image from disk PinmemberDavide Pizzolato12-Nov-03 10:08 
GeneralI want to know the event of when it swaps mouseon with mouseout images Pinsusssdfdsfa7-Oct-03 19:55 
GeneralRe: I want to know the event of when it swaps mouseon with mouseout images PinmemberDavide Pizzolato8-Oct-03 9:56 
Generalhide buttons Pinmemberkaber29-Aug-03 22:52 
GeneralRe: hide buttons Pinmemberkaber29-Aug-03 22:55 
Generalremove and add Pinmemberkaber29-Aug-03 22:52 
GeneralCToolTipCtrl has a possible issue Pinmemberscully1216-Jun-03 11:25 
GeneralRe: CToolTipCtrl has a possible issue PinmemberBernd Giesen6-Apr-04 3:13 
GeneralRe: CToolTipCtrl has a possible issue Pinmemberscully126-Apr-04 3:19 
GeneralRe: CToolTipCtrl has a possible issue Pinmembereastar092117-Aug-04 20:32 
GeneralSet tooltip color PinmemberMykel16-Jun-03 3:13 
GeneralRe: Set tooltip color PinmemberDavide Pizzolato16-Jun-03 10:24 
GeneralRe: Set tooltip color PinmemberMykel17-Jun-03 10:11 
GeneralMultiple line support added Pinmemberrobkent2-Jun-03 4:44 
GeneralRe: Multiple line support added PinmemberPeggy Van Lierde11-Jun-03 5:00 
Generalenabling WS_CLIPCHILDREN causes bad drawing of masked buttons Pinmemberohadp21-May-03 22:05 
GeneralAdding a popup menu which belongs to the button caused error. PinsussVC user4-Mar-03 17:54 
GeneralSelectClipRgn error Pinmemberwangtao5826-Feb-03 20:53 
QuestionHow can I use a bitmap bigger than that used in your code PinmemberAberSabel9-Jan-03 17:40 
GeneralMemory leak in DrawBitmap PinsussAnonymous12-Oct-02 8:39 
GeneralRe: Memory leak in DrawBitmap PinmemberDavide Pizzolato12-Oct-02 21:47 
GeneralCxSkinButton type doesn't shows to chose member variable PinmemberLinda18-Jun-02 9:23 
GeneralRe: CxSkinButton type doesn't shows to chose member variable PinmemberDavide Pizzolato18-Jun-02 10:43 
GeneralRe: CxSkinButton type doesn't shows to chose member variable PinmemberLinda18-Jun-02 21:50 
GeneralRe: CxSkinButton type doesn't shows to chose member variable PinmemberDavide Pizzolato19-Jun-02 9:33 
GeneralRe: CxSkinButton type doesn't shows to chose member variable PinmemberLenda23-Jun-02 4:45 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 9 Jul 2008
Article Copyright 2001 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid