How to integrate CButtonST in your application

In your project include the following files:

BtnST.h

BtnST.cpp

Starting from version 3.5, CButtonST now supports menus created using the BCMenu class.This 3rd party class gives you the ability to show menus using the most recent visual styles as seenon the latest Microsoft products or even in Windows XP.Latest BCMenu version can be found here.

To enable support for BCMenu the following two lines in BtnST.h must be enabled:

#define BTNST_USE_BCMENU
#include "BCMenu.h"

Also, the following files must be included in your project:

BCMenu.h

BCMenu.cpp

Note: please note that when BCMenu support is enabled the parameters accepted by the SetMenu method are different!

Starting from version 3.6, CButtonST can play sounds on particular button states.

To enable support for sound the following line in BtnST.h must be enabled:

#define BTNST_USE_SOUND

This gives access to the SetSound method.

Create a CButtonST object staticallyWith 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:

CButtonST m_btnOk;

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

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

SetIcon (using multi-size resources)Assigns icons to the button.Any previous icon or bitmap will be removed.

// Parameters:// [IN] nIconIn// ID number of the icon resource to show when the mouse is over the<BR>// button. Pass NULL to remove any icon from the button.// [IN] nCxDesiredIn// Specifies the width, in pixels, of the icon to load.// [IN] nCyDesiredIn// Specifies the height, in pixels, of the icon to load.// [IN] nIconOut// ID number of the icon resource to show when the mouse is outside <BR>// the button. Can be NULL.// If this parameter is the special value BTNST_AUTO_GRAY (cast to int) <BR>// the second icon will be automatically created starting from nIconIn <BR>// and converted to grayscale.// If this parameter is the special value BTNST_AUTO_DARKER (cast <BR>// to int) the second icon will be automatically created 25% <BR>// darker starting from nIconIn.// [IN] nCxDesiredOut// Specifies the width, in pixels, of the icon to load.// [IN] nCyDesiredOut// Specifies the height, in pixels, of the icon to load.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDRESOURCE// Failed loading the specified resource.//
DWORD SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, <BR> int nIconOut = NULL, int nCxDesiredOut = 0, int nCyDesiredOut = 0)

SetIcon (using resources)Assigns icons to the button.Any previous icon or bitmap will be removed.

// Parameters:// [IN] nIconIn// ID number of the icon resource to show when the mouse is over the <BR>// button.// Pass NULL to remove any icon from the button.// [IN] nIconOut// ID number of the icon resource to show when the mouse is <BR>// outside the button. Can be NULL.// If this parameter is the special value BTNST_AUTO_GRAY (cast to int) <BR>// the second icon will be automatically created starting from <BR>// nIconIn and converted to grayscale. If this parameter is the <BR>// special value BTNST_AUTO_DARKER (cast to int) the second// icon will be automatically created 25% darker starting from nIconIn.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDRESOURCE// Failed loading the specified resource.//
DWORD SetIcon(int nIconIn, int nIconOut = NULL)

SetIcon (using handles)Assigns icons to the button.Any previous icon or bitmap will be removed.

// Parameters:// [IN] hIconIn// Handle fo the icon to show when the mouse is over the button.// Pass NULL to remove any icon from the button.// [IN] hIconOut// Handle to the icon to show when the mouse is outside the button. <BR>// Can be NULL.// If this parameter is the special value BTNST_AUTO_GRAY the second// icon will be automatically created starting from hIconIn and <BR>// converted to grayscale.// If this parameter is the special value BTNST_AUTO_DARKER the second// icon will be automatically created 25% darker starting from hIconIn.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDRESOURCE// Failed loading the specified resource.//
DWORD SetIcon(HICON hIconIn, HICON hIconOut = NULL)

SetBitmaps (using resources)Assigns bitmaps to the button.Any previous icon or bitmap will be removed.

