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

CXPStyleButtonST v1.2

By , 21 Jan 2002
 

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

About the Author

Davide Calabro
Web Developer
Italy Italy
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questioncan the button use 16x16 icon? sometimes 32x32 seems so big...memberlanmanck14 Jan '09 - 2:32 
thanks
Generalgreat ! thank you!memberlanmanck12 Jan '09 - 3:58 
dfsdf
QuestionHow can use this class in Property Sheet ?member"_$h@nky_"14 Sep '08 - 19:38 
Hi , first of all thanks its a fantastic class.
 
i want to know that how can i use this class in property sheet.
 
thanks in advance.
 
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

QuestionTooltips on buttonsmemberandrewtruckle26 Jun '08 - 10:45 
When my dialogue first displays and I hover the mouse over my 3 XP buttons they show tool tips.
 
But once I have clicked a button the tooltip functionality stops. Even if I manually set the focus to another control the XP buttons won't show tips anymore.
GeneralBCMenu and Themesmemberandrewtruckle25 May '08 - 12:25 
Since this enhancement to CButtonST is now Theme aware, can't the same be done with the BCMenu that is also used by CButtonST and this enhanced class?
GeneralThanks! Great class!memberandrewtruckle25 May '08 - 6:56 
Thanks for this great extension to CButtonST. Just what I wanted!
 
Andy
GeneralChanging BG coloursmemberAlex Evans28 Jun '05 - 12:10 
Hello there
 
Any idea of how / if at all I can change the Bg colours of the button?
I need this in a dialog that has many buttons, and I want some of them to be painted, green, some others in blue etc...
 
And I still want the nice rounded corners and icons...
 
Thanks for any idea in advance
Alex
GeneralRe: Changing BG coloursmemberMoak16 Apr '07 - 11:30 
First I wondered where to use buttons with colored background, then I found that there is a use: For example the Windows Explorer uses themed buttons with bright background for "checked" buttons... e.g. click on 'Search' or 'Folders' and the user can see easily which mode is activated.
 
This functionality was available in the old class CButtonST with the method SetColor(), however there is no support in the themed variant CXPStyleButtonST. I found no quick solution how to achieve the same effect as seen in Windows Explorer (a bright pressed button seams not to be part of a theme). Could be a feature request for Davide. Smile | :)
GeneralComplimentimemberRazor...10 Feb '04 - 4:02 
Per una volta un messaggio in italiano!
Complimenti Davide, soprattutto per la semplicità d'integrazione in una dialog!
Ne ho provati tanti altri di controlli, ma molti davano dei problemi, o bisognava fare delle manipolazioni "strambe"
Bene, saluti dalla Svizzera!
 
PS:Solo una cosa, per farlo funzionare in VC7, ho dovunto aggiungere "#include " perchè non trovava la definizione di CToolTipCtrl. Risultato il Tooltip non funziona...
 
Claudio

GeneralDisabled button icons look wrongsussAnonymous24 Nov '03 - 6:42 
Hi! I have gone with this as it is really great codeSmile | :) I have a problem thought that when I disable a button the Icons got to really simple greyscale that is much less than 256 colours. I think it is only 1 bit greyscale even! I am using windows XP but my app will be distributed on all window os's.
 
Is anyone else getting this problem?
 
Cheers
Dom
 
p.s. XP themed buttons drawn as a toolbar can have an extra state (I put in PBS_SELECTED) of 6 which is the look of a button that is down. (i.e. if you select favorites from the IE toolbar. Buttons not drawn as a toolbar do not have this state thoughFrown | :( Does anyone know how to get a similar effect, and also if in non themed apps it would be possible to draw the button sunken to illustrate it as selected?
 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 22 Jan 2002
Article Copyright 2002 by Davide Calabro
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid