Click here to Skip to main content
15,883,901 members
Articles / Desktop Programming / MFC
Article

CImageTextButton

Rate me:
Please Sign up or sign in to vote.
3.44/5 (19 votes)
22 May 20043 min read 117K   3.6K   50   10
Bitmap button which embeds text on it.

Sample Image - CImageTextButton.jpg

Introduction

Hello guys... Are you looking for a special button?? Then take it :-). I'm Just kidding.. Now, coming to the point, the inspiration to upload this article is, since I didn't get proper solution from the net to show "Text" at a specified position of a bitmap button. I think you got the problem? Yes, I want to show some buttons which have to load some bitmaps for its up, down and disabled state.. and also want to show some text on it.... Remember, there is CBitmapButton, but this can load bitmaps only, can't show text on it. So, I decided to write a reusable class which does the bitmap loading if the bitmaps are given to the class, also provide some methods to give the needed text for the button. Then comes the coloring issue.. who will color the text, by which color the font of the text shows etc.. So, I decided to give methods to support such points.. And now, I.. no, you have a cool simple working code which can be easily incorporated to your workspace.

Features.

  • Loading bitmaps (up, down, disabled state; only up state bitmap is mandatory, others are optional) is possible.
  • Text position can be set in two different ways (can specify x,y position or like DT_CENTER|DT_VCENTER etc.).
  • Text font and font size can be configured for each button.
  • Text color for up, down and disabled can be configured.
  • Button size is resized to the size of the bitmap.

Now, have a look at CImageTextButton?

class CImageTextButton : public CButton
{
 void SetTextPosition(UINT uiPos );
 void SetTextPosition(int nXPos ,int nYPos);
 void SetTextColor(const COLORREF& clrUpDwn, const COLORREF& clrDisabled );
 void SetButtonText(const CString& strCaption);
 void SetButtonImg(UINT uiImageIDU,UINT uiImgIDD =0,UINT uiImgIDX = 0);
 void SetFont(CString srtFntName_i, int nSize_i);
}

Function Usage.

  • void SetButtonImg(UINT uiImageIDU,UINT uiImgIDD =0,UINT uiImgIDX = 0);

    Method used to set the bitmaps to be loaded for the button. The order of bitmaps is up-state bitmap, then down-state bitmap, and the last one is disabled bitmap.

  • void SetButtonText(const CString& strCaption);

    Method used to set the caption/text of the button, if the button wants to have any text. If this method is not called, only the bitmap is loaded.

  • void SetTextColor(const COLORREF& clrUpDwn, const COLORREF& clrDisabled );

    Method used for setting the color of the button caption for the button's up, down and disabled states. This is valid only when the SetButtonText() is called.

  • void SetTextPosition(UINT uiPos );

    Method used for setting the text position. The valid unit values are..

    DT_TOP          0x00000000
    DT_LEFT         0x00000000
    DT_CENTER       0x00000001
    DT_RIGHT        0x00000002 
    DT_VCENTER      0x00000004 
    DT_BOTTOM       0x00000008 
    DT_WORDBREAK    0x00000010 
    DT_SINGLELINE   0x00000020

    Any combination of the above flags can be used.

  • void SetTextPosition(int nXPos ,int nYPos);

    Method used for setting the x,y position of the text. If anyone of the SetTextPostion() is not called, then the text will be shown as DT_SINGLELINE|DT_CENTER|DT_VCENTER.

  • void SetFont(CString srtFntName_i, int nSize_i);

    Method used to set the font name and font size of the text. If it's not called, default font with default size is taken to draw the text.

Steps to use the class.

  • Add the ImageTextButton.h & ImageTextButton.cpp files to the workspace.
  • Add #include "ImageTextButton.h" at the include portion of the dialog which wants to have the CImageTextButton, as shown:
    #include "stdafx.h"
    #include "ImageTextButton.h"
    
  • Declare CImageTextButton objects corresponding to each button as:
    //Adding member variables corresponding to each button
    CImageTextButton m_btnOK;
    CImageTextButton m_btnPlay;
    CImageTextButton m_btnEnable;
    CImageTextButton m_btnStrt;
    
  • Set the properties needed for the buttons inside the Dialog's OnInitDialog() as:
    m_btnOK.SetButtonImg(IDB_OKU,IDB_OKD);
    m_btnOK.SetButtonText("OK");
    m_btnOK.SetFont("Arial",16);
    m_btnOK.SetTextColor(RGB(255,0,0),RGB(129,129,129));
    m_btnOK.SubclassDlgItem(IDOK,this);
    

Important

Don't forget to call SubclassDlgItem() for all the bitmap button members, only after this call will the Windows messages will reach the CImageTextButton at which I played the trick....

Now, it's time to build the application and enjoy the result.

[If any one of you find any problems to use this, please do mail to me ...]

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


Written By
Software Developer (Senior) NeST
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis code doesn't run in WinCE... help me.. Pin
Member 9215233-Nov-04 17:48
Member 9215233-Nov-04 17:48 
GeneralImprovements Pin
Michael Heber27-Oct-04 0:11
Michael Heber27-Oct-04 0:11 
GeneralLook at it Pin
dowj18-Sep-04 13:18
dowj18-Sep-04 13:18 
GeneralUrgent Pin
iipc9-Jul-04 23:58
iipc9-Jul-04 23:58 
GeneralUrgent Pin
iipc9-Jul-04 23:56
iipc9-Jul-04 23:56 
GeneralCButtonST Pin
Davide Calabro26-May-04 23:50
Davide Calabro26-May-04 23:50 
This is the wheel.

SoftechSoftware
Davide Calabro'
davide_calabro@yahoo.com
http://www.softechsoftware.it
GeneralCButtonSSL Pin
armentage24-May-04 4:28
armentage24-May-04 4:28 
GeneralDialog Background color in 1 line code Pin
JOHN1123-May-04 2:32
JOHN1123-May-04 2:32 
GeneralRe: Dialog Background color in 1 line code Pin
Ravi Bhavnani23-May-04 4:37
professionalRavi Bhavnani23-May-04 4:37 
GeneralPretty Good!! Pin
WREY23-May-04 2:29
WREY23-May-04 2:29 

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

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