颜色和背景颜色"/>
VS 2019 MFC CStatusBar 自绘制继承CStatusBar 类可改变每个Panel的字体颜色和背景颜色
宏定义
#define STR_COMMUNICATION_PLC _T("PLC连接:")
#define STR_COMMUNICATION_PLC_PORT _T("PLC端口:")
#define STR_COMMUNICATION_PLC_BAUD _T("通讯频率:")
#define STR_COMMUNICATION_SERVERID _T("PLC地址:")
#define STR_COMMUNICATION_SERVERID _T("PLC地址:")
#define STR_COMMUNICATION_DATABIT _T("通讯数据位:")
#define STR_COMMUNICATION_CHECKBIT _T("通讯校验:")
#define STR_COMMUNICATION_STOPBIT _T("通讯停止位:")
#define STR_COMMUNICATION_MC408 _T("MC408连接:")
#define STR_COMMUNICATION_PORT408 _T("MC408端口:")
#define STR_COMMUNICATION_BAUD408 _T("MC408波特率:")
#define STR_COMMUNICATION_IPADDR408 _T("MC408 IP地址:")
#define STR_SYSTEM_TIME _T("")
#define STR_SYSTEM_RUN _T("RUN:")
#define STR_SYSTEM_STOP _T("STOP:")
#define SYSTEM_STATUSBAR_PANEL_NUM 14
#define SYSTEM_STATUS_COLOR RGB(120,120,120)
#define SYSTEM_STATUS_COLOR_ALARM RGB(255,0,0)
#define SYSTEM_STATUS_COLOR_RUN RGB(0,255,0)
#define SYSTEM_STATUS_COLOR_TIME RGB(0,0,255)
#define SYSTEM_RUN_STASUS _T("")
继承类H头文件
#pragma once
#include"MacroDefinition.h"class CMyStatusBar: public CStatusBar
{DECLARE_DYNAMIC(CMyStatusBar)
public:CMyStatusBar();
public://protected:COLORREF *Color; CString strText;CString *str;int m_StatusBarPanelNum;COLORREF *AllbKcolor;
public:void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);void SetPaneText(int nIndex, LPCTSTR lpszNewText, COLORREF crItemColor);void SetPanelNumber(int nPanels);void SetSinglePanelBackGroudColor(int nIndex,COLORREF bKCtr);public:virtual ~CMyStatusBar();protected:DECLARE_MESSAGE_MAP()
};
继承类CPP文件
#include "pch.h"
#include "DoublePathWayReflowerController.h"
#include "CMyStatusBar.h"IMPLEMENT_DYNAMIC(CMyStatusBar, CStatusBar)CMyStatusBar::CMyStatusBar()
{m_StatusBarPanelNum =0 ;SetPanelNumber(0);Color = NULL;str = NULL;AllbKcolor=NULL;
}CMyStatusBar::~CMyStatusBar()
{delete[] str;delete[] Color;delete[] AllbKcolor;
}
BEGIN_MESSAGE_MAP(CMyStatusBar, CStatusBar)
END_MESSAGE_MAP()
void CMyStatusBar::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{UINT unID = lpDrawItemStruct->itemID;CDC dc;CDC* pDc = CDC::FromHandle(lpDrawItemStruct->hDC);int nSaveDC = pDc->SaveDC();dc.Attach(lpDrawItemStruct->hDC);dc.SetBkMode(TRANSPARENT);CRect rect(&lpDrawItemStruct->rcItem);CRect Lrect;for (int i = 0; i < m_StatusBarPanelNum; i++){CBrush pBrush;pBrush.CreateSolidBrush(AllbKcolor[i]);dc.SetTextColor(Color[i]);GetItemRect(i, &Lrect);dc.FillRect(&Lrect, &pBrush);dc.DrawText(str[i], &Lrect, DT_LEFT);}pDc->RestoreDC(nSaveDC);dc.Detach();}void CMyStatusBar::SetPaneText(int nIndex, LPCTSTR lpszNewText, COLORREF crItemColor, COLORREF bKctr)
{if (nIndex<= m_StatusBarPanelNum-1 ){strText = lpszNewText;Color[nIndex] = crItemColor;GetStatusBarCtrl().SetText(strText, nIndex, SBPS_STRETCH | SBT_OWNERDRAW /*| SBT_NOBORDERS*/);str[nIndex] = strText;SetSinglePanelBackGroudColor(nIndex, bKctr);Invalidate();}else{MessageBox(_T("SetPaneText-> 设置状态栏nIndex索引数大于实际索引数!\n"), _T("信息提示!"), MB_OK | MB_OKCANCEL | MB_ICONINFORMATION);printf("SetPaneText-> 设置状态栏nIndex索引数大于实际索引数!\n");}
}void CMyStatusBar::SetPanelNumber(int nPanels)
{m_StatusBarPanelNum = nPanels;if (Color==NULL)Color = new COLORREF[m_StatusBarPanelNum];if(str==NULL)str = new CString[m_StatusBarPanelNum];if (AllbKcolor==NULL)AllbKcolor = new COLORREF[m_StatusBarPanelNum];
}void CMyStatusBar::SetSinglePanelBackGroudColor(int nIndex, COLORREF bKCtr)
{AllbKcolor[nIndex] = bKCtr;
}
工程H文件中定义CMyStatusBar m_DlgMainStatusBar;
CProgressCtrl m_DlgMainProgressBar;工程应用CPP文件创建状态栏并在状态栏中创建进度条
afx_msg void OnCreateStatusBar();
afx_msg void OnWriteTextToStatusPanel(int nIndex,CString strText,COLORREF textClr);在OnInitDialog中调用OnCreateStatusBar()
void CDoublePathWayReflowerControllerDlg::OnCreateStatusBar()
{CRect rcOfStatus;int rcPanelWith;this->GetClientRect(rcOfStatus);if (!m_DlgMainStatusBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, AFX_IDW_STATUS_BAR)){TRACE0("Failed to create status bar1\n");AfxMessageBox(_T("OnCreateStatusBar->状态栏无法创建或创建失败!\n!"), MB_ICONERROR | MB_ICONINFORMATION);printf("OnCreateStatusBar->状态栏无法创建或创建失败!\n");}m_DlgMainStatusBar.SetIndicators(CStatusBar_Indicators, sizeof(CStatusBar_Indicators) / sizeof(UINT));m_DlgMainStatusBar.MoveWindow(0, rcOfStatus.bottom - 20, rcOfStatus.right, 20);rcPanelWith = rcOfStatus.right / SYSTEM_STATUSBAR_PANEL_NUM;m_DlgMainStatusBar.SetPanelNumber(SYSTEM_STATUSBAR_PANEL_NUM);m_DlgMainStatusBar.SetPaneInfo(SYSTEM_STATUSBAR_PANEL_NUM - 2, ID_PROGRESS_STATUS, SBPS_STRETCH | SBPS_NOBORDERS | SBPS_OWNERDRAW, rcPanelWith);m_DlgMainStatusBar.SetPaneText(0, STR_COMMUNICATION_PLC, SYSTEM_STATUS_COLOR_ALARM, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(1, STR_COMMUNICATION_PLC_PORT, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(2, STR_COMMUNICATION_PLC_BAUD, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(3, STR_COMMUNICATION_SERVERID, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(4, STR_COMMUNICATION_DATABIT, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(5, STR_COMMUNICATION_CHECKBIT, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(6, STR_COMMUNICATION_STOPBIT, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(7, STR_COMMUNICATION_MC408, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(8, STR_COMMUNICATION_PORT408, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(9, STR_COMMUNICATION_BAUD408, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(10, STR_COMMUNICATION_IPADDR408, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(11, STR_SYSTEM_TIME, SYSTEM_STATUS_COLOR, GetSysColor(COLOR_3DFACE));m_DlgMainStatusBar.SetPaneText(13, _T("RUN"), SYSTEM_STATUS_COLOR_RUN, GetSysColor(COLOR_3DFACE));for (int i = 0; i < SYSTEM_STATUSBAR_PANEL_NUM; i++){m_DlgMainStatusBar.SetPaneInfo(i, i, SBPS_STRETCH /*| SBPS_NOBORDERS*/ | SBPS_OWNERDRAW, rcPanelWith);//m_DlgMainStatusBar.SetSinglePanelBackGroudColor(i,GetSysColor(COLOR_3DFACE));}CRect Lrect;m_DlgMainStatusBar.GetItemRect(SYSTEM_STATUSBAR_PANEL_NUM - 2, &Lrect);if (m_DlgMainProgressBar.m_hWnd){m_DlgMainProgressBar.MoveWindow(Lrect);}else{m_DlgMainProgressBar.Create(WS_CHILD | WS_VISIBLE | PBS_SMOOTHREVERSE | PBS_SMOOTH, Lrect, &m_DlgMainStatusBar, 3000);m_DlgMainProgressBar.SetRange(0, 100);m_DlgMainProgressBar.SetPos(50);}
}
Onsize()
void CDoublePathWayReflowerControllerDlg::OnSize(UINT nType, int cx, int cy)
{CDialogEx::OnSize(nType, cx, cy);CRect rcNewWindow;int rcPanelWithNew;this->GetClientRect(rcNewWindow);rcPanelWithNew = rcNewWindow.right / SYSTEM_STATUSBAR_PANEL_NUM;m_ResizeDlg.Move();RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);CWnd* m_pWND = AfxGetApp()->GetMainWnd();if (m_pWND!=NULL){}if (m_DlgMainStatusBar.m_hWnd){m_DlgMainStatusBar.MoveWindow(0, rcNewWindow.bottom - 20, rcNewWindow.right, 20);for (int i = 0; i < SYSTEM_STATUSBAR_PANEL_NUM; i++){m_DlgMainStatusBar.SetPaneInfo(i,0,0, /*i, SBPS_STRETCH | SBPS_NOBORDERS | SBPS_OWNERDRAW,*/ rcPanelWithNew);}CRect Lrect;m_DlgMainStatusBar.GetItemRect(SYSTEM_STATUSBAR_PANEL_NUM-2, &Lrect);if (m_DlgMainProgressBar.m_hWnd){m_DlgMainProgressBar.MoveWindow(Lrect);}}}
OnWriteTextToStatusPanel(int nIndex, CString strText, COLORREF textClr)函数用来给状态栏Panel写入对应的文本,包括各自的文本颜色。
void CDoublePathWayReflowerControllerDlg::OnWriteTextToStatusPanel(int nIndex, CString strText, COLORREF textClr)
{CString strTitle = _T("");if (m_DlgMainStatusBar.m_hWnd){switch (nIndex){case 0:strTitle = STR_COMMUNICATION_PLC;break;case 1:strTitle = STR_COMMUNICATION_PLC_PORT;break;case 2:strTitle = STR_COMMUNICATION_PLC_BAUD;break;case 3:strTitle = STR_COMMUNICATION_SERVERID;break;case 4:strTitle = STR_COMMUNICATION_DATABIT;break;case 5:strTitle = STR_COMMUNICATION_CHECKBIT;break;case 6:strTitle = STR_COMMUNICATION_STOPBIT;break;case 7:strTitle = STR_COMMUNICATION_MC408;break;case 8:strTitle = STR_COMMUNICATION_PORT408;break;case 9:strTitle = STR_COMMUNICATION_BAUD408;break;case 10:strTitle = STR_COMMUNICATION_IPADDR408;break;case 11:strTitle = STR_SYSTEM_TIME;break;case 13:strTitle = SYSTEM_RUN_STASUS;break;default:break;}m_DlgMainStatusBar.SetPaneText(nIndex, strTitle+strText, textClr);}
效果图
更多推荐
VS 2019 MFC CStatusBar 自绘制继承CStatusBar 类可改变每个Panel的字体颜色和背景颜色
发布评论