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

📄 graphisc.cpp

📁 一个很好的虚拟示波器!可一直接运行!他通过串口采集信号与系统单片机上的数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Graphisc.cpp: implementation of the CGraphisc class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Graphisc.h"
#include <math.h>
#include "MainFrm.h"
#include <process.h>
#include <afxmt.h>
#include "Vsbq.h"
#include "BtmView.h"

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

#define Pi 3.1415926
extern CVsbqApp theApp;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//DWORD WINAPI Sendthread(CDC* pDC);
//short card = -1;


UINT EmulatorThreadProc(LPVOID pParam)
{
	return 0; 
}

CGraphisc::CGraphisc()
{	
	memset(pPointArrayRight,0,sizeof(pPointArrayRight));
    memset(pPointArrayLeft,0,sizeof(pPointArrayLeft));
	memset(pPointArrayL,0,sizeof(pPointArrayL));
	memset(pPointArrayR,0,sizeof(pPointArrayR));
	memset(m_DownWave, 0, sizeof(m_DownWave));
	
	m_strReceiveMsg = "";
	m_bLbuttonFlage1 = false;
	m_bLbuttonFlage2 = false;
	m_bLbuttonFlage3 = false;
	m_bLbuttonFlage4 = false;
    LButtonDownFlage = false;
	MouseMoveFlag = false;

	m_pvalData = ((CVsbqApp *)AfxGetApp())->m_valSigData;
	
	//定义画笔用于画示波器的刻度线
	bFontIsCreate = false;
	m_myPen = CreatePen(PS_DASHDOTDOT,1,RGB(55,200,50));
	m_noniusPen = CreatePen(PS_SOLID,1,RGB(100,100,100));
	m_pen.CreatePen(PS_DOT,1,RGB(165,165,165));
	m_pen1.CreatePen(PS_DOT,1,RGB(255,255,255));
	m_penPrint1.CreatePen(PS_DOT,1,RGB(100,100,100));
	m_penPrint2.CreatePen(PS_SOLID,1,RGB(100,100,100));

	//pBrush用于画示波器的底色
	pBrush = new CBrush;
	pOldBrush = new CBrush;
	pBrush->CreateSolidBrush(RGB(40,40,40));

	//次画刷用于画窗体的底色
	pBrush1 = new CBrush;
	pOldBrush1 = new CBrush;
	pBrush1->CreateSolidBrush(GetSysColor(COLOR_BTNFACE));

	pBrush2 = new CBrush;
	pBrush2->CreateSolidBrush(RGB(55,200,50));
	
	pLegendFont = new CFont();

	CreateFont();
}

CGraphisc::~CGraphisc()
{
	if(bFontIsCreate)
	{
		delete pLegendFont;
	}
}


/*************************************************************
///创建一种字体
***************************************************************/
void CGraphisc::CreateFont()
{
	LOGFONT d_lf;

	// 初始化 要创建的字体
	memset(&d_lf, 0, sizeof(LOGFONT));
	lstrcpy(d_lf.lfFaceName, "Times New Roman");

	// 初始化字体大小,获得屏幕DC
	CWindowDC wdc(NULL);
	const int cyPixels = wdc.GetDeviceCaps(LOGPIXELSY);
	d_lf.lfHeight = (-1)*MulDiv(8, cyPixels, 72);

	//创建一种新字体	
	pLegendFont->CreateFontIndirect(&d_lf);
}

/*************************************************************************/
//参数传递,把CoordinateRange的结构题变量传回
/*************************************************************************/
bool CGraphisc::CreateWave(CoordinateRange CdtRange)
{
	this->CdtRange = CdtRange;
	this->StartPoint.x = 0;
	this->StartPoint.y = 0;
	this->EndPoint.x = 0;
	this->EndPoint.y = 0;

	return true;
}

