189 8069 5689

MFC自定义CStatusBar文字的颜色

MFC里面的CStatusBar是没法自定义文字颜色的,需要我们自己绘制。这篇文章是在 Display colored text on Status Bar 代码的基础上进行改进的,使用起来更方便。
实现新建一个单文本框架的MFC程序,如下图所示:

在MainFrm.h中,在类定义之前引入头文件:

成都地区优秀IDC服务器托管提供商(创新互联).为客户提供专业的服务器机柜租赁,四川各地服务器托管,服务器机柜租赁、多线服务器托管.托管咨询专线:18982081108

#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__4DF_617D_45A5_A207_5655A0BE779F__INCLUDED_)
#define AFX_COLOREDSTATUSBARCTRL_H__4DF_617D_45A5_A207_5655A0BE779F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif 

#include 

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:	
	CMapm_TxtColorMap;
	CMapm_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


网站标题:MFC自定义CStatusBar文字的颜色
文章分享:http://jkwzsj.com/article/dsoigjo.html

其他资讯