// Parameters:// [IN] nBitmapIn// ID number of the bitmap resource to show when the mouse is <BR>// over the button.// Pass NULL to remove any bitmap from the button.// [IN] crTransColorIn// Color (inside nBitmapIn) to be used as transparent color.// [IN] nBitmapOut// ID number of the bitmap resource to show when the mouse <BR>// is outside the button.// Can be NULL.// [IN] crTransColorOut// Color (inside nBitmapOut) to be used as transparent color.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDRESOURCE// Failed loading the specified resource.// BTNST_FAILEDMASK// Failed creating mask bitmap.//
DWORD SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, <BR> int nBitmapOut = NULL, COLORREF crTransColorOut = 0)

SetBitmaps (using handles)Assigns bitmaps to the button.Any previous icon or bitmap will be removed.

// Parameters:// [IN] hBitmapIn// Handle fo the bitmap to show when the mouse is over the button.// Pass NULL to remove any bitmap from the button.// [IN] crTransColorIn// Color (inside hBitmapIn) to be used as transparent color.// [IN] hBitmapOut// Handle to the bitmap to show when the mouse is outside the button.// Can be NULL.// [IN] crTransColorOut// Color (inside hBitmapOut) to be used as transparent color.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDRESOURCE// Failed loading the specified resource.// BTNST_FAILEDMASK// Failed creating mask bitmap.//
DWORD SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, <BR> HBITMAP hBitmapOut = NULL, COLORREF crTransColorOut = 0)

SetFlatSets the button to have a standard or flat style.

// Parameters:// [IN] bFlat// If TRUE the button will have a flat style, else// will have a standard style.// By default, CButtonST buttons are flat.// [IN] bRepaint// If TRUE the control will be repainted.//// Return value:// BTNST_OK// Function executed successfully.//
DWORD SetFlat(BOOL bFlat = TRUE, BOOL bRepaint = TRUE)

SetAlignSets the alignment type between icon/bitmap and text.

// Parameters:// [IN] byAlign// Alignment type. Can be one of the following values:// ST_ALIGN_HORIZ Icon/bitmap on the left, text on the right// ST_ALIGN_VERT Icon/bitmap on the top, text on the bottom// ST_ALIGN_HORIZ_RIGHT Icon/bitmap on the right, text on the left// ST_ALIGN_OVERLAP Icon/bitmap on the same space as text// By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.// [IN] bRepaint// If TRUE the control will be repainted.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDALIGN// Alignment type not supported.//
DWORD SetAlign(BYTE byAlign, BOOL bRepaint = TRUE)

SetPressedStyleSets the pressed style.

// Parameters:// [IN] byStyle// Pressed style. Can be one of the following values:// BTNST_PRESSED_LEFTRIGHT Pressed style from left to right (as usual)// BTNST_PRESSED_TOPBOTTOM Pressed style from top to bottom// By default, CButtonST buttons have BTNST_PRESSED_LEFTRIGHT style.// [IN] bRepaint// If TRUE the control will be repainted.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDPRESSEDSTYLE// Pressed style not supported.//
DWORD SetPressedStyle(BYTE byStyle, BOOL bRepaint = TRUE)

SetCheckSets the state of the checkbox.If the button is not a checkbox, this function has no meaning.

// Parameters:// [IN] byColorIndex// Index of the color to set. Can be one of the following values:// BTNST_COLOR_BK_IN Background color when mouse is over the button// BTNST_COLOR_FG_IN Text color when mouse is over the button// BTNST_COLOR_BK_OUT Background color when mouse is outside the button// BTNST_COLOR_FG_OUT Text color when mouse is outside the button// BTNST_COLOR_BK_FOCUS Background color when the button is focused// BTNST_COLOR_FG_FOCUS Text color when the button is focused// [IN] crColor// New color.// [IN] bRepaint// If TRUE the control will be repainted.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDINDEX// Invalid color index.//
DWORD SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint = TRUE)

GetColorReturns the color used for a particular state.

// Parameters:// [IN] byColorIndex// Index of the color to get.// See SetColor for the list of available colors.// [OUT] crpColor// A pointer to a COLORREF that will receive the color.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDINDEX// Invalid color index.//
DWORD GetColor(BYTE byColorIndex, COLORREF* crpColor)

