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

QuestionAbout Font PinmemberNANA@12319-Mar-13 0:44 
QuestionAwsome Work PinmemberAhmedShinwari5-Feb-13 17:06 
QuestionCompile with VS2010 [modified] Pinmembermerano24-Oct-11 11:15 
AnswerRe: Compile with VS2010 PinmemberAlexander Kloep24-Oct-11 20:56 
AnswerRe: Compile with VS2010 PinmemberAHTOXA5-Jun-12 23:56 
GeneralThanks PinmemberMember 108540218-Sep-10 10:34 
QuestionCan I use this code in commercial program Pinmemberhp15619-Sep-09 17:57 
AnswerRe: Can I use this code in commercial program [modified] PinmemberAlexander Kloep20-Sep-09 5:42 
GeneralThanks Pinmembernobihai26-Oct-08 5:25 
GeneralRe: Thanks PinmemberAlexander Kloep26-Oct-08 22:23 
GeneralChanging ToolTips PinmemberRedFraggle10-Jun-06 13:03 
GeneralRe: Changing ToolTips PinmemberAlexander Kloep11-Jun-06 21:44 
GeneralNice one PinmemberJaiju jacob9-Sep-05 1:14 
Questionhow to make colorful button such as red ,blue not gray Pinmembervividtang3-Apr-04 13:59 
AnswerRe: how to make colorful button such as red ,blue not gray Pinmembermerano24-Oct-11 11:39 
GeneralQ Pinmemberdeven chin15-Jan-04 2:43 
GeneralNice, but click speed is poor PinmemberC++ Hacker16-Nov-03 23:55 
GeneralSize of class PinmemberLapompe25-Jun-03 16:00 
QuestionHasn't this been done before? PinmemberEmcee Lam20-Dec-02 15:33 
AnswerRe: Hasn't this been done before? PineditorNishant S20-Dec-02 16:11 
GeneralRe: Hasn't this been done before? PinmemberEmcee Lam20-Dec-02 17:35 
GeneralRe: Hasn't this been done before? PinmemberRubyist8-Aug-07 22:51 
Read "Getting Real" : Always underdo competition, go for less features.
But since you're doing MFC, I guess you don't mind useless bloat.
What am I doing, replying on CodeProject, sheesh
GeneralRe: Hasn't this been done before? PinmemberEmcee Lam13-Aug-07 17:36 
Generalgood Pinmemberyary19-Dec-02 2:48 
GeneralNice butt i got a problem PinmemberDosK20-Nov-02 0:18 

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
Web03 | 2.8.150327.1 | Last Updated 13 Aug 2002
Article Copyright 2002 by Alexander Kloep
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid