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

CImageTextButton

0.00/5 (No votes)
22 May 2004 1  
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