OffsetColorThis function applies an offset to the RGB components of the specified color.This function can be seen as an easy way to make a color darker or lighter than its default value.

// Parameters:// [IN] byColorIndex// Index of the color to set.// See SetColor for the list of available colors.// [IN] shOffsetColor// A short value indicating the offset to apply to the color.// This value must be between -255 and 255.// [IN] bRepaint// If TRUE the control will be repainted.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDINDEX// Invalid color index.// BTNST_BADPARAM// The specified offset is out of range.//
DWORD OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint = TRUE)

SetAlwaysTrackSets the hilight logic for the button.Applies only to flat buttons.

// Parameters:// [IN] bAlwaysTrack// If TRUE the button will be hilighted even if the window that owns it, is// not the active window.// If FALSE the button will be hilighted only if the window that owns it,// is the active window.//// Return value:// BTNST_OK// Function executed successfully.//
DWORD SetAlwaysTrack(BOOL bAlwaysTrack = TRUE)

SetBtnCursorSets the cursor to be used when the mouse is over the button.

SetTooltipText (Using resource)Sets the text to show in the button tooltip.

// Parameters:// [IN] nText// ID number of the string resource containing the text to show.// [IN] bActivate// If TRUE the tooltip will be created active.//void SetTooltipText(int nText, BOOL bActivate = TRUE)

SetTooltipTextSets the text to show in the button tooltip.

// Parameters:// [IN] lpszText// Pointer to a null-terminated string containing the text to show.// [IN] bActivate// If TRUE the tooltip will be created active.//void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE)

EnableBalloonTooltipEnables the tooltip to be displayed using the balloon style.This function must be called before any call to SetTooltipText is made.

// Return value:// TRUE// The button is the default button.// FALSE// The button is not the default button.//
BOOL GetDefault()

DrawTransparentEnables the transparent mode.Note: this operation is not reversible.DrawTransparent should be called just after the button is created.Do not use trasparent buttons until you really need it (you have a bitmappedbackground) since each transparent button makes a copy in memory of its background.This may bring unnecessary memory use and execution overload.

// Parameters:// [IN] bRepaint// If TRUE the control will be repainted.//void DrawTransparent(BOOL bRepaint = FALSE)

SetMenuAssociates a menu to the button.The menu will be displayed clicking the button.This method is available only if BTNST_USE_BCMENU is not defined.

// Parameters:// [IN] nMenu// ID number of the menu resource.// Pass NULL to remove any menu from the button.// [IN] hParentWnd// Handle to the window that owns the menu.// This window receives all messages from the menu.// [IN] bRepaint// If TRUE the control will be repainted.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDRESOURCE// Failed loading the specified resource.//
DWORD SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint = TRUE)

SetMenuAssociates a menu to the button.The menu will be displayed clicking the button.This method is available only if BTNST_USE_BCMENU is defined. The menu will be handled by the BCMenu class.

// Parameters:// [IN] nMenu// ID number of the menu resource.// Pass NULL to remove any menu from the button.// [IN] hParentWnd// Handle to the window that owns the menu.// This window receives all messages from the menu.// [IN] bWinXPStyle// If TRUE the menu will be displayed using the new Windows XP style.// If FALSE the menu will be displayed using the standard style.// [IN] nToolbarID// Resource ID of the toolbar to be associated to the menu.// [IN] sizeToolbarIcon// A CSize object indicating the size (in pixels) of each icon <BR>// into the toolbar.// All icons into the toolbar must have the same size.// [IN] crToolbarBk// A COLORREF value indicating the color to use as background <BR>// for the icons into the toolbar.// This color will be used as the "transparent" color.// [IN] bRepaint// If TRUE the control will be repainted.//// Return value:// BTNST_OK// Function executed successfully.// BTNST_INVALIDRESOURCE// Failed loading the specified resource.//
DWORD SetMenu(UINT nMenu,
HWND hParentWnd,
BOOL bWinXPStyle = TRUE,
UINT nToolbarID = NULL,
CSize sizeToolbarIcon = CSize(16, 16),
COLORREF crToolbarBk = RGB(255, 0, 255),
BOOL bRepaint = TRUE)

