按钮上放置PNG图片
头文件“
CPP文件:
添加一个按钮到界面上 设定为用户自绘
为其增加一个变量 Control类型 选择这个按钮类就行了
初始化的时候调用 m_PngBT.SetButtonImage( L"res\\R.PNG"); (图片是PNG)
- C/C++ code
-
#pragma once class PngButton : public CButton { DECLARE_DYNAMIC(PngButton) public: enum BUTTON_STATE { BUTTON_ENABLE = 0, BUTTON_HOVER = 1, BUTTON_CLICK = 2, BUTTON_DISABLE= 3 }; //按钮状态 PngButton(); virtual ~PngButton(); protected: afx_msg LRESULT OnMouseLeave(WPARAM wparam, LPARAM lparam); afx_msg LRESULT OnMouseHover(WPARAM wparam, LPARAM lparam); DECLARE_MESSAGE_MAP() public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); void SetButtonImage(WCHAR* str); afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); BOOL m_bDisable; // 按钮是否禁用 BOOL m_bCursorOnButton; // 鼠标是否在按钮上 BOOL m_bPress; // 按钮是否被按下 int m_nWidth; //图片宽 int m_nHeight; //图片高 int m_nSliceWidth; //每一块图片的宽 Bitmap *m_btnImage; //按钮图片 CRect m_rectButton; //按钮区域 protected: virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam); public: afx_msg BOOL OnEraseBkgnd(CDC* pDC); };
CPP文件:
- C/C++ code
-
#include "stdafx.h" #include "PngButton.h" // PngButton IMPLEMENT_DYNAMIC(PngButton, CButton) PngButton::PngButton() { m_bDisable = FALSE; m_bCursorOnButton = FALSE; m_bPress = FALSE; } PngButton::~PngButton() { } BEGIN_MESSAGE_MAP(PngButton, CButton) ON_WM_MOUSEMOVE() ON_MESSAGE(WM_MOUSEHOVER,OnMouseHover) ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_ERASEBKGND() END_MESSAGE_MAP() void PngButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct -> hDC); Graphics graphics(pDC -> m_hDC); if( m_bDisable == TRUE ) { ; graphics.DrawImage(m_btnImage,Rect(0,0,m_nSliceWidth,m_nHeight), BUTTON_DISABLE * m_nSliceWidth,0,m_nSliceWidth, m_nHeight,UnitPixel,NULL,NULL,NULL); } else { // click state if( lpDrawItemStruct -> itemState & ODS_SELECTED ) { graphics.DrawImage (m_btnImage,Rect( 0 ,0 , m_nSliceWidth , m_nHeight), BUTTON_CLICK * m_nSliceWidth , 0 , m_nSliceWidth , m_nHeight ,UnitPixel,NULL,NULL,NULL); } // hover state else if ( m_bPress) { graphics.DrawImage(m_btnImage, Rect( 0, 0, m_nSliceWidth, m_nHeight), BUTTON_HOVER * m_nSliceWidth,0, m_nSliceWidth, m_nHeight,UnitPixel,NULL,NULL,NULL); } // enable state else { graphics.DrawImage(m_btnImage,Rect( 0, 0,m_nSliceWidth,m_nHeight), BUTTON_ENABLE*m_nSliceWidth,0, m_nSliceWidth, m_nHeight,UnitPixel,NULL,NULL,NULL); } } } void PngButton::SetButtonImage( WCHAR* str ) { m_btnImage = new Bitmap( str); m_nWidth = m_btnImage -> GetWidth(); m_nHeight = m_btnImage -> GetHeight(); m_nSliceWidth = m_nWidth/4; //图片切成四分 CWnd *pWnd = this -> GetParent(); GetWindowRect( &m_rectButton ); pWnd -> ScreenToClient(m_rectButton); m_rectButton.right = m_rectButton.left + m_nSliceWidth; m_rectButton.bottom = m_rectButton.top + m_nHeight; MoveWindow(m_rectButton); //调整按钮大小以适应图片 } void PngButton::OnMouseMove(UINT nFlags, CPoint point) { if( m_bCursorOnButton == FALSE ) { TRACKMOUSEEVENT tme; ZeroMemory(&tme,sizeof(TRACKMOUSEEVENT)); tme.cbSize = sizeof(tme); tme.hwndTrack = m_hWnd; tme.dwFlags = TME_LEAVE|TME_HOVER; tme.dwHoverTime = 1; m_bCursorOnButton = _TrackMouseEvent(&tme); } CButton::OnMouseMove(nFlags, point); } LRESULT PngButton::OnMouseLeave(WPARAM wparam, LPARAM lparam) { m_bCursorOnButton = FALSE; m_bPress = FALSE; Invalidate(); return 0L; } LRESULT PngButton::OnMouseHover(WPARAM wparam, LPARAM lparam) { m_bPress = TRUE; Invalidate(); return 0L; } void PngButton::OnLButtonDown(UINT nFlags, CPoint point) { Invalidate(); CButton::OnLButtonDown(nFlags, point); } void PngButton::OnLButtonUp(UINT nFlags, CPoint point) { Invalidate(); CButton::OnLButtonUp(nFlags, point); } LRESULT PngButton::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_LBUTTONDBLCLK) { message = WM_LBUTTONDOWN; } return CButton::DefWindowProc(message, wParam, lParam);//很重要,消除双击没反应 } BOOL PngButton::OnEraseBkgnd(CDC* pDC) { return true; }
添加一个按钮到界面上 设定为用户自绘
为其增加一个变量 Control类型 选择这个按钮类就行了
初始化的时候调用 m_PngBT.SetButtonImage( L"res\\R.PNG"); (图片是PNG)