MFC自定义button实现颜色控制

简介: 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

相关文章
|
计算机视觉
我的Qt作品(6)使用Qt完整解析dxf文件并绘制(支持椭圆和样条曲线)
我的Qt作品(6)使用Qt完整解析dxf文件并绘制(支持椭圆和样条曲线)
2124 0
我的Qt作品(6)使用Qt完整解析dxf文件并绘制(支持椭圆和样条曲线)
[c++][记录]编译libusb-win32过程
[c++][记录]编译libusb-win32过程
886 0
|
人工智能 搜索推荐 网络架构
TryOffAnyone:快速将模特服装图还原为平铺商品图,生成标准化的服装展示效果
TryOffAnyone 是一款基于 AI 技术的工具,能够将模特穿着服装的图像快速还原为平铺商品图,支持智能识别、自动优化等功能,适用于电商平台和虚拟试衣场景。
1342 12
TryOffAnyone:快速将模特服装图还原为平铺商品图,生成标准化的服装展示效果
解决方案-CMake error: error in configuration process, project files may be invalid(Windows&VS可参考)
解决方案-CMake error: error in configuration process, project files may be invalid(Windows&VS可参考)
5241 0
|
存储 计算机视觉 C++
OpenCV(十四):ROI区域截取
OpenCV(十四):ROI区域截取
1215 0
|
前端开发 Java Spring
为什么会出现Request method ‘GET‘ not supported`
为什么会出现Request method ‘GET‘ not supported`
5869 1
|
算法 Java 测试技术
【深入探究 C++ 日志库性能比较】glog、log4cplus 和 spdlog 的日志输出性能分析
【深入探究 C++ 日志库性能比较】glog、log4cplus 和 spdlog 的日志输出性能分析
4163 0
|
C++ 开发者
技术经验分享:dumpbin的使用方法_dumpbin的基础使用
技术经验分享:dumpbin的使用方法_dumpbin的基础使用
2121 0
|
算法 计算机视觉
如何判断点在多边形内部:OpenCV--cv2.pointPolygonTest()方法详解
如何判断点在多边形内部:OpenCV--cv2.pointPolygonTest()方法详解
|
编解码 对象存储 UED
[Halcon&标定] 单相机标定
[Halcon&标定] 单相机标定
4035 2