当前位置:网站首页>MFC自定义button实现颜色控制
MFC自定义button实现颜色控制
2022-06-13 10:56:00 【我不是萧海哇~~~~】
此自定义按钮可以实现鼠标的点击、释放、hover和leave功能。
1、新建一个button按钮,修改Owner Drawer的属性为True
2、在工程新建CCustomButton类
#pragma once
#include "stdafx.h"
// CustomButton dialog
class CCustomButton : public CButton
{
DECLARE_DYNAMIC(CCustomButton)
public:
CCustomButton();
virtual ~CCustomButton();
void SetButtonBgColor(COLORREF color);
void SetButtonTextColor(COLORREF color);
private:
COLORREF m_bgColor;
COLORREF m_textColor;
BOOL m_bPressed;
BOOL m_bTrackingMouse;
protected:
DECLARE_MESSAGE_MAP()
afx_msg void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnMouseHover(UINT nFlags, CPoint point);
afx_msg void OnMouseLeave();
};
// CustomButton.cpp : implementation file
//
#include "stdafx.h"
#include "CustomButton.h"
#include "afxdialogex.h"
// CustomButton dialog
IMPLEMENT_DYNAMIC(CCustomButton, CButton)
CCustomButton::CCustomButton()
{
//m_bgColor = RGB(52, 52, 52);
//m_textColor = RGB(153, 153, 153);
//m_bgColor = RGB(0, 255, 0);
//m_textColor = RGB(255, 0, 0);
m_bPressed = FALSE;
m_bTrackingMouse = true;
}
CCustomButton::~CCustomButton()
{
}
BEGIN_MESSAGE_MAP(CCustomButton, CButton)
ON_WM_DRAWITEM()
ON_WM_MOUSEHOVER()
ON_WM_MOUSELEAVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
// CCustomButton 消息处理程序
void CCustomButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect rect;
GetClientRect(rect);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
// UINT state = lpDrawItemStruct->itemState;
//CRect focusRect(rect);
//focusRect.DeflateRect(4, 4, 4, 4);
if (m_bTrackingMouse)
{
m_bgColor = RGB(0, 255, 0);
m_textColor = RGB(255, 0, 0);
CPen pen(PS_DOT, 1, m_bgColor);
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&brush);
dc.SelectObject(&pen);
dc.FillSolidRect(rect, m_bgColor);
dc.Rectangle(rect);
CString strText;
GetWindowText(strText);
//m_bgColor = RGB(52, 52, 52);
//m_textColor = RGB(153, 153, 153);
dc.SetBkColor(m_bgColor);
dc.SetTextColor(m_textColor);
dc.DrawText(strText, rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
}
else
{
m_bgColor = RGB(255, 0, 0);
m_textColor = RGB(0, 255, 0);
CPen pen(PS_DOT, 1, m_bgColor);
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&brush);
dc.SelectObject(&pen);
dc.FillSolidRect(rect, m_bgColor);
dc.Rectangle(rect);
CString strText;
GetWindowText(strText);
//m_bgColor = RGB(40, 53, 69);
//m_textColor = RGB(46, 189, 255);
dc.SetBkColor(m_bgColor);
dc.SetTextColor(m_textColor);
dc.DrawText(strText, rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
//dc.DrawText(strText, rect);
//CBrush brush(RGB(255, 0, 0));
//dc.FillRect(&(lpDrawItemStruct->rcItem), &brush);
//DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
// &lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
// SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
}
// button字体
//CString strText;
//GetWindowText(strText);
//dc.SetBkMode(TRANSPARENT);
//dc.SetTextColor(m_textColor);
///*if () {
// dc.SetTextColor(RGB(172, 168, 153));
//}*/
//dc.DrawText(strText, rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
dc.Detach();
}
//按钮被按下
void CCustomButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_bPressed = TRUE;
CButton::OnLButtonDown(nFlags, point);
}
//按钮被释放
void CCustomButton::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bPressed = FALSE;
CButton::OnLButtonDown(nFlags, point);
}
//设置按钮背景的颜色
void CCustomButton::SetButtonBgColor(COLORREF color)
{
m_bgColor = color;
}
//设置按钮字体的颜色
void CCustomButton::SetButtonTextColor(COLORREF color)
{
m_textColor = color;
}
void CCustomButton::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bTrackingMouse)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.hwndTrack = GetSafeHwnd();
tme.dwHoverTime = 10; // 影响OnMouseHover函数的响应时间,设置小点。
if (::TrackMouseEvent(&tme))
{
m_bTrackingMouse = false;
}
}
CButton::OnMouseMove(nFlags, point);
}
void CCustomButton::OnMouseHover(UINT nFlags, CPoint point)
{
//HDC hdc;
//hdc = ::GetDC(this->GetParent()->GetSafeHwnd());
//HPEN hpen = ::CreatePen(PS_SOLID, 1, RGB(255, 0, 0));//创建画笔
//HPEN oHpen;
//oHpen = (HPEN)::SelectObject(hdc, hpen);//把新的画笔填充到DC
Invalidate();
//CButton* btn = (CButton*)this->GetParent()->GetDlgItem(IDC_BUTTON1)->SetFaceColor(RGB(0, 255, 0));
m_bTrackingMouse = false;
CButton::OnMouseHover(nFlags, point);
}
void CCustomButton::OnMouseLeave()
{
/*CRect rt; GetClientRect(&rt); InvalidateRect(rt); */
Invalidate();
m_bTrackingMouse = true;
CButton::OnMouseLeave();
}
3、点击工程的视图界面,右键按钮,选择"增加变量",将原来类名CButton修改名为CCustomButton
边栏推荐
- d编译时生成唯一标识
- Test cases that testers must master
- Read pgstat [this time Gauss is not a mathematician]
- QTcpServer. QTcpSocket. QUdpSocket之间的区别
- Go 要加个箭头语法,这下更像 PHP 了!
- Record several interesting XSS vulnerability discoveries
- IDEA远程调试spark-submit提交的jar
- Flutter simple and excellent open source dialog uses free_ dialog
- deepin系统中Qt5.12无法输入中文(无法切换中文输入法)解决办法
- SQL server cannot find user or group when creating windows login account
猜你喜欢
Easyclick run code snippet out null
IDEA远程调试spark-submit提交的jar
Codeforces Round #798 (Div. 2)ABCD
Index query list injects MySQL and executes Oracle
Finally, the monthly income is 20000!!
Brief description of redo logs and undo logs in MySQL
[image denoising] image denoising based on MATLAB Gaussian + mean + median + bilateral filtering [including Matlab source code 1872]
China SaaS industry panorama
Experience of electric competition - program-controlled wind pendulum
Spark source code (I) how spark submit submits jars and configuration parameters to spark server
随机推荐
报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究
Deploy vscode on kubernetes cluster
音视频技术开发周刊 | 249
Review of last week's hot spots (6.6-6.12)
d编译时生成唯一标识
vivo大规模 Kubernetes 集群自动化运维实践
Implementation of singleton mode
Folder data synchronization tool sync folders Pro
MySQL到底怎么优化?
低代码开发一个基础模块
Web 3.0?高成本版的P2P而已
Advanced technology management - what management tools can managers use
2022煤矿探放水特种作业证考试题库模拟考试平台操作
记一次水平越权漏洞的利用
Vivo large scale kubernetes cluster automation operation and maintenance practice
Ubuntu installs MySQL compressed package for future reference
What is 400g Ethernet?
2022年劳务员-通用基础(劳务员)上岗证题目及答案
终于,月入 20000 !!
2022煤矿探放水特种作业证考试题库模拟考试平台操作