Click here to Skip to main content
11,478,619 members (67,385 online)
Click here to Skip to main content

CImageTextButton

, 22 May 2004 103.6K 3.3K 50
Rate this:
Please Sign up or sign in to vote.
Bitmap button which embeds text on it.

Sample Image - CImageTextButton.jpg

Introduction

Hello guys... Are you looking for a special button?? Then take it Smile | :) . 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

Share

About the Author

anuragvelekkattu
Software Developer (Senior) NeST
India India
No Biography provided

Comments and Discussions

 
GeneralThis code doesn't run in WinCE... help me.. Pin
hwa38433-Nov-04 18:48
memberhwa38433-Nov-04 18:48 
GeneralImprovements Pin
Michael Heber27-Oct-04 1:11
memberMichael Heber27-Oct-04 1:11 
GeneralLook at it Pin
dowj18-Sep-04 14:18
memberdowj18-Sep-04 14:18 
GeneralUrgent Pin
ankooraga10-Jul-04 0:58
sussankooraga10-Jul-04 0:58 
GeneralUrgent Pin
iipc10-Jul-04 0:56
memberiipc10-Jul-04 0:56 
GeneralCButtonST Pin
Davide Calabro27-May-04 0:50
memberDavide Calabro27-May-04 0:50 
This is the wheel.

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

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
Web02 | 2.8.150520.1 | Last Updated 23 May 2004
Article Copyright 2004 by anuragvelekkattu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid