目录
1.2 在对话框源文件中OnInitDialog()函数中创建画刷对象
1.3 重载OnCtlColor,即WM_CTLCOLOR消息
1. 修改MFC对话框背景色
1.1 在对话框头文件中申明画刷对象
CBrush m_brush;
1.2 在对话框源文件中OnInitDialog()函数中创建画刷对象
BOOL CCameraDemoDlg::OnInitDialog()
{
// ... 为便于阅读,自动生成代码省略
m_brush.CreateSolidBrush(RGB(166, 243, 248));
return TRUE; // return TRUE unless you set the focus to a control
}
1.3 重载OnCtlColor,即WM_CTLCOLOR消息
HBRUSH CCameraDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
if (nCtlColor == CTLCOLOR_DLG)
{
return m_brush;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
2 修改文本框等控件的背景色、文字颜色
HBRUSH CCameraDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
if (nCtlColor == CTLCOLOR_DLG)
{
return m_brush;
}
if (nCtlColor == CTLCOLOR_STATIC)
{
//总体颜色设置
pDC->SetTextColor(RGB(50, 50, 200)); //字体颜色
//pDC->SetBkColor(RGB(166, 243, 248)); //字体背景色
pDC->SetBkMode(TRANSPARENT);//设置背景透明
//STATIC控件单独颜色设置
switch (pWnd->GetDlgCtrlID())
{
case IDC_STATIC_1:
pDC->SetTextColor(RGB(255, 0, 0)); //字体颜色
break;
default:
break;
}
return m_brush;
}
if (nCtlColor == CTLCOLOR_EDIT)
{
pDC->SetTextColor(RGB(150, 105, 200)); //字体颜色
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}
3 修改文本框等控件的字体大小
在对话框源文件中OnInitDialog()函数中创建字体对象并设置
BOOL CCameraDemoDlg::OnInitDialog()
{
// ... 为便于阅读,自动生成代码省略
CRect rect;
m_static_cam.GetClientRect(rect);
m_camRect = { 0, 0, rect.Width(), rect.Height() };
m_static_cam.Load(MAKEINTRESOURCE(IDR_GIF1), _T("GIF"));
m_static_cam.SetPaintRect(&m_camRect);
m_static_cam.Draw();
m_combo_camno.SetCurSel(0);
GetDlgItem(IDC_EDIT_VID)->SetWindowText("1bcf");
GetDlgItem(IDC_EDIT_PID)->SetWindowText("3288");
EnableButton(false);
m_btnClose.SetButtonBgColor(RGB(255, 0, 0));
CFont* p_font = new CFont;
p_font->CreateFont(18, // nHeight,文字大小
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_BOLD, // nWeight,加粗
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFac,字体
((CStatic*)GetDlgItem(IDC_STATIC_1))->SetFont(p_font, FALSE);
((CEdit*)GetDlgItem(IDC_EDIT_VID))->SetFont(p_font, FALSE);
((CEdit*)GetDlgItem(IDC_EDIT_PID))->SetFont(p_font, FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
4 修改按钮控件的背景色、字体颜色
4.1 在对话框内添加button控件,button控件属性中 Owner Draw 描述改为 True
4.2 添加COwerButton类
4.2.1 OwerButton.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);
};
4.2.2 OwerButton.cpp
#include "stdafx.h"
#include "OwerButton.h"
IMPLEMENT_DYNAMIC(COwerButton, CButton)
COwerButton::COwerButton()
{
m_bgColor = RGB(100, 149, 237);
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)
{
COLORREF textColor = m_textColor;
CString btnCaption = "";
//设置标题
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.CreateFont(0, 0, 0, 0, 400, NULL, NULL, NULL, DEFAULT_CHARSET, 0, 0, 0, 0, "黑体");
pDC->SelectObject(&font);
pBrush.CreateSolidBrush(RGB(39, 58, 91));
}
else if (SwitchDlgMouseMoveState == 1)
{
font.CreateFont(0, 0, 0, 0, 400, NULL, NULL, NULL, DEFAULT_CHARSET, 0, 0, 0, 0, "黑体");
pDC->SelectObject(&font);
pBrush.CreateSolidBrush(RGB(50, 90, 180));
}
else if (GetStyle() & WS_DISABLED)//禁止状态
{
font.CreateFont(0, 0, 0, 0, 400, NULL, NULL, NULL, DEFAULT_CHARSET, 0, 0, 0, 0, "黑体");
pDC->SelectObject(&font);
pBrush.CreateSolidBrush(RGB(243, 243, 243));//背景色(灰白色)
textColor = RGB(128, 128, 128);
}
else
{
font.CreateFont(0, 0, 0, 0, 400, NULL, NULL, NULL, DEFAULT_CHARSET, 0, 0, 0, 0, "黑体");
pDC->SelectObject(&font);
pBrush.CreateSolidBrush(m_bgColor);
}
//画矩形
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(textColor);
//绘制文本内容
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;
}
4.3 给button按钮添加变量,变量类型设为COwerButton
注:也可在主界面XXXDlg.h中修改button类型
COwerButton m_btnCapture;
5 演示Demo
5.1 开发环境
-
Visual Studio 2015
-
MFC
-
Windows 10 Pro x64
5.2 功能介绍
演示VS2015 MFC对话框程序UI优化,包括对话框背景色,文本框等控件的背景色、文字颜色、字体大小等设置。
5.3 下载地址
开发环境:
-
Visual Studio 2015
-
MFC
-
Windows 10 Pro x64
下载地址:MFC对话框程序界面UI优化Demo