C++设置静态文本的颜色

// 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 “CColorText_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);
}

此条目发表在C++分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论