MFC中如何修改BUTTON按钮颜色

37 篇文章 3 订阅
32 篇文章 0 订阅
本文档展示了如何创建一个名为COwerButton的类,该类继承自CButton,并实现了按钮的自定义绘制,包括背景颜色、文字颜色的设置,以及按下和释放状态的处理。在DrawItem函数中,详细描述了不同状态下按钮的绘制逻辑,包括边框、焦点矩形和文字的显示。
摘要由CSDN通过智能技术生成

新建一个类继承CButton

.h文件

#pragma once
#include <afxwin.h>
class COwerButton :
    public CButton
{
		DECLARE_DYNAMIC(COwerButton)
	public:
		COwerButton();
		virtual ~COwerButton();
		void SetButtonBgColor(COLORREF color);
		void SetButtonTextColor(COLORREF color);

	private:
		COLORREF m_bgColor;
		COLORREF m_textColor;
		BOOL m_bPressed;

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

.cpp文件


#include "COwerButton.h"
IMPLEMENT_DYNAMIC(COwerButton, CButton)

COwerButton::COwerButton()
{
	m_bgColor = RGB(239, 233, 235);
	m_textColor = RGB(0, 0, 0);
	m_bPressed = FALSE;
}

COwerButton::~COwerButton()
{
}


BEGIN_MESSAGE_MAP(COwerButton, CButton)
	ON_WM_DRAWITEM()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()



// COwerButton 消息处理程序

void COwerButton::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 ((state & ODS_SELECTED) || (state & ODS_FOCUS)) {
		CPen pen(PS_DASHDOTDOT, 1, RGB(0, 0, 0));
		CBrush brush;
		brush.CreateStockObject(NULL_BRUSH);
		dc.SelectObject(&brush);
		dc.SelectObject(&pen);
		dc.FillSolidRect(rect, m_bgColor);
		dc.DrawFocusRect(focusRect);
		dc.Draw3dRect(rect, RGB(51, 51, 51), RGB(0, 0, 0));
	}
	else {
		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(focusRect);
	}
	dc.DrawEdge(rect, BDR_RAISEDINNER | BDR_RAISEDOUTER, BF_BOTTOMLEFT | BF_TOPRIGHT);
	if (m_bPressed) {
		dc.DrawFocusRect(focusRect);
		dc.DrawEdge(rect, BDR_RAISEDINNER | BDR_SUNKENOUTER, BF_BOTTOMLEFT | BF_TOPRIGHT);
		dc.Draw3dRect(rect, RGB(51, 51, 51), RGB(0, 0, 0));
	}
	CString strText;
	GetWindowText(strText);
	dc.SetBkMode(TRANSPARENT);
	dc.SetTextColor(m_textColor);
	if (state & ODS_DISABLED) {
		dc.SetTextColor(RGB(172, 168, 153));
	}
	dc.DrawText(strText, rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
	dc.Detach();
}
//按钮被按下
void COwerButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_bPressed = TRUE;

	CButton::OnLButtonDown(nFlags, point);
}

//按钮被释放
void COwerButton::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_bPressed = FALSE;

	CButton::OnLButtonDown(nFlags, point);
}
//设置按钮背景的颜色 
void COwerButton::SetButtonBgColor(COLORREF color)
{
	m_bgColor = color;
}

//设置按钮字体的颜色
void COwerButton::SetButtonTextColor(COLORREF color)
{
	m_textColor = color;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值