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

相关文章
|
3月前
|
Android开发
在mPaaS框架中,要自定义通知栏背景色
【1月更文挑战第13天】【1月更文挑战第65篇】在mPaaS框架中,要自定义通知栏背景色
27 1
|
5天前
如何实现更改窗体标题栏的样式
如何实现更改窗体标题栏的样式
16 0
|
3月前
|
C++
QT 重写控件(QPushButton为例)实现背景图片的切换和鼠标样式切换
一般在QT开发中,使用setCursor()给控件设置鼠标的样式效果(一般是手型和箭头的切换),一般情况下,这个函数也是起作用的,但是一旦调用了全局QApplication::setOverrideCursor()设置鼠标效果后,在使用setCursor给控件设置鼠标样式就不起效果了,这是QT的机制
75 0
|
3月前
|
Ubuntu iOS开发 MacOS
Qt5标题栏自定义QHeaderView自定义
Qt5标题栏自定义QHeaderView自定义
93 0
|
4月前
【sgDragSize】自定义组件:自定义拖拽修改DIV尺寸组件,适用于窗体大小调整
【sgDragSize】自定义组件:自定义拖拽修改DIV尺寸组件,适用于窗体大小调整
|
4月前
|
索引
[Qt5&控件] 下拉框ComBoBox和层叠窗口StackedWidget控件组合使用
[Qt5&控件] 下拉框ComBoBox和层叠窗口StackedWidget控件组合使用
39 0
|
10月前
|
前端开发 Python
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
492 0
MFC修改窗口背景颜色
MFC修改窗口背景颜色
220 0
Qt隐藏系统标题栏,使用自定义标题栏
Qt隐藏系统标题栏,使用自定义标题栏
Qt隐藏系统标题栏,使用自定义标题栏
|
前端开发 程序员
Qt-QML-ComboBox-自定义,实现状态表示,内容可以动态正价,使用ListModel
Qt-QML-ComboBox-自定义,实现状态表示,内容可以动态正价,使用ListModel
710 0
Qt-QML-ComboBox-自定义,实现状态表示,内容可以动态正价,使用ListModel