MFC自定义button实现颜色控制
此自定义按钮可以实现鼠标的点击、释放、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