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// 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 // the button. Can be NULL.// If this parameter is the special value BTNST_AUTO_GRAY (cast to int) // the second icon will be automatically created starting from nIconIn // and converted to grayscale.// If this parameter is the special value BTNST_AUTO_DARKER (cast // to int) the second icon will be automatically created 25% // 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,
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 // button.// Pass NULL to remove any icon from the button.// [IN] nIconOut// ID number of the icon resource to show when the mouse is // outside the button. Can be NULL.// If this parameter is the special value BTNST_AUTO_GRAY (cast to int) // the second icon will be automatically created starting from // nIconIn and converted to grayscale. If this parameter is the // 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. // Can be NULL.// If this parameter is the special value BTNST_AUTO_GRAY the second// icon will be automatically created starting from hIconIn and // 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 // 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 // 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,
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,
HBITMAP hBitmapOut = NULL, COLORREF crTransColorOut = 0)

SetFlat

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

SetAlign

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

SetPressedStyle

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

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

GetColor

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

OffsetColor

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

SetAlwaysTrack

Sets the highlight logic for the button

Applies only to flat buttons

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

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

SetTooltipText

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

EnableBalloonTooltip

Enables the tooltip to be displayed using the balloon style

This function must be called before any call to SetTooltipText is made

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

SetMenu

Associates 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 // 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 // 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)

SetMenuCallback

Sets the callback message that will be sent to the specified 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 // 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 // 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)

SizeToContent

Resizes 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()

SetSound

Sets 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, // 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 // be loaded.// This parameter must be NULL unless lpszSound specifies a // 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 // the button.// [IN] bPlayAsync// TRUE if the sound must be played asynchronously.// FALSE if the sound must be played synchronously. The // 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)

OnDrawBackground

This 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 classes to produce a whole range of buttons not available by default.

When 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 speed up the graphic operations.

v2.1

Support for two icons

Modified SetIcon member

Added SetShowText/GetShowText members

Fixed a bug dealing with the left mouse button

Little optimizations

v2.0

Changed the class name for name convention

Support for 256 colors icons

Removed a stupid memory leak!

Removed support for CImagelists

Documentation in HTML format

ST_CButton v1.1

Some minor changes

ST_CButton v1.0

First release

Remarks

The demo application shows nearly all the features of the CButtonST class.

CButtonST architecture makes it 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 find and fix 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. A list of licenses authors might use can be found here.

Comments and Discussions

CButtonST has a method called SetFlat. You could use it to make all you buttons as default. Further more, you'll find in CButtonST constructor a variable called m_bIsFlat who should be set to FALSE and it will affect all your CButton's derived classes. Good luck!

i intialised buttons with color and icon on that button in OnIntilaUpdate()
function.

at first running the application the icons and colors are shown. I changed that view with some other view. Now i r eplace the olde view but the icons and colors are not shown . Now what to do show always icon and color
Can u explain PLZ?

be commented out, since the tab control behaves strange if it has button style, e.g.:
make the About tab active and try to activate any of the other 4 tabs. The extepcted behavior is to activate the selected tab and replace the visible dialog. What happens, however, is that the visible dialog is replaced but the new tab does not become selected.

I would like to use your buttons for some apps, but I only get as far as including the header file, vreating a CButtonST object and then I get my error messages.
Am I missing something essential (looks like I'm dump ;-()?
I'd be thankful for a small hint,

Hi,
I have used the CButtonST class in one application with large-sized and colured buttons.
With this kind of buttons is visible a colour flickering when the mouse is passing over the button.
Someone known in wich manner can I remove this effect?

I am trying to use check box in a DialogBar, but when I click on the button it does not stay checked. I looked the comment archive twice but I am not able to find an appropriate answer.

Now the details:
- I am using InitDialogBar, and adding the checkboxes to my DialogBar with resource editor.
- In OnInitDialogBar() I use SubclassDlgItem(). (this is called more than once so I use a boolean variable to be sure that they are called only once)
- Then I set icons, colors etc.

but the button does not stay checked.
Thank you for your answers, I really need them..

it makes it so you can set the 'icon' property in the VC resource editor, and display an image only button that still has an underlying title (this is useful if you want a hotkey associated with the button, but don't want to display the text).

If i do modify TOOLBAR or put new bitmap on toolbar it dosent displays on menu, whats wrong. or How can i use BCMENU for putting images???. Even if i make changes in ur code, or bitmap it fails to show us the images in menu

I use transparent buttons on the dialog with background (CBkDialogST), which is the main window in dialog-based application. My application is heavy enough and it takes some time to start it up. If, after starting exe-file, I switch to another window it, obviously, will get smaller z-order then program beeing started, so it will overlap it. As a result transparent buttons will get their copy background from another window. It could be partly solved if change PaintBk member like this