MFC裡面的CStatusBar是沒法自定義文字顏色的,需要我們自己繪製。這篇文章是在 Display colored text on Status Bar 代碼的基礎上進行改進的,使用起來更方便。 實現新建一個單文本框架的MFC程式,如下圖所示: 在MainFrm.h中,在類定義之前引入頭文件: ...
MFC裡面的CStatusBar是沒法自定義文字顏色的,需要我們自己繪製。這篇文章是在 Display colored text on Status Bar 代碼的基礎上進行改進的,使用起來更方便。
實現新建一個單文本框架的MFC程式,如下圖所示:
在MainFrm.h中,在類定義之前引入頭文件:
#include " ColoredStatusBarCtrl.h"
將MainFrm.h中的m_wndStatusBar換成CColoredStatusBarCtrl類型,代碼如下:
//CStatusBar m_wndStatusBar;
CColoredStatusBarCtrl m_wndStatusBar;
找到MainFrm.cpp中自動生成的數組,代碼如下:
static UINT indicators[] =
{
ID_SEPARATOR, // 狀態行指示器
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
在CMainFrame::OnCreate()函數 return 之前初始化 m_wndStatusBar,代碼如下:
//可以設置自動大小
m_wndStatusBar.AutoSize = true;
//初始化每個窗格
for (int i = 0; i < sizeof(indicators) / sizeof(UINT); i++)
{
CString str;
str = " ";
//更改狀態欄樣式,使其由所有者繪製
bool ret = m_wndStatusBar.GetStatusBarCtrl().SetText(str, i, SBT_OWNERDRAW);
//使每個窗格的大小相同
m_wndStatusBar.SetPaneInfo(i, m_wndStatusBar.GetItemID(i), SBT_OWNERDRAW, 100);
}
//設置字元串
CString str;
str = "123";
m_wndStatusBar.SetText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_NUM), str, RGB(255, 0, 0));
ColoredStatusBarCtrl.h 文件內容如下:
#if !defined(AFX_COLOREDSTATUSBARCTRL_H__4D49433F_617D_45A5_A207_5655A0BE779F__INCLUDED_)
#define AFX_COLOREDSTATUSBARCTRL_H__4D49433F_617D_45A5_A207_5655A0BE779F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif
#include <afxext.h>
class CColoredStatusBarCtrl : public CStatusBar
{
public:
CColoredStatusBarCtrl();
public:
CString m_szStatusString;
public:
bool AutoSize=true;
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
void SetText(UINT nID, CString txt, COLORREF cr);
public:
virtual ~CColoredStatusBarCtrl();
protected:
CMap<UINT, UINT, COLORREF, COLORREF>m_TxtColorMap;
CMap<UINT, UINT, CString, CString>m_TxtMap;
DECLARE_MESSAGE_MAP()
};
#endif
ColoredStatusBarCtrl.cpp 文件內容如下:
// ColoredStatusBarCtrl.cpp : implementation file
//
#include "pch.h"
//#include "dlgviewtest.h"
#include "ColoredStatusBarCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CColoredStatusBarCtrl::CColoredStatusBarCtrl(){}
CColoredStatusBarCtrl::~CColoredStatusBarCtrl(){}
BEGIN_MESSAGE_MAP(CColoredStatusBarCtrl, CStatusBar)
//{{AFX_MSG_MAP(CColoredStatusBarCtrl)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CColoredStatusBarCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// Attach to a CDC object
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
dc.SetBkMode(TRANSPARENT);
// Get the pane rectangle and calculate text coordinates
CRect rect(&lpDrawItemStruct->rcItem);
COLORREF cr;
CString txt;
if (m_TxtColorMap.Lookup(lpDrawItemStruct->itemID, cr))
{
dc.SetTextColor(cr);
}
if (m_TxtMap.Lookup(lpDrawItemStruct->itemID, txt))
{
dc.TextOut(rect.left + 2, rect.top, txt);
if (AutoSize)
{
//設置提示框寬度
CSize sz = dc.GetTextExtent(txt);
SetPaneInfo(lpDrawItemStruct->itemID, lpDrawItemStruct->CtlID, SBT_OWNERDRAW, sz.cx);
}
}
// Detach from the CDC object, otherwise the hDC will be
// destroyed when the CDC object goes out of scope
dc.Detach();
}
void CColoredStatusBarCtrl::SetText(UINT nID,CString txt, COLORREF cr)
{
m_TxtColorMap.SetAt(nID, cr);
m_TxtMap.SetAt(nID, txt);
//觸發重繪
SetPaneText(nID, txt);
}
附件:ColorstatusbarDemo 提取碼: vhrs