//**************************************************************************/
//一个示波器,PointS为示波器的左上角坐标,PointE为示波器的右下角坐标,
//ClientRect为示波器的结构体,WaveType为示波器波形类型,flage为表的类型
//的字符串,ColFlage判断是否为打印
//***************************************************************************/
void CGraphisc::Show(CDC *pDC,CPoint PointS,CPoint PointE,CRect ClientRect,
		CString flage,BOOL ColFlage)
{
	CFont* oldFont;
	float nLatticeSize = 0;        //示波器每一小格的实际高度
	float nLatticeValue = 0;       //示波器每一小格的映射后的高度
	float nLatticeShowValue = 0;
	CString strLatticeValue = "";
	int XGen = 0;
	int YGen = 0;
	int i = 0;

	this->StartPoint = PointS;
	this->EndPoint = PointE;

	if (LButtonDownFlage == FALSE)
	{
		//得到X坐标上的游标的启示位置
		this->m_nonuispointS1.x = PointS.x - 5;
		this->m_nonuispointS1.y = PointS.y- 20;
		this->m_nonuispointE1.x = PointS.x + 5;
		this->m_nonuispointE1.y = PointS.y - 5;

		this->m_nonuispointS2.x = PointE.x - 5;
		this->m_nonuispointS2.y = PointS.y - 20;
		this->m_nonuispointE2.x = PointE.x + 5;
		this->m_nonuispointE2.y = PointS.y - 5;
		
		//得到Y坐标上的游标的启示位置
		this->m_nonuispointYS1.x = PointE.x;
		this->m_nonuispointYS1.y = PointS.y - 5;
		this->m_nonuispointYE1.x = PointE.x + 12;
		this->m_nonuispointYE1.y = PointS.y + 5;

		this->m_nonuispointYS2.x = PointE.x;
		this->m_nonuispointYS2.y = PointE.y - 5;
		this->m_nonuispointYE2.x = PointE.x + 12;
		this->m_nonuispointYE2.y = PointE.y + 5;
	}
	pDC->SelectObject(&m_pen1);
	if (!ColFlage)
	{	
		pOldBrush = pDC->SelectObject(pBrush);	
		//示波器背景
		pDC->Rectangle(PointS.x-1,PointS.y-1,PointE.x,PointE.y);
		pDC->Rectangle(PointS.x,PointS.y,PointE.x,PointE.y);
		pDC->SelectObject(pOldBrush);
	}
	else
	{		
		//示波器背景
		CBrush* pRectangleBrush = 
			CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
		pDC->SelectObject(pRectangleBrush);
		pDC->Rectangle(PointS.x,PointS.y,PointE.x,PointE.y);
	}
		
	pDC->SelectObject(&m_pen);

	//把自创建的字体选入设备环境
	oldFont = pDC->SelectObject(pLegendFont);
	pDC->SetBkMode(TRANSPARENT);           //设置字体背景色透明
	//	pDC->SetTextColor(RGB(5,160,100));     //设置字体颜色
	pDC->SetTextColor(RGB(230,195,0));     //设置字体颜色
	pDC->TextOut(0,0,"");
	pDC->SelectObject(oldFont);

	//画x轴线
	YGen = (PointE.y - PointS.y)/CdtRange.yLattice;
		
	for (i=1; i < CdtRange.yLattice; i++)
	{
		pDC->MoveTo(PointS.x , PointS.y + i*YGen);
		pDC->LineTo(PointE.x , PointS.y + i*YGen);	
	}

	//画y轴线
	XGen = (PointE.x - PointS.x)/CdtRange.xLattice;
	
	for (i=1; i < CdtRange.xLattice; i++)
	{
		pDC->MoveTo(PointS.x + i*XGen, PointS.y);
		pDC->LineTo(PointS.x + i*XGen, PointE.y);
	}

	if (!ColFlage)
	{
		pDC->SelectObject(&m_pen1);
		pDC->MoveTo(PointS.x + 5*XGen, PointS.y);
		pDC->LineTo(PointS.x + 5*XGen, PointE.y);
		pDC->MoveTo(PointS.x , PointS.y + 4*YGen);
		pDC->LineTo(PointE.x , PointS.y + 4*YGen);
	}
	else
	{
		pDC->SelectObject(&m_penPrint1);
		pDC->MoveTo(PointS.x + 5*XGen, PointS.y);
		pDC->LineTo(PointS.x + 5*XGen, PointE.y);
		pDC->MoveTo(PointS.x , PointS.y + 4*YGen);
		pDC->LineTo(PointE.x , PointS.y + 4*YGen);
		
		pDC->SelectObject(&m_penPrint2);
		pDC->MoveTo(PointS.x, PointS.y);
		pDC->LineTo(PointS.x, PointE.y);
		pDC->LineTo(PointE.x , PointE.y);
		pDC->LineTo(PointE.x , PointS.y);
		pDC->LineTo(PointS.x , PointS.y);
	}
	
		
	//写x轴线上的刻度数据
//	for (i=0; i < CdtRange.yLattice; i++)
	{
/*		nLatticeShowValue = (1) * nLatticeValue;
		strLatticeValue = "";
		strLatticeValue.Format("%3.0f",nLatticeShowValue);
		strLatticeValue = strLatticeValue.Left(5) + "伏/格";
//		pDC->TextOut( PointS.x, PointE.y - (0)*YGen - 10, theApp.m_strVolete1);*/
	}

	pDC->SetTextColor(RGB(200,5,5));     //设置字体颜色

		if (theApp.m_bnoniusx==TRUE)
		{
			// 画X轴上游标
			CPoint pts1[6];	
			CPoint pts2[6];
			pOldBrush = pDC->SelectObject(pBrush2);
			pDC->SelectObject(m_noniusPen);
			
			pts1[0].x = m_nonuispointS1.x;
			pts1[0].y = this->StartPoint.y ;
			pts1[1].x = m_nonuispointS1.x;
			pts1[1].y = this->StartPoint.y ;
			pts1[2].x = m_nonuispointE1.x;
			pts1[2].y = this->StartPoint.y ;
			pts1[3].x = m_nonuispointE1.x;
			pts1[3].y = this->StartPoint.y ;
			pts1[4].x = m_nonuispointE1.x -5;
			pts1[4].y = this->StartPoint.y + 8;
			pts1[5] = pts1[0];
			pDC->Polygon(pts1, 5);
			pDC->Polyline(pts1, 6);
			
			pts2[0].x = m_nonuispointS2.x-2;
			pts2[0].y = this->StartPoint.y;
			pts2[1].x = m_nonuispointS2.x-2;
			pts2[1].y = this->StartPoint.y ;
			pts2[2].x = m_nonuispointE2.x-2;
			pts2[2].y = this->StartPoint.y ;
			pts2[3].x = m_nonuispointE2.x-2;
			pts2[3].y = this->StartPoint.y ;
			pts2[4].x = m_nonuispointE2.x -7;
			pts2[4].y = this->StartPoint.y +8;
			pts2[5] = pts2[0];
			pDC->Polygon(pts2, 5);
			pDC->Polyline(pts2, 6);
				
			pDC->SelectObject(m_myPen);
			pDC->MoveTo(pts1[4]);
			pDC->LineTo(pts1[4].x, this->EndPoint.y);
			
			// 获得当前游标所在的位置
			CString str = "";
			float m_number1 = 0;
			float m_number2 = 0;
			m_number1 = 10.0 * (float)(m_nonuispointS1.x - StartPoint.x +5)
				/ (float)(EndPoint.x - StartPoint.x);
			str.Format("%.2f", m_number1);
			pDC->TextOut(m_nonuispointE1.x-5, this->StartPoint.y,str);

			m_number2 = 10.0 * (float)(m_nonuispointS2.x - StartPoint.x +5)
				/ (float)(EndPoint.x - StartPoint.x);
			str.Format("%.2f", m_number2);
			pDC->MoveTo(pts2[4]);
			pDC->LineTo(pts2[4].x, this->EndPoint.y);
			pDC->TextOut(m_nonuispointE2.x-5, this->StartPoint.y,str);

			// 计算时间
/*			m_number2 = fabs(m_number2 - m_number1);
			str.Format("%.2f",m_number2);
			pDC->TextOut(this->StartPoint.x + 240, this->EndPoint.y + 15,"△t="+str+"格" );
	*/	}
		if (theApp.m_bnoniusy==TRUE)
		{
			// 画Y轴上游标
			CPoint pts1[6];	
			CPoint pts2[6];
			pOldBrush = pDC->SelectObject(pBrush2);
			pDC->SelectObject(m_noniusPen);
			
			pts1[0].x = this->EndPoint.x-11;
			pts1[0].y = m_nonuispointYS1.y+5;
			pts1[1].x = this->EndPoint.x -1;
			pts1[1].y = m_nonuispointYS1.y;
			pts1[2].x = this->EndPoint.x -1;
			pts1[2].y = m_nonuispointYS1.y;
			pts1[3].x = this->EndPoint.x -1;
			pts1[3].y = m_nonuispointYE1.y;
			pts1[4].x = this->EndPoint.x -1;
			pts1[4].y = m_nonuispointYE1.y;
			pts1[5] = pts1[0];
			pDC->Polygon(pts1, 5);
			pDC->Polygon(pts1, 6);
			
			pts2[0].x = this->EndPoint.x-11;
			pts2[0].y = m_nonuispointYS2.y + 3;
			pts2[1].x = this->EndPoint.x -1;
			pts2[1].y = m_nonuispointYS2.y-2;
			pts2[2].x = this->EndPoint.x -1;
			pts2[2].y = m_nonuispointYS2.y-2;
			pts2[3].x = this->EndPoint.x -1;
			pts2[3].y = m_nonuispointYE2.y-2;
			pts2[4].x = this->EndPoint.x -1;
			pts2[4].y = m_nonuispointYE2.y-2;
			pts2[5] = pts2[0];
			pDC->Polygon(pts2, 5);
			pDC->Polygon(pts2, 6);
			
			pDC->SelectObject(m_myPen);
			pDC->MoveTo(pts1[0]);
			pDC->LineTo(this->StartPoint.x, pts1[0].y);
			
			CString str = "";
			float m_number1 = 0;
			float m_number2 = 0;
			m_number1 = (float)(EndPoint.y - m_nonuispointYS1.y - 5)/(float)(EndPoint.y - StartPoint.y);
			m_number1 = 8*m_number1 - 4;
			str.Format("%1.1f", m_number1);
			pDC->TextOut(this->EndPoint.x, m_nonuispointYS1.y - 10,str);
		
			m_number2 = (float)(EndPoint.y - m_nonuispointYS2.y - 5)/(float)(EndPoint.y - StartPoint.y);
			m_number2 = 8*m_number2 - 4;
			str.Format("%1.1f", m_number2);
			pDC->MoveTo(pts2[0]);
			pDC->LineTo(this->StartPoint.x, pts2[0].y);
			pDC->TextOut(this->EndPoint.x, m_nonuispointYS2.y - 10,str);

			// 计算△Y,
/*			m_number2 = fabs(m_number2 - m_number1);
			str.Format("%1.1f",m_number2);
			pDC->TextOut(this->EndPoint.x - 330, this->EndPoint.y + 15,"△r="+str+"格" );*/		
		}

	//写表的名称
	pDC->SetTextColor(RGB(230,195,0));     //设置字体颜色
	if (!ColFlage)
	{
		pDC->TextOut(PointE.x-215, PointE.y-16, flage);
	}
	else
	{		
		pDC->TextOut(PointE.x-265, PointE.y-16, flage);
	//	pDC->SetTextColor(RGB(5,160,100));     //设置字体颜色
		pDC->SetTextColor(RGB(20,5,0));     //设置字体颜色
		pDC->TextOut(PointS.x+285, PointS.y-16, "波 形 文 件");
		pDC->SetTextColor(RGB(230,195,0));     //设置字体颜色
	}
	

	//标志,使其在析构的时候销毁自创建的字体
	bFontIsCreate = true;  
}


/**************************************************************
//加载光标函数
***************************************************************/
void CGraphisc::LoadCur(UINT CurName)
{
	m_hCursor=AfxGetApp()->LoadCursor(CurName);
}


/**************************************************************
//判断当前点是否在示波器内
***************************************************************/
int CGraphisc::IsINWave(CPoint cpoint)
{
	if (cpoint.x>this->m_nonuispointS1.x && cpoint.x<this->m_nonuispointE1.x
		&& cpoint.y>this->StartPoint.y - 20 && cpoint.y<this->StartPoint.y + 5)
		
	{
		return 1;
	}
	if(cpoint.x>this->m_nonuispointS2.x && cpoint.x<this->m_nonuispointE2.x
		&& cpoint.y>this->StartPoint.y - 20 && cpoint.y<this->StartPoint.y + 5)
	{
		return 2;
	}
	if (cpoint.x>this->EndPoint.x - 8 && cpoint.x<this->EndPoint.x+12
		&& cpoint.y>this->m_nonuispointYS1.y && cpoint.y<this->m_nonuispointYE1.y + 5)
	{
		return 3;
	}
	if (cpoint.x>this->EndPoint.x - 8 && cpoint.x<this->EndPoint.x+12
		&& cpoint.y>this->m_nonuispointYS2.y && cpoint.y<this->m_nonuispointYE2.y + 5)
	{
		return 4;
	}
	else	
	{
		return 0;
	}
}


/**************************************************************
//鼠标左键按下
***************************************************************/
void CGraphisc::LButtonDown(CPoint cPoint, int flage)
{	
	this->m_oldMousePoint = this->LButtonDownPoint = cPoint;
	
	//设置鼠标左键是否按下
	this->LButtonDownFlage = true;
	if (flage == 1)
	{
		this->m_bLbuttonFlage1 = true;
	}
	else if(flage == 2)
	{
		this->m_bLbuttonFlage2 = true;
	}
	else if (flage == 3)
	{
		this->m_bLbuttonFlage3 = true;
	}
	else if (flage == 4)
	{
		this->m_bLbuttonFlage4 = true;
	}
	
}


/**************************************************************
//鼠标左键弹起
***************************************************************/
void CGraphisc::OnLButtonUp(UINT nFlags, CPoint point)
{
	CPoint m_myPoint;
	m_myPoint = point;
	if (m_myPoint.x>EndPoint.x)
	{
		m_myPoint.x = EndPoint.x;
	}
	if (m_myPoint.x<StartPoint.x)
	{
		m_myPoint.x = StartPoint.x;
	}
	if (m_myPoint.y >EndPoint.y)
	{
		m_myPoint.y = EndPoint.y;
	}
	if (m_myPoint.y<StartPoint.y)
	{
		m_myPoint.y = StartPoint.y;
	}
	if (m_bLbuttonFlage1)
	{	
		this->m_nonuispointS1.x = m_myPoint.x - 5;
		this->m_nonuispointE1.x = m_myPoint.x + 5;	
	}	
	if(m_bLbuttonFlage2)
	{
		this->m_nonuispointS2.x = m_myPoint.x - 5;
		this->m_nonuispointE2.x = m_myPoint.x + 5;
	}
	if (m_bLbuttonFlage3)
	{
		this->m_nonuispointYS1.y = m_myPoint.y -5;
		this->m_nonuispointYE1.y = m_myPoint.y +5;
	}
	if (m_bLbuttonFlage4)
	{
		this->m_nonuispointYS2.y = m_myPoint.y -5;
		this->m_nonuispointYE2.y = m_myPoint.y +5;
	}
	
	this->m_bLbuttonFlage1 = false;
	this->m_bLbuttonFlage2 = false;
	this->m_bLbuttonFlage3 = false;
	this->m_bLbuttonFlage4 = false;
}

 /**************************************************************
//鼠标移动
***************************************************************/
void CGraphisc::OnMouseMove(UINT nFlags, CPoint point)
{
	CPoint m_myPoint;
	m_myPoint = point;
	if (m_myPoint.x>EndPoint.x)
	{
		m_myPoint.x = EndPoint.x;
	}
	if (m_myPoint.x<StartPoint.x)
	{
		m_myPoint.x = StartPoint.x;
	}
	if (m_myPoint.y >EndPoint.y)
	{
		m_myPoint.y = EndPoint.y;
	}
	if (m_myPoint.y<StartPoint.y)
	{
		m_myPoint.y = StartPoint.y;
	}
	if (m_bLbuttonFlage1)
	{	
		this->m_nonuispointS1.x = m_myPoint.x - 5;
		this->m_nonuispointE1.x = m_myPoint.x + 5;
		LoadCur(IDC_CURSOR2);       

⌨️ 快捷键说明

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