📄 wraningcontrol.cpp
字号:
// WraningControl.cpp : implementation file
//
#include "stdafx.h"
#include "DK20DieselizeDynamotor.h"
#include "WraningControl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWraningControl
CWraningControl::CWraningControl()
{
// m_Ownstatic =NULL ;
// m_font =NULL ;
strText =_T("TITLE") ;
iItemcount = 0 ; //条目数为0
iItemheight = 26 ; //条目缺省高度
iTopheight = 36 ;
iItemspace = 2 ;
m_crBegin = RGB(15,124,170);
m_crEnd =RGB(251,254,203) ;
m_crtitleEnd =RGB(186,223,226) ;
m_crtitleBegin =RGB(92,110,111);
}
CWraningControl::~CWraningControl()
{
// if (m_Ownstatic !=NULL)
// delete []m_Ownstatic ;
// m_Ownstatic =NULL ;
}
BEGIN_MESSAGE_MAP(CWraningControl, CWnd)
//{{AFX_MSG_MAP(CWraningControl)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWraningControl message handlers
void CWraningControl::OnPaint()
{
CPaintDC dc(this); // device context for painting
CBitmap bitmap ;
CRect rcTop , rcBottom ; //控件矩形, 标题矩形 , 标题以下矩形
CFont m_fontControl ; //报警控件台头字体
VERIFY(m_fontControl.CreateFont(
20, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("黑体"))); // lpszFacename
GetClientRect(&rcClient) ;
rcTop = rcClient ;
rcBottom =rcClient ;
rcTop.bottom = rcTop.top + iTopheight ; //
rcBottom.top = rcTop.bottom ;
bitmap.CreateCompatibleBitmap(&dc , rcClient.Width() , rcClient.Height()) ;
CDC memDc ;
memDc.CreateCompatibleDC(&dc ) ;
CBitmap *oldbitmap = memDc.SelectObject(&bitmap) ;
memDc.FillSolidRect(&rcClient , RGB(0,0,0)) ;
memDc.SetBkMode(TRANSPARENT) ;
rcClient.InflateRect(1,1,1,1) ;
rcClient.DeflateRect(1,1,1,1) ;
memDc.SetTextColor(RGB(0,0,0) );
//渐变标题头
rcTop.DeflateRect(2,2,2,2) ;
memDc.FillSolidRect( &rcTop , RGB(0,0,0)) ;
CRect rcTmp ;
rcTmp =rcTop ;
rcTmp.bottom = rcTmp.Height() /2 + rcTmp.top ;
rcTmp.DeflateRect(1,1,1,0) ;
DrawGradientFill(&memDc , &rcTmp , m_crtitleBegin, m_crtitleEnd ,5) ;
rcTmp =rcTop ;
rcTmp.top = rcTmp.Height() /2 + rcTmp.top ;
rcTmp.DeflateRect(1,0,1,2) ;
DrawGradientFill(&memDc , &rcTmp , m_crtitleEnd, m_crtitleBegin ,5) ;
rcTop.DeflateRect(-2,-2,-2,-2) ;
CFont * oldFont = memDc.SelectObject( &m_fontControl ) ;
memDc.DrawText(strText , &rcTop , DT_CENTER |DT_VCENTER) ;
memDc.SelectObject(oldFont) ;
m_fontControl.DeleteObject() ;
dc.BitBlt(0,0,rcClient.Width() , rcClient.Height() ,&memDc , 0,0,SRCCOPY) ;
memDc.SelectObject(oldbitmap) ;
bitmap.DeleteObject() ;
memDc.DeleteDC() ;
DeleteObject(oldbitmap) ; oldbitmap =NULL ;
DeleteObject(oldFont) ; oldFont =NULL ;
ReleaseDC(&dc) ;
}
void CWraningControl::DrawGradientFill(CDC *pDC, CRect *pRect, COLORREF crStart, COLORREF crEnd, int nSegments)
{
COLORREF cr;
int nR = GetRValue(crStart);
int nG = GetGValue(crStart);
int nB = GetBValue(crStart);
int neB = GetBValue(crEnd);
int neG = GetGValue(crEnd);
int neR = GetRValue(crEnd);
if(nSegments > pRect->Height())
nSegments = pRect->Height();
int nDiffR = (neR - nR);
int nDiffG = (neG - nG);
int nDiffB = (neB - nB);
int ndR = 256 * (nDiffR) / (max(nSegments,1));
int ndG = 256 * (nDiffG) / (max(nSegments,1));
int ndB = 256 * (nDiffB) / (max(nSegments,1));
nR *= 256;
nG *= 256;
nB *= 256;
neR *= 256;
neG *= 256;
neB *= 256;
int nCY = pRect->Height() / max(nSegments,1), nTop = pRect->top , nBottom =nTop;
CPen *hpen =(CPen*) pDC->SelectStockObject(NULL_PEN);
for (int i = 0; i < nSegments; i++, nR += ndR, nG += ndG, nB += ndB)
{
if (i == (nSegments - 1))
nTop = pRect->bottom;
else
nTop += nCY;
cr = RGB(nR / 256, nG / 256, nB / 256);
CBrush br(cr);
CBrush* pbrOld = pDC->SelectObject(&br);
pDC->Rectangle(pRect->left, nTop+1, pRect->right , nBottom);
pDC->SelectObject(pbrOld);
pbrOld =NULL ;
br.DeleteObject() ;
DeleteObject(pbrOld) ;
nBottom = nTop ;
}
pDC->SelectObject(hpen) ;
DeleteObject(hpen) ;
}
#define IDINCRESE WM_USER+3000
//初始化条目控件
BOOL CWraningControl::Init(int iNum , CRect rcIn)
{
iItemcount = iNum ;
rcClient = rcIn ;
int iTop = iTopheight +2 ;
for(int i = 0 ; i < iNum ; i++)
{
CRect rc ;
rc.top = iTop ;
rc.bottom = rc.top + iItemheight ;
rc.left = 2 ;
rc.right = rcIn.Width() -2 ;
m_Ownstatic[i].Create(NULL ,_T("") ,WS_CHILD|WS_VISIBLE ,rc,this , IDINCRESE+i,NULL ) ;
m_Ownstatic[i].strTextleft =_T("") ;
m_Ownstatic[i].strTextright =_T("") ;
iTop = rc.bottom + iItemspace ;
}
return TRUE ;
}
//刷新相应的项
void CWraningControl::Refreshs(int iIndexitem)
{
for (int i =0 ; i < iItemcount ; i++)
{
m_Ownstatic[i].InvalidateRect(m_Ownstatic[i].rcInvali , FALSE) ;
m_Ownstatic[i].UpdateWindow() ;
}
}
//设定渐变颜色
void CWraningControl::SetGradientColor(COLORREF crBegin, COLORREF crEnd)
{
if (m_Ownstatic !=NULL)
m_Ownstatic[0].SetGradientColor(crBegin ,crEnd ) ;
}
//设定抬头渐变颜色
void CWraningControl::SetTitleGradientColor(COLORREF crBegin, COLORREF crEnd)
{
m_crtitleBegin = crBegin ;
m_crtitleEnd = crEnd ;
}
//设定项目
void CWraningControl::SetItem(int iIndex , CString strLeft, float fRight, int Status)
{
if (iIndex>=0 && iIndex <iItemcount )
{
CString strTmp ;
strTmp.Format(_T("%.2f") ,fRight ) ;
m_Ownstatic[iIndex].iStatus = Status ;
m_Ownstatic[iIndex].strTextleft = strLeft;
m_Ownstatic[iIndex].strTextright = strTmp;
// Refreshs(iIndex) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -