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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds