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:

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

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
   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:


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);
    and you should call
    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.



  • 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!!

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

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date