📄 ownstatic.cpp
字号:
// OwnStatic.cpp : implementation file
//
#include "stdafx.h"
#include "DK20DieselizeDynamotor.h"
#include "OwnStatic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COwnStatic
BOOL bCreated =0 ;
COwnStatic::COwnStatic()
{
strTextleft = _T("NO TEXT") ;
strTextright = _T("NO VALUE") ;
//报警灯的3种颜色
crRed = RGB(255 , 0,0) ;
crGreen = RGB(0,200,0) ;
crDarkgreen = RGB(0,126 , 0) ;
crDarkred = RGB(242,168 ,13) ;
bRed = 1 ; //红色 0 暗红色
bTime = 0 ;
iStatus = 0 ; //正常状态 绿色 , 1 深绿色 , 2 红色,3 闪烁的红色
m_crBegin = RGB(3,148,255);//(15,124,170);
m_crEnd =RGB(251,254,203) ;
bCreated =0 ;
}
COwnStatic::~COwnStatic()
{
/*if (!bTime)
KillTimer(1) ; */
}
BEGIN_MESSAGE_MAP(COwnStatic, CWnd)
//{{AFX_MSG_MAP(COwnStatic)
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COwnStatic message handlers
// 文字跟报警颜色 4:3 的宽度比
void COwnStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC memDc ;
CBitmap bitmap ;
CRect rcClient ,rcClientleft , rcClientright ;
GetClientRect(&rcClient) ;
bitmap.CreateCompatibleBitmap(&dc , rcClient.Width() , rcClient.Height()) ;
memDc.CreateCompatibleDC(&dc) ;
CBitmap *oldBitmap = memDc.SelectObject(&bitmap) ;
memDc.SetBkMode(TRANSPARENT) ;
memDc.FillSolidRect(rcClient ,RGB(0,0,0)) ;
rcClientleft = rcClient ;
rcClientright = rcClient ;
rcClientleft.right = rcClient.left + rcClient.Width() *3 /4 ; //报警色与文字的分界
rcClientright.left = rcClientleft.right-1 ;
rcInvali =rcClientright ;
//左半部
memDc.SetTextColor(RGB(0,0,0)) ;
memDc.FillSolidRect(&rcClient, m_crEnd) ;
CRect rcTmp ;
rcTmp =rcClientleft ;
rcTmp.bottom = rcTmp.Height() /2 + rcTmp.top ;
rcTmp.DeflateRect(1,1,1,0) ;
DrawGradientFill(&memDc , &rcTmp ,m_crBegin, m_crEnd ,10) ;
rcTmp =rcClientleft ;
rcTmp.top = rcTmp.Height() /2 + rcClientleft.top ;
rcTmp.DeflateRect(1,0,1,2) ;
DrawGradientFill(&memDc , &rcTmp , m_crEnd, m_crBegin ,10) ;
CFont m_fontControl ;
VERIFY(m_fontControl.CreateFont(
13, // 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
CFont * oldFont = memDc.SelectObject( &m_fontControl ) ;
//计算字符个数
CString str(_T("佛")) ;
CRect rcStr ;
CRect rcRc1 , rcRc2 ;
memDc.DrawText(str ,&rcStr , DT_CALCRECT ) ;
int chracterNum = (int)(rcClientleft.Width() / rcStr.Width());
if (strTextleft.GetLength() * rcStr.Width() > rcClientleft.Width()) //需要换行
{
rcRc1 = rcClientleft ;
rcRc1.bottom = rcClientleft.Height()/2 + rcClientleft.top ;
memDc.DrawText(strTextleft.Left(chracterNum+10) ,rcRc1, DT_VCENTER |DT_CENTER) ;
rcRc2 = rcClientleft ;
rcRc2.top = rcClientleft.Height() /2 + rcClientleft.top ;
memDc.DrawText(strTextleft.Right(strTextleft.GetLength()-chracterNum-10) ,rcRc2, DT_VCENTER |DT_CENTER) ;
}else
memDc.DrawText(strTextleft ,rcClientleft, DT_VCENTER |DT_CENTER) ;
memDc.SelectObject(oldFont) ;
DeleteObject(oldFont) ;oldFont =NULL ;
m_fontControl.DeleteObject() ;
//右半部
memDc.SetTextColor(RGB(255,255,255)) ;
rcClientright.DeflateRect(1,1,1,1) ;
switch(iStatus) {
case 0:
memDc.FillSolidRect(&rcClientright , crGreen) ; //右边背景色
if (bTime)
KillTimer(1) ;
bTime = 0 ;
break;
case 1:
memDc.FillSolidRect(&rcClientright , crDarkgreen) ; //右边背景色
if (bTime)
KillTimer(1) ;
bTime = 0 ;
break;
case 2:
memDc.FillSolidRect(&rcClientright , crRed) ; //右边背景色
if (bTime)
KillTimer(1) ;
bTime = 0 ;
break;
case 3:
if (bRed)
memDc.FillSolidRect(&rcClientright , crRed) ; //右边背景色
else
memDc.FillSolidRect(&rcClientright , crDarkred) ; //右边背景色
if (!bTime)
SetTimer(1,500,NULL ) ;
bTime = 1 ;
break;
}
rcClientright.DeflateRect(-1,-1,-1,-1) ;
memDc.DrawText(strTextright ,rcClientright,DT_CENTER|DT_VCENTER ) ;
dc.BitBlt(0,0,rcClient.Width() , rcClient.Height() , &memDc , 0,0,SRCCOPY) ;
memDc.SelectObject(oldBitmap) ;
bitmap.DeleteObject() ;
DeleteObject(oldBitmap) ; oldBitmap =NULL ;
memDc.DeleteDC() ;
ReleaseDC(&dc) ;
}
void COwnStatic::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);
br.DeleteObject() ;
DeleteObject(pbrOld) ;
pbrOld =NULL ;
nBottom = nTop ;
}
pDC->SelectObject(hpen) ;
DeleteObject(hpen) ;
hpen = NULL ;
}
//设定渐变颜色
void COwnStatic::SetGradientColor(COLORREF crBegin, COLORREF crEnd)
{
m_crBegin = crBegin ;
m_crEnd = crEnd ;
}
void COwnStatic::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
bRed = !bRed ;
InvalidateRect(&rcInvali , FALSE) ;
CWnd::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -