Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CButton with icon

0.00/5 (No votes)
12 Aug 2002 2  
A small class to create a button with an icon on it

Sample Image - iconbutton.jpg

Introduction

The idea was to have a small piece of code which makes it easy to create a button with an icon on it. I saw a lot of great code, but it was not exactly what I want. So I decided to create my own CIconButton-class. This class makes it easy to set an icon on a button.

  1. Simply add a button to your dialog.
  2. Set the button style "ownerdraw", otherwise the code doesn't work.
  3. Then generate a member variable with the class-wizard.
  4. Change the class name from CButton to CIconButton (make sure you have include the "iconbutton.h" file and add the "iconbutton.cpp" file to your project)
  5. Create an icon in the resource manager.
  6. Set the icon to the button, set the font, the font size, etc

Last version

It�s possible to give the button a tool tip text. You can do this by calling the function SetTipText ( "tooltip-text" );

For more details look in the demo-project

Methods

void SetIconID ( const UINT nID )	
void SetTextColor ( const COLORREF color )	
void SetItalic ( bool bVal = true )
void SetUnderline ( bool bVal = true )	
void SetStrikeOut ( bool bVal = true )	
void SetFaceName ( const CString &sVal )
void SetWeight ( const int nVal )	
void SetHeight ( const int nVal)
void SetWidth ( const int nVal)	
void SetIconSize ( const int x, const int y )	
void SetIconRight ( bool bVal = true ) 
void SetTipText ( const CString &sTxt )  new

void Disable ( void );
void Enable ( void );

Note

I think this is the last enhancement, otherwise the "small piece of code" is going to get a big hunk ;-))

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