ImageTextButton

ImageTextButton

Hello, Guys.

Actually, this piece of code was made because I have such a requirement for one of my work projects.

The requirement is like this...

  • A dialog contains some buttons and all the buttons.
  • Some buttons have to load some bitmps and also need to embed some text on them.
  • The button is at some specified position, like this:

Because there is a CBitmapButton in MFC, my first consideration was to use this, but is seems that it is little bit difficult to handle. So, I decided to make a reuseable class for doing all this and it's working fine. So, I'm now starting what I tried to do with this code: Derive a CImageTextButton class from CButton and override the following:

//{{AFX_MSG(CImageTextButton)
   // NOTE - the ClassWizard will add and remove member functions
   // here.
   afx_msg void OnSetFocus(CWnd* pOldWnd);
   afx_msg void OnEnable(BOOL bEnable);
   virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_MSG

Then, I implemented the loading and drawing text on the button inside the DrawItem method. Also, I provided some methods to set the behaviour of the button.

// Implementation
public:
   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);

Method functionality

void SetTextPosition(UINT uiPos );

This method can be used to fix the position of the Text to be shown. This UINT is valid for the following:

DT_BOTTOM
DT_CENTER
DT_HIDEPREFIX
DT_LEFT
DT_NOPREFIX
DT_PREFIXONLY
DT_RIGHT
DT_SINGLELINE
DT_TOP
DT_VCENTER
DT_WORDBREAK
DT_WORD_ELLIPSIS

of the standard DrawText() method text formats.

void SetTextPosition(int nXPos ,int nYPos);

This method can be used to fix the exact x,y position of the button text (should be called if needed).

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

This method can be used to set the text color for the button for normal and disabled states. It should be called if the text color we want needs to be changed rather than using RGB(255,255,255).

void SetButtonText(const CString& strCaption);

This method is used to set the text of the button to be shown. If it is not called, only the image is shown.

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

This method is used to set the button bitmaps for the up, down, and disabled states. Only the up state is mandatory. The button is fully loaded as the bitmap and the method resizes the button according to the bitmap loaded.

void SetFont(CString srtFntName_i, int nSize_i)

This method is used to set the font details: the name and the size of the font to show the text in the button. It is not mandatory to call this method; if not called, it will take the default font settings for the button text.

Steps to be Taken

  1. Include the "ImageTextButton.h" ImageTextButton.cpp files in the workspace.
  2. Include #include "ImageTextButton.h" in the dialog class in which you want to have an image text button.
  3. Add a member corresponding to each button you want, like so:
  4.   CImageTextButton m_btnOK;
      CImageTextButton m_btnPlay;
      CImageTextButton m_btnEnable;
      CImageTextButton m_btnStrt;
    

    in the sample.

  5. Take the OnInitDialog of the dialog in which you want the image text button and set the button details, like this:
  6.   m_btnStrt.SetButtonImg(IDB_STRTU,IDB_STRTD,IDB_STRTX);
      m_btnStrt.SetButtonText("Start");
      m_btnStrt.SetFont("Arial",14);
      m_btnStrt.SetTextPosition(7,3);
      m_btnStrt.SetTextColor(RGB(0,255,255),RGB(129,129,129));
    
    and you should call
     m_btnStrt.SubclassDlgItem(IDC_STRT_BTN,this);
    to get the windows messaged to the custom class we derived. Next, build and run the code. You can find the result.

I don't think that this is the pure solution for this, but this code is light and if anyone can point out any probs do mail them to me. Enjoy it.



Downloads

Comments

  • Nothing new to offer by this article. Just duplicate copy!!!

    Posted by imrank on 05/30/2004 07:55pm

    In this article nothing new to offer! As this technique already demonstrated by author of CxShadeButton Project by Davide Pizzolato, way back in 2001. This guy copied the same technique in different colour scheme. STOP REPEATING AND COPYING SOMEONE ELSE WORK!!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Flash technology is becoming more prominent in the storage industry. Offering superior speed and reliability when compared to traditional hard disk drives – flash storage is a flexible and increasingly cost-effective technology that can be used to optimize enterprise storage environments. This ebook explores the many uses and benefits of flash storage technology in the enterprise. Check it out to discover and learn all you need to: Optimize storage performance Leverage server flash as storage cache …

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds