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

CButton with icon

, 12 Aug 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
A small class to create a button with an icon on it

Sample Image - iconbutton.jpg


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


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


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander Kloep
Software Developer
Germany Germany
Working as software engineer since 2001. I started with c, c++ came up to c# and wpf today.

Comments and Discussions

GeneralHelp Alt+Key PinmemberAnonymous2-May-02 7:02 
GeneralCool Class but... PinmemberAnonymous15-Mar-02 7:23 
GeneralRe: Cool Class but... PinmemberCam16-Mar-02 9:45 
GeneralRe: Cool Class but... PinmemberAnonymous23-Mar-02 6:48 
Because SetIcon overrides the text you've set. This class combines the text AND icon on the button. Also, if your icon is too big for the button, it'll be resized, and not like with SetIcon clipped.
And for the author of this class, why are you passing constant value copies of things? like const int whatever. It's a bit useless... and you also override the default font that is set for the window... kinda annoying if ya set a certain font to use in the whole window and the button overrides it so ya need to reset it again... just use the default font unless the user overrides it.
GeneralRe: Cool Class but... PinmemberVince003-Jun-02 6:54 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 13 Aug 2002
Article Copyright 2002 by Alexander Kloep
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid