Click here to Skip to main content
11,495,371 members (64,503 online)
Click here to Skip to main content

CxSkinButton

, 9 Jul 2008 CPOL 481.8K 13.1K 162
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()
    {
    //    ...
        m_btn1.SetTextColor(RGB(255,0,0));
        m_btn1.SetToolTipText("Button1");
        m_btn1.SetSkin(IDB_B1NORM,IDB_B1DOWN,IDB_B1OVER,IDB_B1GRAY,
                           0,IDB_B1MASK,0,0,0);
    //    ...

    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.

Remarks

  • 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)

Compatibility

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.

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralUsing with ON_UPDATE_COMMAND_UI() Pin
Dana Holt4-Jun-02 16:50
memberDana Holt4-Jun-02 16:50 
Questionwho can explain this function to me? Pin
Anonymous24-Mar-02 18:44
memberAnonymous24-Mar-02 18:44 
GeneralOther problem Pin
seongho17-Dec-01 18:13
memberseongho17-Dec-01 18:13 
GeneralRe: Other problem Pin
Davide Pizzolato19-Dec-01 7:52
memberDavide Pizzolato19-Dec-01 7:52 
GeneralAnother correction Pin
Happy Bob19-Nov-01 6:25
memberHappy Bob19-Nov-01 6:25 
GeneralSmall correction Pin
Kostya Osypa6-Nov-01 5:34
memberKostya Osypa6-Nov-01 5:34 
GeneralRe: Small correction Pin
Davide Pizzolato6-Nov-01 8:21
memberDavide Pizzolato6-Nov-01 8:21 
GeneralGDI resource leakage fixed Pin
Davide Pizzolato27-Oct-01 8:12
memberDavide Pizzolato27-Oct-01 8:12 
Generalassertion failed error Pin
lakai24-Aug-01 15:04
memberlakai24-Aug-01 15:04 
GeneralWin 95 before IE 3 Pin
njc4-Aug-01 4:53
membernjc4-Aug-01 4:53 
GeneralRe: Win 95 before IE 3 Pin
Davide Pizzolato4-Aug-01 8:15
memberDavide Pizzolato4-Aug-01 8:15 
GeneralRe: Win 95 before IE 3 Pin
njc4-Aug-01 12:24
membernjc4-Aug-01 12:24 
GeneralRe: Win 95 before IE 3 Pin
Galito13-Mar-02 18:34
memberGalito13-Mar-02 18:34 
GeneralRe: Win 95 before IE 3 Pin
Davide Pizzolato13-Mar-02 21:34
memberDavide Pizzolato13-Mar-02 21:34 
GeneralTransparency Pin
Gilad Novik4-Jul-01 9:05
memberGilad Novik4-Jul-01 9:05 
GeneralRe: Transparency Pin
Mike_K16-May-03 1:48
memberMike_K16-May-03 1:48 
GeneralRe: Transparency Pin
Mike_K16-May-03 6:26
memberMike_K16-May-03 6:26 
GeneralA little improvement Pin
Gilad Novik4-Jul-01 8:59
memberGilad Novik4-Jul-01 8:59 
GeneralRe: A little improvement Pin
Thales6-Jul-01 13:44
memberThales6-Jul-01 13:44 
GeneralRe: A little improvement Pin
Gilad Novik7-Jul-01 21:29
memberGilad Novik7-Jul-01 21:29 
Generalmissing masks for "down" and "over" Pin
Peter Marino3-Jul-01 13:11
memberPeter Marino3-Jul-01 13:11 
General9 bitmaps! Pin
Davide Pizzolato4-Jul-01 8:41
memberDavide Pizzolato4-Jul-01 8:41 
GeneralGood work David Pin
Amit Dey6-Jun-01 2:02
memberAmit Dey6-Jun-01 2:02 
GeneralError...! Pin
rehan31-May-01 21:24
memberrehan31-May-01 21:24 
GeneralRe: Fixed in release 1.40 Pin
Davide Pizzolato4-Jun-01 2:30
memberDavide Pizzolato4-Jun-01 2:30 
QuestionHow drawn is the roundbutton? Pin
Anonymous28-May-01 0:46
memberAnonymous28-May-01 0:46 
QuestionHow drawn is the roundbutton? Pin
Anonymous28-May-01 0:44
memberAnonymous28-May-01 0:44 
QuestionWhat to do when buttons are dynamic ? - Please help! Pin
Robert K21-May-01 5:32
memberRobert K21-May-01 5:32 
AnswerRe: What to do when buttons are dynamic ? - Please help! Pin
Gilad Novik4-Jul-01 9:09
memberGilad Novik4-Jul-01 9:09 
GeneralAlmost Perfect Pin
Julien16-May-01 18:30
memberJulien16-May-01 18:30 
GeneralRe: Almost Perfect Pin
Davide Pizzolato17-May-01 2:46
memberDavide Pizzolato17-May-01 2:46 
GeneralCBitmapButton Pin
Bert8-May-01 12:46
memberBert8-May-01 12:46 
GeneralRe: CBitmapButton Pin
Davide Pizzolato9-May-01 10:54
memberDavide Pizzolato9-May-01 10:54 
GeneralClass Wizard & CxSkinButton from scratch Pin
Davide Pizzolato4-May-01 11:37
memberDavide Pizzolato4-May-01 11:37 
GeneralEmpty rectangle Pin
robert2-May-01 2:02
memberrobert2-May-01 2:02 
GeneralExcellent Work! :) Pin
Appreciater20-Mar-01 2:28
memberAppreciater20-Mar-01 2:28 

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.150520.1 | Last Updated 9 Jul 2008
Article Copyright 2001 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid