⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 led.cpp

📁 485通讯接口模块的编程应用
💻 CPP
字号:
// Led.cpp : implementation file
//

#include "stdafx.h"
#include "DK20DieselizeDynamotor.h"
#include "Led.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLed

//初始化一些变量 
CLed::CLed()
{
	colorCircle1 = RGB(255,91,10) ;     
	colorCircle2 = RGB(37,219,13) ; 
	colorCircle3 = RGB(252,138,116) ;     //绿色
	colorText1   = RGB(255,255,255)  ;  //白色
	colorText2   = RGB(255,91,10)  ;    //红色
	colorBK      = RGB(173,211,198);//(31,78,100) ; 
	
	fScale = 0.6              ; //
	iStatus = 2; 
    bTimer = 0 ; 
	bRed = 0 ; 
	strText =_T("TEST11111111") ; 

}

CLed::~CLed()
{
}


BEGIN_MESSAGE_MAP(CLed, CStatic)
	//{{AFX_MSG_MAP(CLed)
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLed message handlers
//***        控件说明
//***绘制的报警灯大小约为总控件的一定比例lScale 下面为显示的标题
//***可以修改参数 : 背景色 , 文字色1 , 圆圈占总图形比例 , 
//***               文字色2, 圆圈色1 ,圆圈色2, 圆圈色3 ,
//***               报警上限 ,报警下限,当前值  ,状态(1红色 2绿色 3闪烁)
//***工作原理
//***根据设定的上限与下限判断给定什么颜色,同时进行状态分配

void CLed::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
 	// TODO: Add your message handler code here
 	// Do not call CStatic::OnPaint() for painting messages
    CDC memdc  ; 
	CBitmap bitmap ; 
	CRect rc ; 
	
	GetClientRect(&rc) ; 
	memdc.CreateCompatibleDC(&dc) ; 
	bitmap.CreateCompatibleBitmap(&dc,rc.Width() , rc.Height()) ; 
	CBitmap *oldBitmap = memdc.SelectObject(&bitmap) ; 
	memdc.FillSolidRect(&rc , colorBK)  ; 
 	CBrush brush ; 
	CBrush *oldBrush ; 
	CRect rcCircle ; 
	
	rcCircle = rc ; 
	rcCircle.left = rc.left+(int)(rc.Width()*(1.0f-fScale)/2.0f) ; 
	rcCircle.right= rcCircle.left + (int)((rc.Width()*fScale)) ; 
	rcCircle.top  = rcCircle.left; 
    rcCircle.bottom= rcCircle.right ; 
	
	rcRefresh  = rcCircle ; 
	

	switch(iStatus) 
	{
	case 1:
		brush.CreateSolidBrush(colorCircle1) ; 
	    oldBrush =memdc.SelectObject(&brush)  ; 
        memdc.Ellipse(&rcCircle) ; 
 		memdc.SelectObject(oldBrush)  ; 
		brush.DeleteObject() ; 
 		DeleteObject(oldBrush) ; 
		if (bTimer)
			KillTimer(1) ; 
		bTimer = 0 ; 
		
      	break;
	case 2:
		brush.CreateSolidBrush(colorCircle2) ; 
		oldBrush =memdc.SelectObject(&brush)  ; 
        memdc.Ellipse(&rcCircle) ; 
		memdc.SelectObject(oldBrush)  ; 
		brush.DeleteObject() ; 
		DeleteObject(oldBrush) ; 

		if (bTimer)
			KillTimer(1) ; 
		bTimer = 0 ; 
		
 		break;
	case 3:
 		if (bRed)
		{
			brush.CreateSolidBrush(colorCircle1) ; 
			oldBrush =memdc.SelectObject(&brush)  ; 
			memdc.Ellipse(&rcCircle) ; 
			memdc.SelectObject(oldBrush)  ; 
			brush.DeleteObject() ; 
			DeleteObject(oldBrush) ; 
		}
 		else
		{
			brush.CreateSolidBrush(colorCircle3) ; 
			oldBrush =memdc.SelectObject(&brush)  ; 
			memdc.Ellipse(&rcCircle) ; 
			memdc.SelectObject(oldBrush)  ; 
			brush.DeleteObject() ; 
			DeleteObject(oldBrush) ; 
			

		}
  		if (!bTimer)
			SetTimer(1,500,0) ;
        bTimer = 1  ; 
  		break;

 
	}
     
	//绘制文字
	CFont m_fontControl; 
	VERIFY(m_fontControl.CreateFont(
		14,                        // nHeight
		0,                         // nWidth
		0,                         // nEscapement
		0,                         // nOrientation
		FW_BOLD,                 // 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 ) ; 
	memdc.SetBkMode(TRANSPARENT) ; 
	memdc.SetTextColor(RGB(0,0,0)) ; 
  	memdc.DrawText(strText ,&rc ,DT_CENTER|DT_BOTTOM)  ; 
	
	//绘制
	dc.BitBlt(0,0,rc.Width() ,rc.Height() ,&memdc , 0,0,SRCCOPY)  ; 
 	
	//清理
	memdc.SelectObject(oldBitmap) ; 
	memdc.SelectObject(oldFont)  ; 
	m_fontControl.DeleteObject() ; 
	DeleteObject(oldFont) ;  oldFont =NULL ; 
	bitmap.DeleteObject() ; 
	memdc.DeleteDC() ; 
	ReleaseDC(&memdc) ; 
	DeleteObject(oldBitmap) ; oldBitmap =NULL ; 


  
}

//执行闪烁
void CLed::OnTimer(UINT nIDEvent) 
{
	bRed = !bRed ; 
    RefreshCircle() ; 
  	CStatic::OnTimer(nIDEvent);
}

//刷新图形颜色
void CLed::RefreshCircle()
{
  InvalidateRect(&rcRefresh , FALSE) ; 
} 

//改变状态   
void CLed::SetStatus(int istatus)
{
 	iStatus = istatus ; 
	RefreshCircle()   ; 
}

//设定上限
void CLed::SetUpperValue(float fUpper)
{
	fWarnupper = fUpper ; 	
}
//下限
void CLed::SetLowerValue(float fLower)
{
	fWarnlower = fLower ; 
}
//当前值
void CLed::SetCurValue(float fvalue)
{
	fCurrentvalue = fvalue ; 

}

//计算状态
//预处理:
//先要调用SetCurValue 设定当前值  1红色 2绿色 3闪烁

void CLed::CalStatus()
{
    if (fCurrentvalue > fWarnupper || fCurrentvalue < fWarnlower) 
	{	
		if (iStatus == 2)
		{
  			SetStatus(3) ;  //正常到闪烁
			RefreshCircle() ; //刷新
		}
	}else if (iStatus == 3 || iStatus == 1) //从报警进入正常
	{
		SetStatus(2) ;   
		RefreshCircle() ; //刷新
	}
}

//设定文字
CString CLed::SetText(CString str)
{
  strText = str ; 
  return strText ; 
}
 

int CLed::GetControlStatus()
{
 	return iStatus ; 
}

CString CLed::GetStrText()
{

	return strText ; 
}
//取得当前值
CString CLed::GetCurValue()
{
	CString strTmp ; 
	strTmp.Format(_T("%.0f") , fCurrentvalue) ; 

	return strTmp ; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -