Static Text控件重绘后增加字体颜色设置接口,背景颜色设置接口,字体设置,字体大小设置接口。
继承类H头文件
#pragma once
class CMyStatic : public CStatic
{
DECLARE_DYNAMIC(CMyStatic)
public:
CMyStatic();
virtual ~CMyStatic();
protected:
DECLARE_MESSAGE_MAP()
public:
CFont m_Text;
bool SetTextFont(const LOGFONT& lgfnt);
bool SetTextFont(LONG nHeight, bool bBold, bool bItalic, const CString& sFaceName);
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void m_SetBackClr(COLORREF bKClr);
afx_msg void m_SetTextClr(COLORREF textClr);
afx_msg void m_SetNormalBk(COLORREF nmBkClr);
afx_msg void m_SetNormalText(COLORREF nmTextClr);
afx_msg void m_SetBkColor(COLORREF bKColor);
afx_msg void m_SetNormalBkColor(COLORREF nbKColor);
COLORREF m_BkClr;
COLORREF m_TextClr;
COLORREF m_NormalBkClr;
COLORREF m_NomalTextClr;
COLORREF m_bKColor;
COLORREF m_bKNormalColor;
CBrush m_ClrBrush;
CBrush m_ClrNormalBrush;
BOOL Is_Focused;
BOOL Set_TextColorFlag;
};
继承类CPP实现文件
#include "pch.h"
#include "CMyStatic.h"
IMPLEMENT_DYNAMIC(CMyStatic, CStatic)
CMyStatic::CMyStatic()
{
Is_Focused = FALSE;
Set_TextColorFlag = TRUE;
m_BkClr = RGB(255,255,255);
m_TextClr = RGB(100,130,50);
m_NormalBkClr = RGB(255, 255, 255);
m_NomalTextClr = RGB(10,10,10);
m_bKColor = RGB(0,255,0);
m_bKNormalColor = RGB(255,255,255);
}
CMyStatic::~CMyStatic()
{
}
BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
bool CMyStatic::SetTextFont(const LOGFONT& lgfnt)
{
m_Text.DeleteObject();
m_Text.CreateFontIndirect(&lgfnt);
SetFont(&m_Text, TRUE);
Invalidate();
return true;
}
bool CMyStatic::SetTextFont(LONG nHeight, bool bBold, bool bItalic, const CString& sFaceName)
{
LOGFONT lgfnt;
lgfnt.lfHeight = -MulDiv(nHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
lgfnt.lfWidth = 0;
lgfnt.lfEscapement = 0;
lgfnt.lfOrientation = 0;
lgfnt.lfWeight = bBold ? FW_BOLD : FW_DONTCARE;
lgfnt.lfItalic = bItalic ? TRUE : FALSE;
lgfnt.lfUnderline = FALSE;
lgfnt.lfStrikeOut = FALSE;
lgfnt.lfCharSet = DEFAULT_CHARSET;
lgfnt.lfOutPrecision = OUT_DEFAULT_PRECIS;
lgfnt.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lgfnt.lfQuality = DEFAULT_QUALITY;
lgfnt.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
wcscpy_s(lgfnt.lfFaceName, sFaceName);
return SetTextFont(lgfnt);
}
HBRUSH CMyStatic::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetBkMode(TRANSPARENT);
if (Set_TextColorFlag)
{
pDC->SetBkColor(m_BkClr);
pDC->SetTextColor(m_TextClr);
return(HBRUSH)m_ClrBrush;
}
else
{
pDC->SetBkColor(m_NormalBkClr);
pDC->SetTextColor(m_NomalTextClr);
return(HBRUSH)m_ClrNormalBrush;
}
}
void CMyStatic::OnSetFocus(CWnd* pOldWnd)
{
CStatic::OnSetFocus(pOldWnd);
Is_Focused = TRUE;
}
void CMyStatic::OnKillFocus(CWnd* pNewWnd)
{
CStatic::OnKillFocus(pNewWnd);
Is_Focused = FALSE;
}
void CMyStatic::m_SetBackClr(COLORREF bKClr)
{
m_BkClr = bKClr;
Invalidate();
}
void CMyStatic::m_SetTextClr(COLORREF textClr)
{
m_TextClr = textClr;
Invalidate();
}
void CMyStatic::m_SetNormalBk(COLORREF nmBkClr)
{
m_NormalBkClr = nmBkClr;
Invalidate();
}
void CMyStatic::m_SetNormalText(COLORREF nmTextClr)
{
m_NomalTextClr = nmTextClr;
Invalidate();
}
void CMyStatic::m_SetBkColor(COLORREF bKColor)
{
m_bKColor = bKColor;
m_ClrBrush.Detach();
m_ClrBrush.CreateSolidBrush(m_bKColor);
Invalidate();
}
void CMyStatic::m_SetNormalBkColor(COLORREF nbKColor)
{
m_bKNormalColor = nbKColor;
m_ClrNormalBrush.Detach();
m_ClrNormalBrush.CreateSolidBrush(m_bKNormalColor);
Invalidate();
}