Click here to Skip to main content
11,569,923 members (54,751 online)
Click here to Skip to main content

CButton with icon

, 12 Aug 2002 CPOL 218.8K 5.8K 57
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

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

License

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionAbout Font Pin
NANA@12318-Mar-13 23:44
memberNANA@12318-Mar-13 23:44 
QuestionAwsome Work Pin
AhmedShinwari5-Feb-13 16:06
memberAhmedShinwari5-Feb-13 16:06 
QuestionCompile with VS2010 Pin
merano24-Oct-11 10:15
membermerano24-Oct-11 10:15 
AnswerRe: Compile with VS2010 Pin
Alexander Kloep24-Oct-11 19:56
memberAlexander Kloep24-Oct-11 19:56 
AnswerRe: Compile with VS2010 Pin
AHTOXA5-Jun-12 22:56
memberAHTOXA5-Jun-12 22:56 
GeneralThanks Pin
Member 108540218-Sep-10 9:34
memberMember 108540218-Sep-10 9:34 
QuestionCan I use this code in commercial program Pin
hp15619-Sep-09 16:57
memberhp15619-Sep-09 16:57 
AnswerRe: Can I use this code in commercial program [modified] Pin
Alexander Kloep20-Sep-09 4:42
memberAlexander Kloep20-Sep-09 4:42 
GeneralThanks Pin
nobihai26-Oct-08 4:25
membernobihai26-Oct-08 4:25 
GeneralRe: Thanks Pin
Alexander Kloep26-Oct-08 21:23
memberAlexander Kloep26-Oct-08 21:23 
GeneralChanging ToolTips Pin
RedFraggle10-Jun-06 12:03
memberRedFraggle10-Jun-06 12:03 
GeneralRe: Changing ToolTips Pin
Alexander Kloep11-Jun-06 20:44
memberAlexander Kloep11-Jun-06 20:44 
GeneralNice one Pin
Jaiju jacob9-Sep-05 0:14
memberJaiju jacob9-Sep-05 0:14 
Questionhow to make colorful button such as red ,blue not gray Pin
vividtang3-Apr-04 12:59
membervividtang3-Apr-04 12:59 
AnswerRe: how to make colorful button such as red ,blue not gray Pin
merano24-Oct-11 10:39
membermerano24-Oct-11 10:39 
GeneralQ Pin
deven chin15-Jan-04 1:43
memberdeven chin15-Jan-04 1:43 
GeneralNice, but click speed is poor Pin
C++ Hacker16-Nov-03 22:55
memberC++ Hacker16-Nov-03 22:55 
GeneralSize of class Pin
Lapompe25-Jun-03 15:00
memberLapompe25-Jun-03 15:00 
QuestionHasn't this been done before? Pin
Emcee Lam20-Dec-02 14:33
memberEmcee Lam20-Dec-02 14:33 
AnswerRe: Hasn't this been done before? Pin
Nishant S20-Dec-02 15:11
editorNishant S20-Dec-02 15:11 
GeneralRe: Hasn't this been done before? Pin
Emcee Lam20-Dec-02 16:35
memberEmcee Lam20-Dec-02 16:35 
GeneralRe: Hasn't this been done before? Pin
Rubyist8-Aug-07 21:51
memberRubyist8-Aug-07 21:51 
GeneralRe: Hasn't this been done before? Pin
Emcee Lam13-Aug-07 16:36
memberEmcee Lam13-Aug-07 16:36 
Generalgood Pin
yary19-Dec-02 1:48
memberyary19-Dec-02 1:48 
GeneralNice butt i got a problem Pin
DosK19-Nov-02 23:18
memberDosK19-Nov-02 23:18 
GeneralRe: Nice butt i got a problem Pin
DosK19-Nov-02 23:41
memberDosK19-Nov-02 23:41 
GeneralNice Class Pin
Anonymous9-Oct-02 9:47
sussAnonymous9-Oct-02 9:47 
GeneralCONTROL on text and icon Pin
Anonymous25-Jun-02 2:42
memberAnonymous25-Jun-02 2:42 
GeneralHelp Alt+Key Pin
Anonymous2-May-02 6:02
memberAnonymous2-May-02 6:02 
GeneralCool Class but... Pin
Anonymous15-Mar-02 6:23
memberAnonymous15-Mar-02 6:23 
GeneralRe: Cool Class but... Pin
Cam16-Mar-02 8:45
memberCam16-Mar-02 8:45 
Amen to that.

~Cam Desautels (BinaryUprising.com)
GeneralRe: Cool Class but... Pin
Anonymous23-Mar-02 5:48
memberAnonymous23-Mar-02 5:48 
GeneralRe: Cool Class but... Pin
Vince003-Jun-02 5:54
memberVince003-Jun-02 5: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
Web03 | 2.8.150624.2 | Last Updated 13 Aug 2002
Article Copyright 2002 by Alexander Kloep
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid