65.9K
CodeProject is changing. Read more.
Home

Image Button Control

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.46/5 (42 votes)

Mar 28, 2003

viewsIcon

203223

downloadIcon

5535

Image Button Control MFC Class ( CImageButton )

Sample Image - CImageButton.jpg

Class header file

CImageButton class info :

class CImageButton : public CButton
{
// Construction
public:
 CImageButton();

 enum {
    TEXT_INCLUDE      = 0x0001, 
    IMAGE_RIGHT      = 0x0002,
    IMAGE_VCENTER     = 0x0004,
    IMAGE_BOTTOM      = 0x0008,
    IMAGE_HCENTER     = 0x0010,
    FOCUS_TEXTONLY    = 0x0020,
    }; 
// Attributes
public: 
// Operations
public: 
// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CImageButton)
 public:
 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
 //}}AFX_VIRTUAL 
// Implementation
public:
 void SetTextColor( COLORREF color );
 BOOL SetAlignStyle( DWORD dwStyle );
 BOOL SetButtonImage( UINT uiImageID , COLORREF  clrMask );
 virtual ~CImageButton(); 
 // Generated message map functions
protected: 

 void ReCalculateSettings(); 

 COLORREF    m_clrMask;
 CBitmap     m_bitmapImage;
 BITMAP      m_bitmap;
 HBITMAP     m_hbmpDisabled;
 CImageList m_ImageList;
 BOOL        m_bLoaded; 
 DWORD       m_dwAlign;   //View Style 

 CRect       m_RectImage;
 CRect       m_RectText;
 CRect       m_RectTextFocus; 
 COLORREF    m_clrText;       //Text Color
 
 //{{AFX_MSG(CImageButton)
 afx_msg void OnSetFocus(CWnd* pOldWnd);
 afx_msg void OnSysColorChange();
 afx_msg void OnSize(UINT nType, int cx, int cy);
 afx_msg void OnEnable(BOOL bEnable);
 //}}AFX_MSG 
 DECLARE_MESSAGE_MAP()
}; 
  

Class Usage

Here is some sample code to show you how to use the class.

CImageButton m_Button; 
m_Button.SetTextColor( RGB( 200 , 0 , 0 ) );
m_Button.SetAlignStyle( CImageButton::FOCUS_TEXTONLY |  
    CImageButton::TEXT_INCLUDE |   CImageButton::IMAGE_RIGHT | 
    CImageButton::IMAGE_VCENTER );
 m_Button.SetButtonImage( IDB_BITMAP1 , RGB( 255 , 0 , 255 ) );