加载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;
}

MFC 按钮上贴png图片 边框为透明的相关推荐

  1. C语言用句柄显示bmp图片,VC编程之VC MFC界面上显示BMP图片

    本文主要向大家介绍了VC编程之VC MFC界面上显示BMP图片,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助. 1.通过点击界面浏览按钮选择BMP图像文件. 点击浏览按钮打开文件对话框选择 ...

  2. MFC 按钮控件添加图片

    1.将准备好的BMP格式图片放入项目中的"res"文件夹中: 2.在项目"资源视图"的"Bitmap"文件夹上右击选择"添加资源& ...

  3. VC MFC界面上显示BMP图片

    1.通过点击界面浏览按钮选择BMP图像文件. 点击浏览按钮打开文件对话框选择BMP图像文件,得到文件所在的路径目录.关键代码如下: void ShowBMPDlg::OnButtonSelectiam ...

  4. html引入png不显示透明北京,解决在网页上显示PNG图片底色不透明的方法

    来源: 网易博客 类别: 编程 作者: 阿拉蕾 发布时间: 2009-9-7 22:09:18 很多时间,我们需要在网页上插入一些没有底色,只有轮廓的图片,而常用的底色透明的图片的有GIF和PNG,如 ...

  5. 解决在网页上显示PNG图片底色不透明的方法

    来源: 网易博客 类别: 编程 作者: 阿拉蕾 发布时间: 2009-9-7 22:09:18 很多时间,我们需要在网页上插入一些没有底色,只有轮廓的图片,而常用的底色透明的图片的有GIF和PNG,如 ...

  6. 9行代码实现图片上传和预览(自定义按钮上传)

    9行代码实现图片上传和预览(自定义按钮上传) 结果展示: 默认展示 2.点击按钮后,选择图片文件 图片预览 首先我们定义一个type为file的input,并将它隐藏,绘制一个button,在按钮点击 ...

  7. php图片添加角标,分享一段html在消息按钮上增加数量角标的实现代码

    这篇文章主要介绍了html在消息按钮上增加数量角标的实现代码,需要的朋友可以参考下 html代码: 消息4 css代码: /*角标 */ .ii{ display: none; background: ...

  8. MFC按钮CXPButton类,代码阅读起来还是挺不错的

    在操手MFC的时候,经常会抱怨MFC界面不如其他的框架或语言,比如VB,C#等等,面对MS在系统上的不断更新换代,我们也越来越追求软件的视觉效果,譬如我们会更喜欢win7下的玻璃效果,看起来很炫. 在 ...

  9. 【MFC】vs2013_MFC使用文件之15.mfc 按钮CBitmapButton的使用

    本文是基于对话框的 博文基于 无幻 的博文为基础写的 http://blog.csdn.net/akof1314/article/details/4951836 笔者使用mfc撑死2个星期,不过这是有 ...

最新文章

  1. javascript es6 module 模块 的使用
  2. JAVA-微信支付开发
  3. Redis常用命令之操作Set(集合)
  4. 网络化沟通及协作的人机交互编程语言-机器人语言2
  5. linux系统下定时备份,在Linux系统中简单地实现定时备份的方法 -电脑资料
  6. 图解机器学习—算法原理与Python语言实现(文末留言送书)
  7. Time Limit Exceeded的原因及避免方法
  8. 悟饭服务器连接中断,英雄联盟连接服务器失败解决方法
  9. 无线局域网中iPhone无法访问IIS
  10. JavaScript的pop()
  11. Tomcatnbsp;Servletnbsp;JSPamp;nbs…
  12. 错题本——数据结构(线性表)
  13. Springcloud快速入门
  14. WIN10 双显示器设置
  15. 地理分布团队的敏捷生命周期
  16. 黑马程序员 — JAVA基础 — 内部类、异常
  17. 搜索系统—搜索引擎的原理,架构与细节
  18. 最大流最小割定理(max flow/min cut theory)
  19. 学生党 10 分钟搭了一个网站,后来净赚 100 万美金....
  20. 微软服务器加速,通过 Azure CDN 进行动态站点加速

热门文章

  1. 手绘绘画培训如何系统学习?找美术集网校错不了!
  2. vector的日常笔记 酒馆浪人的博客
  3. “爱心接力 温暖传递” ——利尔达走进福利院,冬日送温暖活动
  4. 谷歌浏览器开发者工具的使用详解
  5. 第二章 Visual Studio开发环境的安装、配置
  6. SPI FLASH 二线/四线模式
  7. 【JS】用户可选择确定或取消的提示框
  8. C++之构造函数的初始化参数表
  9. vue项目查看vue版本及cli版本
  10. vmware虚拟机中的ubuntu开机黑屏,按任何键都没反应