- 在对话框内添加button控件,button控件属性中 所有者 描述改为TRUE
- 添加COwerButton类。代码如下:
COwerButton.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);
int SwitchDlgMouseMoveState;
private:
COLORREF m_bgColor;
COLORREF m_textColor;
COLORREF m_DownColor;
BOOL m_bPressed;
protected:
DECLARE_MESSAGE_MAP()
BOOL m_bTracking; //在鼠标按下没有释放时该值为true
afx_msg void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
};
COwerButton.cpp文章来源:https://www.toymoban.com/news/detail-518096.html
#include "pch.h"
#include "COwerButton.h"
IMPLEMENT_DYNAMIC(COwerButton, CButton)
COwerButton::COwerButton()
{
m_bgColor = RGB(39, 58, 91);
m_textColor = RGB(255, 255, 255);
m_bPressed = FALSE;
SwitchDlgMouseMoveState = 0;
}
COwerButton::~COwerButton()
{
}
BEGIN_MESSAGE_MAP(COwerButton, CButton)
ON_WM_DRAWITEM()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
// COwerButton 消息处理程序
void COwerButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CString btnCaption = L"";
//设置标题
CFont font;
CWnd* pItemCWnd = FromHandle(lpDrawItemStruct->hwndItem);
pItemCWnd->GetWindowText(btnCaption);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect drawRect;
CBrush pBrush;
CRect rect = lpDrawItemStruct->rcItem;//空间选择
//获得绘图DC
//得到原Button的矩形大小
drawRect.CopyRect(&(lpDrawItemStruct->rcItem));
//绘制控件框架
pDC->DrawFrameControl(&drawRect, DFC_BUTTON, lpDrawItemStruct->CtlType);
if (ODS_SELECTED & lpDrawItemStruct->itemState)
{
font.CreateFontW(0, 0, 0, 0, 400, NULL, NULL, NULL, DEFAULT_CHARSET, 0, 0, 0, 0, L"黑体");
pDC->SelectObject(&font);
pBrush.CreateSolidBrush(RGB(100, 149, 237));
}
else if (SwitchDlgMouseMoveState == 1)
{
font.CreateFontW(0, 0, 0, 0, 400, NULL, NULL, NULL, DEFAULT_CHARSET, 0, 0, 0, 0, L"黑体");
pDC->SelectObject(&font);
pBrush.CreateSolidBrush(RGB(30, 144, 255));
}
else
{
font.CreateFontW(0, 0, 0, 0, 400, NULL, NULL, NULL, DEFAULT_CHARSET, 0, 0, 0, 0, L"黑体");
pDC->SelectObject(&font);
pBrush.CreateSolidBrush(RGB(39, 58, 91));
}
//画矩形
pDC->FillRect(drawRect, &pBrush);
//定义一个CRect用于绘制文本
CRect textRect;
//拷贝矩形区域
textRect.CopyRect(&drawRect);
//获得字符串尺寸
CSize sz = pDC->GetTextExtent(btnCaption);
//调整文本位置 居中
textRect.top += (textRect.Height() - sz.cy) / 2;
//设置文本背景透明
pDC->SetBkMode(TRANSPARENT);
//设置文本颜色
pDC->SetTextColor(RGB(255, 255, 255));
//绘制文本内容
pDC->DrawText(btnCaption, &textRect, DT_RIGHT | DT_CENTER | DT_BOTTOM);
}
//设置按钮背景的颜色
void COwerButton::SetButtonBgColor(COLORREF color)
{
m_bgColor = color;
}
//设置按钮字体的颜色
void COwerButton::SetButtonTextColor(COLORREF color)
{
m_textColor = color;
}
void COwerButton::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
SwitchDlgMouseMoveState = 1;
// 重画按钮
Invalidate(TRUE);
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.dwHoverTime = 1;
m_bTracking = _TrackMouseEvent(&tme);
}
CButton::OnMouseMove(nFlags, point);
}
LRESULT COwerButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
SwitchDlgMouseMoveState = 0;
m_bTracking = FALSE;
InvalidateRect(NULL, FALSE);
return 0;
}
- 给button按钮添加变量,变量类型设为COwerButton,也可在主界面XXXDlg.h中修改button类型
COwerButton m_button1;文章来源地址https://www.toymoban.com/news/detail-518096.html
- 运行完成,效果如下:
当鼠标移动到按钮上以及点击按钮时按钮颜色会发生变化:
到了这里,关于MFC重载CButton类,实现改变按钮背景色、字体样式更改、鼠标滑动按钮变色功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!