从CStatic中派生出这个类
使用时先在对话框编辑器添加一个static文本框,再直接用classWizard 添加对话框的一个member variable成员CLevelBar, 又或者直接用Create动态生成。
该控件还提供警戒线设定,参数超过警戒线部分会用另一种颜色表示
代码如下:
类定义
/
// CLevelBar.h 参数监视窗
// Coder : Dai Jun scut-ee-425
// Version: 1.0.0
// Created: 2007-07-12
/
class CLevelBar : public CStatic
{
// Construction
public:
CLevelBar();
// Attributes
public:
COLORREF m_bkColor; //背景色
COLORREF m_barColor; //前景色
COLORREF m_barbkColor; //安全剩余区颜色
COLORREF m_txColor; //文字颜色
COLORREF m_warnColor; //危险区前景
COLORREF m_warnbkColor; //危险剩余区颜色
int nPercent; //当前参数百分比(0~100)
protected:
int m_nSlice; //切开份数
char szValue[32]; //标注文字
int nWarn; //危险线百分比(20~100)
CRect m_dwRect; //绘制区rect
// Operations
public:
void SetPercent(int p);
void SetText(char *szText);
void SetSlice(int n);
void SetWarnLevel(int lev);
void SetColorBK(COLORREF color);
void SetColors(COLORREF fore, COLORREF barbk, COLORREF warn, COLORREF warnbk);
protected:
void DrawBackground();
void DrawBar();
void DrawText();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLevelBar)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CLevelBar();
// Generated message map functions
protected:
//{{AFX_MSG(CLevelBar)
afx_msg void OnPaint();
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//
/
实现文件:
CLevelBar::CLevelBar() : m_nSlice(20)
{
m_bkColor = RGB(0,0,0);
m_barColor = RGB(30,250,30);
m_barbkColor = RGB(0,50,0);
m_txColor = m_barColor;
m_warnbkColor = RGB(50,0,0);
m_warnColor = RGB(255,20,20);
nPercent = 0;
nWarn = 100;
m_txColor = (nPercent > nWarn) ? m_warnColor : m_barColor;
strcpy(szValue, " ");
}
CLevelBar::~CLevelBar()
{
}
BEGIN_MESSAGE_MAP(CLevelBar, CStatic)
//{{AFX_MSG_MAP(CLevelBar)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CLevelBar message handlers
void CLevelBar::OnPaint()
{
CPaintDC dc(this); // device context for painting
GetClientRect(&m_dwRect);
//画背景
DrawBackground();
DrawBar();
DrawText();
dc.SelectObject(pOldPen);
}
void CLevelBar::DrawBackground()
{
CClientDC dc(this);
CBrush br;
br.CreateSolidBrush(m_bkColor);
dc.FillRect(&m_dwRect, &br);
br.DeleteObject();
}
//绘制游标
void CLevelBar::DrawBar()
{
CClientDC dc(this);
int barWidth, barLen;
CRect rectBar;
CRect rct;
rectBar.left = m_dwRect.left + 15; //bar区大小
rectBar.right = m_dwRect.right - 15;
rectBar.top = m_dwRect.top + 5;
rectBar.bottom = m_dwRect.bottom - 25;
barWidth = rectBar.Height() / m_nSlice - 1;
barLen = rectBar.Width() / 2 - 2;
//划前景条
CBrush br;
br.CreateSolidBrush(m_barColor);
int i;
int foreSlice = nPercent * m_nSlice / 100;
int warnSlice = nWarn * m_nSlice / 100;
if( (nPercent > nWarn) && (foreSlice == warnSlice))
{
--warnSlice;
}
for(i=0; i < foreSlice; i++)
{
rct.SetRect(rectBar.left + 1, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.left + barLen, rectBar.bottom - (barWidth+1)*i);
dc.FillRect(&rct,&br);
rct.SetRect(rectBar.left+barLen + 2, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.right - 1, rectBar.bottom - (barWidth+1)*i);
dc.FillRect(&rct,&br);
if( (i+1) == warnSlice )
{
++i;
break; //退出循环
}
}
if(foreSlice > i) //划告警部分
{
br.DeleteObject();
br.CreateSolidBrush(m_warnColor);
for( ; i<foreSlice ; i++)
{
rct.SetRect(rectBar.left + 2, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.left + barLen, rectBar.bottom - (barWidth+1)*i );
dc.FillRect(&rct,&br);
rct.SetRect(rectBar.left+barLen+2, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.right - 1 , rectBar.bottom - (barWidth+1)*i );
dc.FillRect(&rct,&br);
}
}
br.DeleteObject();
br.CreateSolidBrush(m_barbkColor);
//话背景条
for( ; i<m_nSlice && i<warnSlice ; i++)
{
rct.SetRect(rectBar.left + 1, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.left + barLen, rectBar.bottom - (barWidth+1)*i);
dc.FillRect(&rct,&br);
rct.SetRect(rectBar.left + barLen + 2, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.right - 1, rectBar.bottom - (barWidth+1)*i);
dc.FillRect(&rct,&br);
}
br.DeleteObject();
br.CreateSolidBrush(m_warnbkColor);
//话告警背景
for( ; i<m_nSlice; i++)
{
rct.SetRect(rectBar.left + 2, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.left + barLen, rectBar.bottom - (barWidth+1)*i);
dc.FillRect(&rct,&br);
rct.SetRect(rectBar.left+barLen+2, rectBar.bottom - barWidth - (barWidth+1)*i,
rectBar.right - 1, rectBar.bottom - (barWidth+1)*i );
dc.FillRect(&rct,&br);
}
br.DeleteObject();
}
void CLevelBar::DrawText()
{
CClientDC dc(this);
CRect rct;
rct.top = m_dwRect.bottom - 20;
rct.left = m_dwRect.left ;
rct.right = m_dwRect.right ;
rct.bottom = m_dwRect.bottom - 5;
dc.SetBkColor(m_bkColor);
dc.SetTextColor(m_txColor);
dc.DrawText(CString(szValue), &rct, DT_CENTER);
}
void CLevelBar::SetText(char *szText)
{
strcpy(szValue, szText);
}
void CLevelBar::SetSlice(int n)
{
m_nSlice = (n>50) ? 50 : n;
}
void CLevelBar::SetPercent(int p)
{
nPercent = (p>100 || p<0) ? 0 : p;
m_txColor = (nPercent > nWarn) ? m_warnColor : m_barColor;
}
void CLevelBar::SetWarnLevel(int lev)
{
nWarn = (lev>100 || lev<20) ? 100 : lev;
}
void CLevelBar::SetColorBK(COLORREF color)
{
m_bkColor = color;
}
void CLevelBar::SetColors(COLORREF fore, COLORREF barbk, COLORREF warn, COLORREF warnbk)
{
m_barColor = fore;
m_barbkColor = barbk;
m_warnColor = warn;
m_warnbkColor = warnbk;
m_txColor = (nPercent > nWarn) ? m_warnColor : m_barColor;
}