// MyButton.cpp : 实现文件
//
#include "pch.h"
#include "MyButton.h"
// CMyButton
IMPLEMENT_DYNAMIC(CMyButton, CButton)
CMyButton::CMyButton()
{
m_bIsInWnd = FALSE;
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_MOUSEMOVE()
ON_WM_MOUSEHOVER()
ON_WM_MOUSELEAVE()
END_MESSAGE_MAP()
// CMyButton 消息处理程序
//设置按钮图片路径
void CMyButton::SetImagePath(CString strNoramlImgPath, CString strPressImgPath, CString strFloatImgPath)
{
m_strNormalImgPath = strNoramlImgPath;
m_strPressImgPath = strPressImgPath;
m_strFloatImgPath = strFloatImgPath;
}
void CMyButton::SetBkImg(CString strBkImg)
{
if (strBkImg.IsEmpty())
return;
m_BkImg.Load(strBkImg);
}
//初始化按钮,主要是调整按钮的位置,处理透明色
void CMyButton::ReleaseImg()
{
if (m_imgNormal)
{
m_imgNormal.Destroy();
}
if (m_imgPress)
{
m_imgPress.Destroy();
}
if (m_imgFloat)
{
m_imgFloat.Destroy();
}
}
bool CMyButton::InitMyButton(int nX/*左上角X坐标*/, int nY/*左上角Y坐标*/, int nW/*图像宽*/, int nH/*图像高*/, bool bIsPng/*是否是PNG图片*/)
{
HRESULT hr = 0;
if (m_strNormalImgPath.IsEmpty())
return false;
if (m_strPressImgPath.IsEmpty())
return false;
if (m_strFloatImgPath.IsEmpty())
return false;
hr = m_imgNormal.Load(m_strNormalImgPath);
int a = GetLastError();
if (FAILED(hr))
return false;
hr = m_imgPress.Load(m_strPressImgPath);
if (FAILED(hr))
return false;
hr = m_imgFloat.Load(m_strFloatImgPath);
if (FAILED(hr))
return false;
if (bIsPng)
{
if (m_imgNormal.GetBPP() == 32)
{
int i = 0;
int j = 0;
for (i = 0; i < m_imgNormal.GetWidth(); i++)
{
for (j = 0; j < m_imgNormal.GetHeight(); j++)
{
byte * pbyte = (byte *)m_imgNormal.GetPixelAddress(i, j);
pbyte[0] = pbyte[0] * pbyte[3] / 255;
pbyte[1] = pbyte[1] * pbyte[3] / 255;
pbyte[2] = pbyte[2] * pbyte[3] / 255;
}
}
}
if (m_imgPress.GetBPP() == 32)
{
int i = 0;
int j = 0;
for (i = 0; i < m_imgPress.GetWidth(); i++)
{
for (j = 0; j < m_imgPress.GetHeight(); j++)
{
byte * pbyte = (byte *)m_imgPress.GetPixelAddress(i, j);
pbyte[0] = pbyte[0] * pbyte[3] / 255;
pbyte[1] = pbyte[1] * pbyte[3] / 255;
pbyte[2] = pbyte[2] * pbyte[3] / 255;
}
}
}
if (m_imgFloat.GetBPP() == 32)
{
int i = 0;
int j = 0;
for (i = 0; i < m_imgFloat.GetWidth(); i++)
{
for (j = 0; j < m_imgFloat.GetHeight(); j++)
{
byte * pbyte = (byte *)m_imgFloat.GetPixelAddress(i, j);
pbyte[0] = pbyte[0] * pbyte[3] / 255;
pbyte[1] = pbyte[1] * pbyte[3] / 255;
pbyte[2] = pbyte[2] * pbyte[3] / 255;
}
}
}
}
MoveWindow(nX, nY, nW, nH);
return true;
}
//自绘制函数
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (!lpDrawItemStruct)
return;
HDC hMemDC;
HBITMAP bmpMem;
HGDIOBJ hOldObj;
bmpMem = CreateCompatibleBitmap(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top);
if (!bmpMem)
return;
hMemDC = CreateCompatibleDC(lpDrawItemStruct->hDC);
if (!hMemDC)
{
if (bmpMem)
{
::DeleteObject(bmpMem);
bmpMem = NULL;
}
return;
}
hOldObj = ::SelectObject(hMemDC, bmpMem);
int nW = lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left;
int nH = lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top;
RECT rectTmp = { 0 };
rectTmp = lpDrawItemStruct->rcItem;
MapWindowPoints(GetParent(), &rectTmp);
if (m_BkImg.IsNull() == false)
m_BkImg.Draw(hMemDC, 0, 0, rectTmp.right - rectTmp.left, rectTmp.bottom - rectTmp.top, rectTmp.left, rectTmp.top, rectTmp.right - rectTmp.left, rectTmp.bottom - rectTmp.top);
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
//按钮被选择
m_imgPress.AlphaBlend(hMemDC, 0, 0, nW, nH, 0, 0, nW, nH);
}
else
{
//默认状态
m_imgNormal.AlphaBlend(hMemDC, 0, 0, nW, nH, 0, 0, nW, nH);
}
::BitBlt(lpDrawItemStruct->hDC, 0, 0, nW, nH, hMemDC,0,0,SRCCOPY);
SelectObject(hMemDC, hOldObj);
if (bmpMem)
{
::DeleteObject(bmpMem);
bmpMem = NULL;
}
if (hMemDC)
{
::DeleteDC(hMemDC);
hMemDC = NULL;
}
return;
}
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CButton::OnMouseMove(nFlags, point);
if (!m_bIsInWnd)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.dwHoverTime = 10;
tme.hwndTrack = m_hWnd;
_TrackMouseEvent(&tme);
m_bIsInWnd = TRUE;
}
}
void CMyButton::OnMouseHover(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
HDC hMemDC;
HBITMAP bmpMem;
HGDIOBJ hOldObj;
HDC hDC = ::GetDC(GetSafeHwnd());
CRect rcItem;
GetClientRect(&rcItem);
if (hDC)
{
bmpMem = CreateCompatibleBitmap(hDC, rcItem.Width(), rcItem.Height());
if (!bmpMem)
{
::ReleaseDC(GetSafeHwnd(), hDC);
return;
}
hMemDC = CreateCompatibleDC(hDC);
if (!hMemDC)
{
if (bmpMem)
{
::DeleteObject(bmpMem);
bmpMem = NULL;
}
::ReleaseDC(GetSafeHwnd(), hDC);
return;
}
hOldObj = ::SelectObject(hMemDC, bmpMem);
RECT rectTmp = { 0 };
rectTmp = rcItem;
MapWindowPoints(GetParent(), &rectTmp);
if (m_BkImg.IsNull() == false)
m_BkImg.Draw(hMemDC, 0, 0, rectTmp.right - rectTmp.left, rectTmp.bottom - rectTmp.top, rectTmp.left, rectTmp.top, rectTmp.right - rectTmp.left, rectTmp.bottom - rectTmp.top);
int nW = rcItem.right - rcItem.left;
int nH = rcItem.bottom - rcItem.top;
m_imgFloat.AlphaBlend(hMemDC, 0, 0, nW, nH, 0, 0,nW,nH);
::BitBlt(hDC, 0, 0, nW, nH, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOldObj);
if (bmpMem)
{
::DeleteObject(bmpMem);
bmpMem = NULL;
}
if (hMemDC)
{
::DeleteDC(hMemDC);
hMemDC = NULL;
}
::ReleaseDC(GetSafeHwnd(), hDC);
}
CButton::OnMouseHover(nFlags, point);
}
void CMyButton::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CButton::OnMouseLeave();
InvalidateRect(NULL);
m_bIsInWnd = FALSE;
}
没有合适的资源?快使用搜索试试~ 我知道了~
MFC界面美化之-按钮美化(重绘按钮)
共94个文件
tlog:18个
png:16个
obj:8个
需积分: 5 21 下载量 51 浏览量
2024-03-19
16:05:42
上传
评论
收藏 118.15MB ZIP 举报
温馨提示
项目中包含的内容: 1.使用vs2022能直接运行后看到界面的程序 2.能够复用的库文件mybutton.h,mybutton.cpp 3.本人运行程序后,截取的效果图 这个项目文件是对mfc的按钮进行美化的一个完整工程,主要是利用图片贴图,来达到美化按钮的效果,把对应的对按钮进行重写的类,封装成了mybutton.h,mybutton.cpp。可以下载下来进行复用,我使用的开发软件是VS2022. 如果你使用的其他的开发软件,那么只需要仿照我的用法,直接把mybutton.h,mybutton.cpp这两个文件放到你的项目中就行了。
资源推荐
资源详情
资源评论
收起资源包目录
CustomButton.zip (94个子文件)
CustomButton
效果图.png 6KB
CustomButton
CustomButton.vcxproj.user 238B
CustomButton.aps 106KB
CustomButton.cpp 3KB
CustomButton.rc 11KB
CustomButton.h 514B
resource.h 737B
framework.h 2KB
res
stop.png 635B
icon_minimiz.png 204B
CustomButton.rc2 680B
icon_popup_off.png 387B
start.png 723B
icon_square.png 277B
CustomButton.ico 66KB
pch.h 544B
x64
Release
vc143.pdb 6.79MB
CustomButton.obj 54KB
CustomButton.pch 80.81MB
CustomButton.iobj 956KB
MyButton.obj 84KB
CustomButton.exe.recipe 303B
CustomButton.log 423B
CustomButtonDlg.obj 73KB
pch.obj 4.84MB
CustomButton.ipdb 305KB
CustomButton.res 68KB
CustomButton.tlog
CustomButton.lastbuildstate 163B
CL.write.1.tlog 2KB
rc.command.1.tlog 478B
rc.write.1.tlog 278B
rc.read.1.tlog 3KB
CL.command.1.tlog 3KB
link.command.1.tlog 2KB
link.read.1.tlog 7KB
link.write.1.tlog 1KB
CL.read.1.tlog 81KB
Debug
vc143.pdb 6.81MB
CustomButton.obj 100KB
CustomButton.pch 87.75MB
CustomButton.ilk 3.16MB
MyButton.obj 265KB
CustomButton.exe.recipe 301B
CustomButton.log 165B
CustomButtonDlg.obj 82KB
pch.obj 798KB
vc143.idb 1.31MB
CustomButton.res 68KB
CustomButton.tlog
CustomButton.lastbuildstate 161B
CL.write.1.tlog 4KB
rc.command.1.tlog 470B
rc.write.1.tlog 274B
rc.read.1.tlog 3KB
CL.command.1.tlog 3KB
link.command.1.tlog 2KB
link.read.1.tlog 6KB
link.write.1.tlog 1KB
CL.read.1.tlog 80KB
CustomButton.vcxproj.filters 2KB
CustomButtonDlg.h 827B
CustomButton.vcxproj 11KB
pch.cpp 158B
button
MyButton.cpp 6KB
MyButton.h 1KB
CustomButtonDlg.cpp 4KB
targetver.h 295B
.vs
CustomButton
v17
fileList.bin 65KB
Browse.VC.db 62.16MB
.suo 41KB
ipch
938a9983635b0f52.ipch 149.13MB
8cb19257814609c2.ipch 149.13MB
FileContentIndex
read.lock 0B
756086bb-632b-4ee5-a64d-bff9a9b19398.vsidx 43KB
017e8310-058d-4448-a25c-6ec82884709a.vsidx 3KB
merges
2a8fe1e5-b18a-428e-b701-2f416104adf9.vsidx 13KB
x64
Release
res
stop.png 635B
icon_minimiz.png 204B
CustomButton.rc2 680B
icon_popup_off.png 387B
start.png 723B
icon_square.png 277B
CustomButton.ico 66KB
CustomButton.exe 107KB
CustomButton.pdb 6.23MB
Debug
res
stop.png 635B
icon_minimiz.png 204B
CustomButton.rc2 680B
icon_popup_off.png 387B
start.png 723B
icon_square.png 277B
CustomButton.ico 66KB
CustomButton.exe 301KB
CustomButton.pdb 12.72MB
CustomButton.sln 1KB
共 94 条
- 1
资源评论
会&唱歌的鱼
- 粉丝: 368
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Spring Boot框架的实时监控与文件管理系统.zip
- 基于Spring Boot框架的实习管理系统.zip
- 基于Spring Boot框架的外卖管理系统.zip
- 基于Spring Boot框架的在线聊天系统.zip
- 【目标检测数据集】烟火检测数据集8300张VOC+YOLO格式.zip
- 基于Spring Boot框架的在线考试管理系统094fa87b038262ba944109a231f24c66.zip
- 基于Spring Boot框架的在线考试管理系统.zip
- 【目标检测数据集】烟火火灾检测数据集750张VOC+YOLO格式.zip
- 【目标检测数据集】水泥搅拌车数据集2165张VOC+YOLO格式.zip
- 基于Spring Boot框架的图书馆管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功