// ColorText_Static.h : header file
#pragma once
// CColorText_Static window
class CColorText_Static : public CStatic
{
// Attributes
private:
BOOL b_FillBackColor;
COLORREF m_DisBackColro;
COLORREF m_BackColor;
COLORREF m_TextColor;
CFont *m_Font;
CBrush m_BackBrush;
CBrush m_DisBackBrush;
// Construction
public:
CFont *GetFont() { return m_Font; }
CColorText_Static();
virtual void SetFont(LOGFONT *LogFont, BOOL bRedraw = TRUE);
virtual void SetFont(CFont* pFont, BOOL bRedraw = TRUE);
virtual void SetFont(int nHeight, LPCTSTR fontName, BOOL bRedraw = true);
void Set_BackColor(COLORREF cr);
COLORREF Get_BackColor() { return(m_BackColor); };
void Set_TextColor(COLORREF cr)
{
m_TextColor = cr;
if(GetSafeHwnd())
Invalidate();
}
COLORREF Get_TextColor()
{
return(m_TextColor);
}
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CColorText_Static)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CColorText_Static();
// Generated message map functions
protected:
//{{AFX_MSG(CColorText_Static)
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//
// ColorText_Static.cpp : implementation file
//
#include "stdafx.h"
#include "ColorText_Static.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CColorText_Static
CColorText_Static::CColorText_Static()
{
b_FillBackColor = 0;
m_Font = 0;
m_DisBackColro = RGB(200, 200, 200);
Set_BackColor(GetSysColor(COLOR_3DFACE));
Set_TextColor(0);
}
CColorText_Static::~CColorText_Static()
{
if(m_Font)
{
m_Font->DeleteObject();
delete m_Font;
}
m_BackBrush.DeleteObject();
m_DisBackBrush.DeleteObject();
}
void CColorText_Static::SetFont(LOGFONT *LogFont, BOOL bRedraw)
{
if(m_Font)
m_Font->DeleteObject();
if(m_Font == NULL)
m_Font = new CFont();
if(m_Font)
{
if(!m_Font->CreatePointFontIndirect( LogFont ))
{
delete m_Font;
m_Font = NULL;
}
}
}
void CColorText_Static::SetFont(CFont* pFont, BOOL bRedraw)
{
LOGFONT LogFont;
pFont->GetLogFont(&LogFont);
SetFont(&LogFont, bRedraw);
}
void CColorText_Static::SetFont(int nHeight, LPCTSTR fontName, BOOL bRedraw)
{
if(m_Font)
m_Font->DeleteObject();
if(m_Font == NULL)
m_Font = new CFont();
if(m_Font)
{
if(!m_Font->CreatePointFont(nHeight, fontName))
{
delete m_Font;
m_Font = NULL;
}
}
if(bRedraw && GetSafeHwnd())
Invalidate();
}
void CColorText_Static::Set_BackColor(COLORREF cr)
{
m_BackColor = cr;
b_FillBackColor = true;
m_BackBrush.DeleteObject();
m_BackBrush.CreateSolidBrush(m_BackColor);
m_DisBackBrush.DeleteObject();
m_DisBackBrush.CreateSolidBrush(m_DisBackColro);
if(GetSafeHwnd())
Invalidate();
}
BEGIN_MESSAGE_MAP(CColorText_Static, CStatic)
//{{AFX_MSG_MAP(CColorText_Static)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CColorText_Static message handlers
HBRUSH CColorText_Static::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(m_TextColor);
if(m_Font)
pDC->SelectObject(m_Font);
HBRUSH hBrush = (HBRUSH) m_BackBrush;
if(!IsWindowEnabled())
hBrush = (HBRUSH) m_DisBackBrush;
return ( hBrush );
// TODO: Return a non-NULL brush if the parent's handler should not be called return NULL;
}
BOOL CColorText_Static::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
return TRUE;
//return CStatic::OnEraseBkgnd(pDC);
}
//测试代码
//在对话框上添加静态文本框编辑ID为IDC_COLOR_STATIC
// 在头文件里加
#include "ColorText_Static.h"
//关联变量与控件 (在此建立的测试对话框类为CFfDlg)
//FfDlg.h
public:
CColorText_Static m_ColorText;
//FfDlg.cpp
void CFfDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFfDlg)
DDX_Control(pDX, IDC_COLOR_STATIC, m_ColorText);
//}}AFX_DATA_MAP
}
//初始化控件参数
BOOL CFfDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_ColorText.Set_BackColor(RGB(255,0,0)); //控件背景色
m_ColorText.Set_TextColor(RGB(0,255,0)); //字体颜色
m_ColorText.SetFont(100, _T("宋体")); //设置显示字体和大小
m_ColorText.SetWindowText(_T("测试控件")); //设置显示内容
return true;
}