SetMenuCallbackSets the callback message that will be sent to thespecified window just before the menu associated to the button is displayed.

// Parameters:// [IN] hWnd// Handle of the window that will receive the callback message.// Pass NULL to remove any previously specified callback message.// [IN] nMessage// Callback message to send to window.// [IN] lParam// A 32 bits user specified value that will be passed to the <BR>// callback function.//// Remarks:// the callback function must be in the form:// LRESULT On_MenuCallback(WPARAM wParam, LPARAM lParam)// Where:// [IN] wParam// If support for BCMenu is enabled: a pointer to BCMenu// else a HMENU handle to the menu that is being to be <BR>// displayed.// [IN] lParam// The 32 bits user specified value.//// Return value:// BTNST_OK// Function executed successfully.//
DWORD SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam = 0)

SizeToContentResizes the button to the same size of the image.To get good results both the IN and OUT images should have the same size.

void SizeToContent()

SetSoundSets the sound that must be played on particular button states.This method is available only if BTNST_USE_SOUND is defined.

// Parameters:// [IN] lpszSound// A string that specifies the sound to play.// If hMod is NULL this string is interpreted as a filename, <BR>// else it is interpreted as a resource identifier.// Pass NULL to remove any previously specified sound.// [IN] hMod// Handle to the executable file that contains the resource to <BR>// be loaded.// This parameter must be NULL unless lpszSound specifies a <BR>// resource identifier.// [IN] bPlayOnClick// TRUE if the sound must be played when the button is clicked.// FALSE if the sound must be played when the mouse is moved over <BR>// the button.// [IN] bPlayAsync// TRUE if the sound must be played asynchronously.// FALSE if the sound must be played synchronously. The <BR>// application takes control after the sound is completely played.//// Return value:// BTNST_OK// Function executed successfully.//
DWORD SetSound(LPCTSTR lpszSound,
HMODULE hMod = NULL,
BOOL bPlayOnClick = FALSE,
BOOL bPlayAsync = TRUE)

OnDrawBackgroundThis function is called every time the button background needs to be painted.If the button is in transparent mode this function will NOT be called.This is a virtual function that can be rewritten in CButtonST-derived classesto produce a whole range of buttons not available by default.

OnDrawBorderThis function is called every time the button border needs to be painted.This is a virtual function that can be rewritten in CButtonST-derived classesto produce a whole range of buttons not available by default.

History

v3.9 (03/March/2003)Added support for Windows XP iconsAdded support for multi-size iconsAdded BTNST_AUTO_DARKER as a special value for second iconFixed the grayscale icon bug under Win9x/MeClass is now indipendent from TTS_BALLOON

v3.8 (25/November/2002)Added support for balloon tooltipsAdded EnableBalloonTooltip methodOnDrawBorder virtual method is now called also for non-flat buttonsOnDrawBackground and OnDrawBorder now receive a correct CRect* parameterFixed a little color bugCorrectly works under MFC 7.0

v3.6 (09/July/2002)Added SetMenuCallback method to give the abilityto modify the associated menu just before is it displayedAdded basic support for soundsAdded SetSound methodAdded ResizeToContent method

v3.5 (18/April/2002)Second icon can be automatically created in grayscaleAdded BTNST_AUTO_GRAY as a special value for second iconBitmap is draw disabled if the button is disabledAdded support for owner draw menus (using BCMenu class)Added an overloaded SetMenu method to support BCMenu classAdded OffsetColor methodAdded support for DDX_Check calls

v2.4Added support for tooltipsAdded SetTooltipText, ActivateTooltip membersThe "Double-click bug" should be fixed

v2.3The class should now work from within a DLLThe "Spacebar-Bug" should be fixedAdded RedrawWindow() as the last line of SetIcon memberThe focus rectangle is now the last thing drawnThe focus rectangle can now be drawn also for "flat" buttonsAdded SetFlatFocus, GetFlatFocus membersAdded SetBtnCursor memberFlat buttons can now work like in IE

