Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

CXPStyleButtonST v1.2

Rate me:
Please Sign up or sign in to vote.
4.77/5 (27 votes)
21 Jan 20023 min read 247.8K   11K   102   26
Windows XP buttons with all CButtonST features

Sample Image Sample Image

SoftechSoftware homepage
SoftechSoftware Email

Introduction

CXPStyleButtonST is a scalable CButtonST-derived control. If running under Windows XP the application buttons will be skinned using the current selected theme (if any), while if running under Windows 9x/ME/NT/2000 the buttons will have the standard, well known CButtonST flat style. In both cases all CButtonST features are granted! Using CXPStyleButtonST - with a zero-cost implementation - your applications running under XP will have the new, smooth and elegant style but also will run error-less under old Windows versions.

Integrating CXPStyleButtonST into Your Application

In your project include the following files:

  • BtnST.h
  • BtnST.cpp
  • XPStyleButtonST.h
  • XPStyleButtonST.cpp
  • ThemeHelperST.h
  • ThemeHelperST.cpp

Create a instance of CThemeHelperST. This class encapsulates all the required APIs to access the current selected theme (if any) or to run error-less if none is selected or if running under a old Windows version. You need only one instance for all the buttons, so this can be global to the application or only to the current dialog.

CThemeHelperST m_ThemeHelper;

Creating a CXPStyleButtonST Object Statically

With dialog editor create a standard button called, for example, IDOK (you don't need to make it owner drawn) and create a member variable for this button:

CXPStyleButtonST m_btnOk;

Now attach the button to CXPStyleButtonST. For dialog-based applications, in your OnInitDialog:

// Call the base-class method
CDialog::OnInitDialog();

// Create the IDOK button
m_btnOk.SubclassDlgItem(IDOK, this);

Or in your DoDataExchange:

// Call the base method
CDialog::DoDataExchange(pDX);

// Create the IDOK button
DDX_Control(pDX, IDOK, m_btnOk);

Assign the CThemeHelperST instance to the button. This is fundamental or your button will not have the current theme style even if running under XP! In your OnInitDialog:

// Assign theme helper instance to the button
m_btnOk.SetThemeHelper(&m_ThemeHelper);

Creating a CXPStyleButtonST Object Dynamically

In your application, create a member variable for the button. Please note that this variable is a pointer:

CXPStyleButtonST* m_pbtnOk;

Now create the button. For dialog-based applications, in your OnInitDialog:

// Call the base-class method
CDialog::OnInitDialog();

// Create the IDOK button
m_pbtnOk = new CXPStyleButtonST;
m_pbtnOk->Create(_T("&Ok"), WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP,
	CRect(10, 10, 200, 100), this, IDOK);
// Set the same font of the application
m_pbtnOk->SetFont(GetFont());

Assign the CThemeHelperST instance to the button as described in the previous section. Remember to destroy the button or you will get a memory leak. This can be done, for example, in your class destructor:

if (m_pbtnOk) delete m_pbtnOk;

Class methods

SetThemeHelper
Assigns a CThemeHelperST instance to the button.

// Parameters:
//     [IN]   pTheme
//            Pointer to a CThemeHelperST instance.
//            Pass NULL to remove any previous instance.
//
void SetThemeHelper(CThemeHelperST* pTheme)

DrawAsToolbar
Sets if the button must be drawn using the flat toolbar-style.

// Parameters:
//     [IN]   bDrawAsToolbar
//            If TRUE the button will be drawn using the flat toolbar-style.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD DrawAsToolbar(BOOL bDrawAsToolbar, BOOL bRepaint = TRUE)

GetVersionI
Returns the class version as a short value.

// Return value:
//     Class version. Divide by 10 to get actual version.
//
static short GetVersionI()

GetVersionC
Returns the class version as a string value.

// Return value:
//     Pointer to a null-terminated string containing the class version.
//
static LPCTSTR GetVersionC()

Remarks

To compile CXPStyleButtonST you need the Platform SDK (August 2001 or newer). This is not mandatory because at compile-time, if not found, it will be emulated. At run-time you don't need any additional SDK or libraries installed.

History

  • v1.2 (24/January/2003)
    Added DrawAsToolbar method
    Made compatible with CButtonST v3.8
  • v1.1 (31/October/2002)
    Fixed a visual bug when the button was pressed
  • v1.0 (21/January/2002)
    First release

Credits

CThemeHelperST is based on the CVisualStylesXP code published by David Yuheng Zhao (yuheng_zhao@yahoo.com). Thanks very much!

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

Terms of use

THIS SOFTWARE IS FREE FOR PERSONAL USE OR FREEWARE APPLICATIONS. IF YOU USE THIS SOFTWARE IN COMMERCIAL OR SHAREWARE APPLICATIONS YOU ARE GENTLY ASKED TO DONATE 5$ (FIVE U.S. DOLLARS) TO THE AUTHOR:

Davide Calabro'
P.O. Box 65
21019 Somma Lombardo (VA)
Italy

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
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncan the button use 16x16 icon? sometimes 32x32 seems so big... Pin
Jason.LYJ14-Jan-09 2:32
professionalJason.LYJ14-Jan-09 2:32 
Generalgreat ! thank you! Pin
Jason.LYJ12-Jan-09 3:58
professionalJason.LYJ12-Jan-09 3:58 
QuestionHow can use this class in Property Sheet ? Pin
Le@rner14-Sep-08 19:38
Le@rner14-Sep-08 19:38 
QuestionTooltips on buttons Pin
andrewtruckle26-Jun-08 10:45
andrewtruckle26-Jun-08 10:45 
GeneralBCMenu and Themes Pin
andrewtruckle25-May-08 12:25
andrewtruckle25-May-08 12:25 
GeneralThanks! Great class! Pin
andrewtruckle25-May-08 6:56
andrewtruckle25-May-08 6:56 
GeneralChanging BG colours Pin
Alex Evans28-Jun-05 12:10
Alex Evans28-Jun-05 12:10 
GeneralRe: Changing BG colours Pin
Moak16-Apr-07 11:30
Moak16-Apr-07 11:30 
GeneralComplimenti Pin
Member 69538410-Feb-04 4:02
Member 69538410-Feb-04 4:02 
GeneralDisabled button icons look wrong Pin
Anonymous24-Nov-03 6:42
Anonymous24-Nov-03 6:42 
GeneralLegality of Icon use Pin
blindcop11-Nov-03 7:15
blindcop11-Nov-03 7:15 
QuestionWhy does this work for toolbar buttons? Pin
Hidde Wallaart23-Oct-03 5:04
Hidde Wallaart23-Oct-03 5:04 
QuestionHow to convert in an ocx? Pin
Anderson_Mesquita14-Mar-03 2:47
Anderson_Mesquita14-Mar-03 2:47 
Generalwhy doesn't work Pin
C·o·Y·o·T·e7-Mar-03 7:24
C·o·Y·o·T·e7-Mar-03 7:24 
QuestionCould it be done so it looks winxp in win98 ? Pin
joeriw26-Jan-03 11:38
joeriw26-Jan-03 11:38 
AnswerRe: Could it be done so it looks winxp in win98 ? Pin
Heath Stewart5-Feb-03 2:30
protectorHeath Stewart5-Feb-03 2:30 
GeneralRe: Could it be done so it looks winxp in win98 ? Pin
joeriw5-Feb-03 9:23
joeriw5-Feb-03 9:23 
GeneralDoesnt work with latest CBtnST Pin
joeriw23-Jan-03 19:41
joeriw23-Jan-03 19:41 
GeneralRe: Doesnt work with latest CBtnST Pin
Davide Calabro5-Feb-03 3:33
Davide Calabro5-Feb-03 3:33 
GeneralRe: Doesnt work with latest CBtnST Pin
joeriw5-Feb-03 9:19
joeriw5-Feb-03 9:19 
GeneralNo hovering effect when not in focus Pin
drc_slime19-Sep-02 5:49
drc_slime19-Sep-02 5:49 
GeneralVisual Bug Fix Pin
Andrew Welch25-Aug-02 20:20
Andrew Welch25-Aug-02 20:20 
GeneralRe: Visual Bug Fix Pin
Davide Calabro4-Nov-02 2:12
Davide Calabro4-Nov-02 2:12 
GeneralBig or Small problem with Overlay board Pin
13-Feb-02 21:05
suss13-Feb-02 21:05 
GeneralThanks a lot Pin
Nnamdi Onyeyiri23-Jan-02 9:43
Nnamdi Onyeyiri23-Jan-02 9:43 

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.