在网上找到了这么一段代码,但是怎么用在对话框中?
//-----------------以下为MyEdit.h---------------------------------------------
#pragma once
// CMyEdit
class CMyEdit : public CEdit
{
DECLARE_DYNAMIC(CMyEdit)
public:
CMyEdit();
virtual ~CMyEdit();
protected:
DECLARE_MESSAGE_MAP()
public:
void SetBackColor(COLORREF rgb);//设置文本框背景色
void SetTextColor(COLORREF rgb);//设置文本框的字体颜色
void SetTextFont(const LOGFONT &lf);//设置字体
COLORREF GetBackColor(void){return m_crBackGnd;}//获取当前背景色
COLORREF GetTextColor(void){return m_crText;}//获取当前文本色
BOOL GetTextFont(LOGFONT &lf);//获取当前字体
private:
COLORREF m_crText;//字体的颜色
COLORREF m_crBackGnd;//字体的背景颜色
CFont m_font;//字体
CBrush m_brBackGnd;//整个文本区的画刷
//控件自己的消息反射函数CtlColor,绘制控件之前调用
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
};
//---------------以下为MyEdit.cpp-------------------------------------------
#include "stdafx.h"
#include "MyEdit.h"
// CMyEdit
IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
CMyEdit::CMyEdit()
{
//初始化为系统字体和窗口颜色
m_crText = GetSysColor(COLOR_WINDOWTEXT);
m_crBackGnd = GetSysColor(COLOR_WINDOW);
m_font.CreatePointFont(90,"宋体");
m_brBackGnd.CreateSolidBrush(GetSysColor(COLOR_WINDOW));
}
CMyEdit::~CMyEdit()
{
}
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
//WM_CTLCOLOR的消息反射
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
// CMyEdit 消息处理程序
void CMyEdit::SetBackColor(COLORREF rgb)
{
//设置文字背景颜色
m_crBackGnd = rgb;
//释放旧的画刷
if (m_brBackGnd.GetSafeHandle())
m_brBackGnd.DeleteObject();
//使用文字背景颜色创建新的画刷,使得文字框背景和文字背景一致
m_brBackGnd.CreateSolidBrush(rgb);
//redraw
Invalidate(TRUE);
}
void CMyEdit::SetTextColor(COLORREF rgb)
{
//设置文字颜色
m_crText = rgb;
//redraw
Invalidate(TRUE);
}
void CMyEdit::SetTextFont(const LOGFONT &lf)
{
//创建新的字体,并设置为CEDIT的字体
if(m_font.GetSafeHandle())
{
m_font.DeleteObject();
}
m_font.CreateFontIndirectA(&lf);
CEdit::SetFont(&m_font);
//redraw
Invalidate(TRUE);
}
BOOL CMyEdit::GetTextFont(LOGFONT &lf)
{
if(m_font.GetLogFont(&lf)!=0)
{ return TRUE; }
return FALSE;
}
HBRUSH CMyEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
//刷新前更改文本颜色
pDC->SetTextColor(m_crText);
//刷新前更改文本背景
pDC->SetBkColor(m_crBackGnd);
//返回画刷,用来绘制整个控件背景
return m_brBackGnd;
}