5,530,111 members and growing! (16,829 online)
Email Password   helpLost your password?
Desktop Development » Button Controls » Owner-draw buttons     Intermediate License: The Code Project Open License (CPOL)

CxSkinButton

By Davide Pizzolato

A skinned button class with clipping region, tooltip and mouse tracking
VC6, C++Windows, NT4, Win2K, MFC, VS6, Visual Studio, Dev

Posted: 19 Mar 2001
Updated: 9 Jul 2008
Views: 317,011
Bookmarked: 99 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
96 votes for this Article.
Popularity: 8.99 Rating: 4.54 out of 5
3 votes, 7.1%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
6 votes, 14.3%
4
33 votes, 78.6%
5

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)

About the Author

Davide Pizzolato


Supporter

Location: Italy Italy

Other popular Button Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 76 (Total in Forum: 76) (Refresh)FirstPrevNext
Subject  Author Date 
Generalwelcome backmemberTeashirt218:33 9 Jul '08  
Questionwhen using xSkinButton in MFC regular dll can not auto change state !memberarler22:20 1 Nov '07  
Questionhow can add icon in bitmap?membershenhua8816:07 29 May '07  
QuestionLicense to use?memberSRKarthik1:20 21 May '07  
GeneralMask Not Workingmembersiddharthfunda22:26 29 Mar '07  
GeneralTooltips not working in demomemberstecevsrf8:28 4 Jan '07  
GeneralNeed nIDCtlmemberblackbox7920:44 7 Jan '06  
GeneralCxSkinButton does not draw textsussStanislav Khatsko17:27 10 Nov '04  
GeneralCxSkinButton does not draw textmemberpremiumclock17:27 10 Nov '04  
GeneralLeak in CreateRgnFromBitmapmembersachelis5:24 4 May '04  
GeneralRe: Leak in CreateRgnFromBitmapmembersachelis5:46 4 May '04  
GeneralRe: Leak in CreateRgnFromBitmapmemberBernd Giesen2:12 2 Jul '04  
GeneralFlaw in DrawItem() methodmemberBernd Giesen1:50 7 Apr '04  
GeneralLoading image from diskmemberWolfSupernova7:02 12 Nov '03  
GeneralRe: Loading image from disksupporterDavide Pizzolato10:08 12 Nov '03  
GeneralI want to know the event of when it swaps mouseon with mouseout imagessusssdfdsfa19:55 7 Oct '03  
GeneralRe: I want to know the event of when it swaps mouseon with mouseout imagessupporterDavide Pizzolato9:56 8 Oct '03  
Generalhide buttonsmemberkaber22:52 29 Aug '03  
GeneralRe: hide buttonsmemberkaber22:55 29 Aug '03  
Generalremove and addmemberkaber22:52 29 Aug '03  
GeneralCToolTipCtrl has a possible issuememberscully1211:25 16 Jun '03  
GeneralRe: CToolTipCtrl has a possible issuememberBernd Giesen3:13 6 Apr '04  
GeneralRe: CToolTipCtrl has a possible issuememberscully123:19 6 Apr '04  
GeneralRe: CToolTipCtrl has a possible issuemembereastar092120:32 17 Aug '04  
GeneralSet tooltip colormemberMykel3:13 16 Jun '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jul 2008
Editor: Sean Ewington
Copyright 2001 by Davide Pizzolato
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project