MFC CEdit设置背景色和字体颜色

MFC CEdit 设置背景色和字体颜色

基于继承CEdit控件自行撰写的CEditEx类,便于使用~c++

1. 新建CEditEx类

EditEx.hweb

#if !defined(AFX_EDITEX_H__9B5C9C39_B497_4EBB_91BC_8D23F5BFEDBE__INCLUDED_)
#define AFX_EDITEX_H__9B5C9C39_B497_4EBB_91BC_8D23F5BFEDBE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// EditEx.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CEditEx window

class CEditEx : public CEdit
{
// Construction
public:
	CEditEx();

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CEditEx)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CEditEx();
	void SetTextColor(COLORREF rgb);
	void SetBackColor(COLORREF rgb);
	// Generated message map functions
protected:
	//text and text background colors
	COLORREF m_crText;
	COLORREF m_crBackGnd;
	//background brush
	CBrush m_brBackGnd;
	//{{AFX_MSG(CEditEx)
	afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_EDITEX_H__9B5C9C39_B497_4EBB_91BC_8D23F5BFEDBE__INCLUDED_)

EditEx.cppide

// EditEx.cpp : implementation file
//

#include "stdafx.h"
#include "EditEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEditEx

CEditEx::CEditEx()
{
}

CEditEx::~CEditEx()
{
}


BEGIN_MESSAGE_MAP(CEditEx, CEdit)
	//{{AFX_MSG_MAP(CEditEx)
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditEx message handlers

HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a non-NULL brush if the parent's handler should not be called
	// TODO: Return a non-NULL brush if the parent's handler should not be called
	
	//set text color
	pDC->SetTextColor(m_crText);
	//set the text's background color
	pDC->SetBkColor(m_crBackGnd);
	//return the brush used for background this sets control background
	return m_brBackGnd;
}


void CEditEx::SetBackColor(COLORREF rgb)
{
	//set background color ref (used for text's background)
	m_crBackGnd = rgb;
	
	//free brush
	if (m_brBackGnd.GetSafeHandle())
       m_brBackGnd.DeleteObject();
	//set brush to new color
	m_brBackGnd.CreateSolidBrush(rgb);
	
	//redraw
	Invalidate(TRUE);
}


void CEditEx::SetTextColor(COLORREF rgb)
{
	//set text color ref
	m_crText = rgb;

	//redraw
	Invalidate(TRUE);
}

2. 对话框添加编辑控件Edit Control

ID:IDC_EDIT
绑定变量:CEditEx m_edit;
头文件:#include"EditEx.h"
添加绑定控件:svg

void CXXXDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEEGTestDlg)
	DDX_Control(pDX, IDC_EDIT, m_edit);
}

3. 使用

示例,在对话框初始化函数OnInitDialog()中调用:函数

m_edit.SetWindowTextA("当前状态:未链接");
m_edit.SetTextColor(RGB(0, 0, 0));			//黑色
m_edit.SetBackColor(RGB(255, 255, 255));	//白色

示例图片:
在这里插入图片描述字体