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

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

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