首先看看效果
然后看如何实现
自定义CTextStatic类
头文件
#define NORMAL_COLOR RGB(0,0,255)
#define ABNORMAL_COLOR RGB(200,0,0)
class CTextStatic : public CStatic
{
// Construction
public:
CTextStatic();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTextStatic)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CTextStatic();
// Generated message map functions
protected:
//{{AFX_MSG(CTextStatic)
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
LOGFONT m_LogFontTitle;
UINT m_nFormat;
DWORD m_dwColor;
void SetTitleText(CString sTitle);
private:
CString m_sTitleText;
};
源文件
CTextStatic::CTextStatic()
{
m_LogFontTitle.lfHeight = 20;
m_LogFontTitle.lfWeight = 400;
m_LogFontTitle.lfOrientation= 0;
m_LogFontTitle.lfEscapement = 0;
m_LogFontTitle.lfWidth = 10;
m_LogFontTitle.lfItalic = false;
m_LogFontTitle.lfUnderline = false;
m_LogFontTitle.lfStrikeOut = false;
m_LogFontTitle.lfCharSet = GB2312_CHARSET;
m_LogFontTitle.lfOutPrecision = PROOF_QUALITY;
m_LogFontTitle.lfClipPrecision = CLIP_DEFAULT_PRECIS;
m_LogFontTitle.lfQuality = PROOF_QUALITY;
m_LogFontTitle.lfPitchAndFamily = DEFAULT_PITCH;
strcpy(m_LogFontTitle.lfFaceName,"黑体");
m_nFormat = DT_SINGLELINE|DT_LEFT |DT_VCENTER|DT_END_ELLIPSIS;
m_sTitleText = "test";
m_dwColor = RGB(0,0,255);
}
CTextStatic::~CTextStatic()
{
}
BEGIN_MESSAGE_MAP(CTextStatic, CStatic)
//{{AFX_MSG_MAP(CTextStatic)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CTextStatic message handlers
void CTextStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
RECT rect;
GetClientRect(&rect);
HFONT hFont = ::CreateFontIndirect(&m_LogFontTitle);
HFONT hOldFont = (HFONT)::SelectObject(dc.m_hDC,hFont);
dc.SetTextColor(m_dwColor);//画文字
dc.SetBkMode(TRANSPARENT);
dc.DrawText(m_sTitleText,&rect,m_nFormat);
hFont = (HFONT)::SelectObject(dc.m_hDC,hOldFont);
::DeleteObject(hFont);
::DeleteObject(hOldFont);
// Do not call CStatic::OnPaint() for painting messages
}
void CTextStatic::SetTitleText(CString sTitle)
{
m_sTitleText = sTitle;
}
对话框类
头文件
#include "TextStatic.h"
class CFaultShowByColorDlg : public CDialog
{
// Construction
public:
CFaultShowByColorDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CFaultShowByColorDlg)
enum { IDD = IDD_FAULTSHOWBYCOLOR_DIALOG };
CTextStatic m_stc5;
CTextStatic m_stc4;
CTextStatic m_stc3;
CTextStatic m_stc2;
CTextStatic m_stc1;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFaultShowByColorDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CFaultShowByColorDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
void InitUi();
CTextStatic *arrayTest[5];
};
源文件
void CFaultShowByColorDlg::InitUi()
{
m_stc1.SetTitleText("1.故障显示1");
m_stc2.SetTitleText("2.故障显示2");
m_stc3.SetTitleText("3.故障显示3");
m_stc4.SetTitleText("4.故障显示4");
m_stc5.SetTitleText("5.故障显示5");
arrayTest[0] = &m_stc1;
arrayTest[1] = &m_stc2;
arrayTest[2] = &m_stc3;
arrayTest[3] = &m_stc4;
arrayTest[4] = &m_stc5;
BYTE test_value = 11;
CRect rect;
for (int i=0; i<5; i++)
{
if (test_value%2 == 1)
{
arrayTest[i]->m_dwColor = ABNORMAL_COLOR;
}
else
{
arrayTest[i]->m_dwColor = NORMAL_COLOR;
}
arrayTest[i]->GetWindowRect(&rect);
ScreenToClient(&rect);
InvalidateRect(&rect,TRUE);
UpdateWindow();
test_value = test_value/2;
}
}