MFC 按钮上贴png图片 边框为透明的

加载pngbutton类

fff.cpp 加上外部定义:

ULONG_PTR gdiplusToken;

InitInstance加上:

GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

stdafx.h加上:

#include <GdiPlus.h>
using namespace Gdiplus;

链接器加上:

gdiplus.lib

pngbutton.h

/***************************************************************************************


PngButton


by:breakind


用途:使用PNG来作为按钮图片


使用方法:将按键四种状态的图片设置进来就OK了


注意:使用GDI+完成,所以需要GDIPLUS的支持
****************************************************************************************/
#pragma once




// PngButton.h


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




pngbutton.cpp

// PngButton.cpp : implementation file
//


#include "stdafx.h"
#include "fff.h"
#include "PngButton.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()






// PngButton message handlers




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




// TODO:  Add your code to draw the specified item
}


void PngButton::SetButtonImage(WCHAR* str)
{

m_btnImage = new Bitmap(str);//创建BITMAP对象



m_nWidth = m_btnImage -> GetWidth();
m_nHeight = m_btnImage -> GetHeight();


m_nSliceWidth = m_nWidth;   //图片切成四分
//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)
{
// TODO: Add your message handler code here and/or call default
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)
{
// TODO: Add your message handler code here and/or call default
Invalidate();


CButton::OnLButtonDown(nFlags, point);
}


void PngButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Invalidate();
CButton::OnLButtonUp(nFlags, point);
}


LRESULT PngButton::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if (message == WM_LBUTTONDBLCLK)
{
message = WM_LBUTTONDOWN;
}
return CButton::DefWindowProc(message, wParam, lParam);//很重要,消除双击没反应
}


BOOL PngButton::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default


//return CButton::OnEraseBkgnd(pDC);
return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值