v2.2Removed SubclassDlgItem member (this is transparent for the user)Added PreSubclassWindow member (this allows DDX_ calls)Added SetDefaultActiveFgColor, SetActiveFgColor, GetActiveFgColor membersAdded SetDefaultActiveBgColor, SetActiveBgColor, GetActiveBgColor membersAdded SetDefaultInactiveFgColor, SetInactiveFgColor, GetInactiveFgColor membersAdded SetDefaultInactiveBgColor, SetInactiveBgColor, GetInactiveBgColor membersWhen the mouse is over a button the focus now remains to the control that owns it!The flat buttons now work properly also in windows not derived from CDialog!A memory DC (CMemDC) is used to draw the button. This should speeds up the graphic operations.

v2.1Support for two iconsModified SetIcon memberAdded SetShowText/GetShowText membersFixed a bug dealing with the left mouse buttonLittle optimizations

v2.0Changed the class name for name conventionSupport for 256 colors iconsRemoved a stupid memory leak!Removed support for CImagelistsDocumentation in HTML format

ST_CButton v1.1Some minor changes

ST_CButton v1.0First release

Remarks

The demo application shows nearly all the features of the CButtonST class.CButtonST architecture makes possible to produce a whole range of buttons not available by default. If someone implements new button styles I will be happy to include his code in the next CButtonST demo application.

Thanks

Thanks very much to the dozens of users that are using CButtonST in their applications.Thanks also to all the people that finds and fixes bugs. Thanks!If possible, please send a screenshot of your application where CButtonST is used. These screenshots will be collected for personal interest.

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.

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.

have read your "a standard glass button with image " article and it was realy nice and helpful.
I am trying to develop a GUI for my application like Regedit software -GUI.
In Regedit GUI i saw some command buttons which is 3D ,double colored and also looking very professional.
I like to use the same kind of command button in my application.Is it possible for you to develop one sample demo project which has the command buttons looking as it is in Regedit GUI?

// Lifetime of Graphic Objects
// The graphic object returned bySelectObject is “temporary.” That is, it will be deleted by
// theOnIdle member function of class CWinApp the next time the program gets idle time.
// As long as you use the object returned by SelectObject in a single function
// without returning control to the main message loop, you will have no problem.

This is a really helpful class. Many thanks, it's saved me a load of time.
I have a question regarding the tool tips - in that I can't get them to work . I'm using VC2008 on Vista and the buttons are in an activeX control. Any thoughts as to why they might not work? The code I'm using is below.

Q:
Currently when mouse moves over a button that button can react to the mouse over it changing icon or background.
I wonder how to produce the following effect: When mouse moves over the one button the background and/or icon is changed on a few other buttons.
Any hints would be greatly appreciated!
Regards,
Samuel
P.S.
Great Demo! Thank you Davide!

I have tried a fix on that thread which have proved not working. After a couple of reboots (my program runs at startup and starts minimizes...that's when the problem randomly occurs)I found the problem inside BitBlt. it fails randomly with a value of "Invalid handle" which I suppose that is the handle of its parent DC; I suppose this could happen if the main window goes hidden between

this is the same as declaring m_dcBk and m_bmpBk inside PaintBk, those vars are used only there so there is no reason to declare them as class members...only if we want to speed painting...aka avoiding constructors/destructurs on each paint call; so you've found that my previous fixes didn't worked?

Moved to windows 7. I found the problem still persist here. Found a workaround. On the parent holding my controls, I have the background dynamically painted inside OnEraseBkgnd(CDC* pDC). I've put a Sleep(300) after the first painting of the background (only once). In this way I let time to the others windows covering my window to go in the background so that I can catch my background clear for the buttons' initialization.

I've been using this class for some time and recently noticed that the focus rectangle always appears, even though this is not the default behavior of the standard buttons - their focus rectangle appears only when you start pressing tab. Is there any way to add a check if tab has been used to set the focus, and only then to display the focus rectangle?