Loading

MFC自定义CStatusBar文字的颜色

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

posted @ 2022-07-31 20:24  二次元攻城狮  阅读(211)  评论(0编辑  收藏  举报