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

📄 drawdlgresp.cpp

📁 一个用于无刷直流电动机的驱动与控制程序.里面所用的采集卡的型号为MC08.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DrawDlgResp.cpp : implementation file
//

#include "stdafx.h"
#include "TestForm.h"
#include "DrawDlgResp.h"

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

#define datasize 128
/////////////////////////////////////////////////////////////////////////////
// DrawDlgResp dialog


DrawDlgResp::DrawDlgResp(CWnd* pParent /*=NULL*/)
	: CDialog(DrawDlgResp::IDD, pParent)
{
	//{{AFX_DATA_INIT(DrawDlgResp)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	////---------------------------------------////
	/*数据数组的初始化*/
	for(int i=0;i<10;i++)
	{
		data_Peak[i]=0;		//最多显示10个最大的峰值,并按从大到小排序
		data_Peak_pos[i]=0;	//10个最大幅值对应的位置
	}	
	dataShow_Y=NULL;
	dataShow_X=NULL;	
	pOldBmp =NULL;	//原有位图
	pBits=NULL;		//保存位图相关
	pBitMap=NULL;	//保存位图相关

	/*布尔变量的初始化*/
	m_bInitflag =TRUE;		//初始化对话框
	m_bGridOn =TRUE;		//画网格
	m_bStartOn =FALSE;		//开始试验
	m_bDrawOn =FALSE;		//开始画实时曲线
	m_bDataReady =FALSE;	//数据获取完成
	m_bSaveBitmap =TRUE;	//保存位图(保存试验实时曲线)标志
	m_bBitmapReady =FALSE;	//位图(保存试验实时曲线)创建成功

	/*整型变量的初始化*/
	m_nDrawType = 1;	//画图类型

	m_GridX=9;		//x向网格数
	m_GridY=19;		//Y向网格数

	m_LineWidth=1;		//画笔粗细

	m_nHitnember=0;	//树型控件点击
	m_Peak_Num=0;	//峰值个数为0
	m_X_Long=0;		//x向数组大小
	m_Y_Long=0;		//y向数组大小

	/*浮点数变量初始化*/
	m_Average=0.0;	//平均值
	m_ds=0.0;		//均方值
	m_dMax_X=0.0;	//X轴最大值
	m_dMin_X=0.0;	//X轴最小值
	m_dMax_Y=0.0;	//y轴最大值
	m_dMin_Y=0.0;	//y轴最小值	
	m_dOverdata=0.0;//超调值
	m_dRisetime=0.0;//上升时间
	m_dStabletime=0.0;//稳定时间

	/*字符串变量的初始化*/
//	Uint_X="时间(s)";		//X坐标轴名称
//	Uint_Y="输出轴转速(RPM)";			//Y坐标轴名称	
//	Title="过度过程试验     转速-时间曲线"; //标题名称
	Uint_X=" ";		//X坐标轴名称
	Uint_Y=" ";		//Y坐标轴名称	
	Title="过度过程试验绘图窗口"; //标题名称
	
	/*颜色变量的初始化*/
	m_FontColor =RGB(0,10,100);		//字体颜色初始化
	//m_LabelColor=RGB(128, 128, 128);
	m_BkColor=RGB(255,255,255);		//绘图区背景颜色初始化
	m_LineColor =RGB(255,0,0);		//实时曲线颜色初始化
	m_LabelColor=RGB(224, 223, 227);//除实时显示区域外的颜色初始化	
	////---------------------------------------////
}


void DrawDlgResp::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DrawDlgResp)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DrawDlgResp, CDialog)
	//{{AFX_MSG_MAP(DrawDlgResp)
	ON_WM_PAINT()
	ON_MESSAGE(WM_MOTDLGRESPSTART_HIT,OnMotrespstarthit)
	ON_MESSAGE(WM_POWERDAQ_FINISH,OnPowerDaqFinish)
	ON_MESSAGE(WM_OPENDATA,OnOpendata)
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_MENU_SAVEBITMAP, OnMenuSavebitmap)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DrawDlgResp message handlers
//////////////////////////////////////
BOOL DrawDlgResp::OnInitDialog() 
{
	CDialog::OnInitDialog();	
	// TODO: Add extra initialization here	
	CreateMyFont();//创建字体

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void DrawDlgResp::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
		////---------------------------------------////	
	GetClientRect(&m_ClientRect);

	if (m_bStartOn)
	{
		if(m_bSaveBitmap)
		{
			CreatBitmap();//创建需要保存的位图
			m_bSaveBitmap =FALSE;
			m_bBitmapReady=TRUE;//位图(保存试验实时曲线)创建成功			
		}		
	}

	switch(m_nDrawType)
	{
	case 1:
		//MessageBox("1");
		if(m_bInitflag)
		InitDlg();
		DrawTitle(&m_pMemory);//画题目
		DrawGrid(&m_pMemory);//画网格
		dc.BitBlt(m_ClientRect.left,m_ClientRect.top,m_ClientRect.Width(),
					m_ClientRect.Height(),&m_pMemory,0,0,SRCCOPY);
		break;
	case 2:
		DrawGrid(&m_pMemory);//画网格
		DrawTitle(&m_pMemory);//画题目
		//MessageBox("1");
		DrawTitleX(&m_pMemory);//画X轴标题		
		DrawTitleY(&m_pMemory);//画Y轴标题

		DrawMessage(&m_pMemory);//画显示信息
		dc.BitBlt(m_ClientRect.left,m_ClientRect.top,m_ClientRect.Width(),
					m_ClientRect.Height(),&m_pMemory,0,0,SRCCOPY);
		break;
	case 3:
		//DrawGrid(&m_pMemory);//画网格
		DrawTitle(&m_pMemory);//画题目
		
		DrawData(&m_pMemory);//画曲线

		DrawMessage(&m_pMemory);//画显示信息
		dc.BitBlt(m_ClientRect.left,m_ClientRect.top,m_ClientRect.Width(),
					m_ClientRect.Height(),&m_pMemory,0,0,SRCCOPY);
		break;
	default:
		break;
	}	
		CDialog::OnPaint();
	////---------------------------------------////	
	// Do not call CDialog::OnPaint() for painting messages
}

//初始化对话框
void DrawDlgResp::InitDlg()
{
	m_bInitflag = FALSE;
	GetClientRect(&m_ClientRect);
	CDC *pDC=GetDC();	
	
	CBitmap Bitmap;
	m_pMemory.CreateCompatibleDC(pDC);
	memBitmap.CreateCompatibleBitmap(pDC,m_ClientRect.Width(),m_ClientRect.Height());
	pOldBmp=m_pMemory.SelectObject(&memBitmap);
	//m_pMemory.FillRect(m_ClientRect,&CBrush(RGB(236, 233, 216)));
	m_pMemory.FillRect(m_ClientRect,&CBrush(RGB(224, 223, 227)));

	//更新屏幕
	//pDC->BitBlt(0,0,m_ClientRect.Width(),m_ClientRect.Height(),&m_pMemory,0,0,SRCCOPY);

	m_pMemory.SelectObject(&pOldBmp);	
	//MessageBox("123");	
	DistributeRect();
	ReleaseDC(pDC);	
}

//创建字体
void DrawDlgResp::CreateMyFont()
{
	LOGFONT Font;
	memset(&Font, 0, sizeof(LOGFONT));
	lstrcpy(Font.lfFaceName, "Times New Roman");
	CWindowDC wdc(NULL) ;
	const int cyPixels = wdc.GetDeviceCaps(LOGPIXELSY);

	//标题用笔,粗体
	Font.lfHeight = -1 * MulDiv(9, cyPixels, 72);
	Font.lfUnderline=false;//不带下划线
	Font.lfWeight = FW_BOLD ;
	Title_Font.CreateFontIndirect(&Font);	

	//X轴标题显示用字体
	Font.lfHeight = -1 * MulDiv(8, cyPixels, 72);
	Font.lfUnderline=false;//不带下划线
	Font.lfWeight = FW_BOLD ;
	Title_Font_X.CreateFontIndirect(&Font);

	//Y轴标题显示用字体
	Font.lfWeight =FW_BOLD;
	Font.lfOrientation = 900; // Rotate font 90 degree for x axis
	Font.lfEscapement = 900;  
	Font.lfUnderline=false;//不带下划线
	Font.lfHeight = -1 * MulDiv(8, cyPixels, 70);
	Title_Font_Y.CreateFontIndirect(&Font);

	//标定文字用笔,不加粗
	Font.lfWeight = FW_THIN ;
	Font.lfOrientation = 0; // Rotate font 90 degree for x axis
	Font.lfEscapement = 0;
	Font.lfUnderline=false;//不带下划线
	Font.lfHeight = -1 * MulDiv(7, cyPixels, 70);
	Label_Font_X.CreateFontIndirect(&Font);

	//显示信息标题用字体,带下划线,加粗
	Font.lfHeight = -1 * MulDiv(8, cyPixels, 72);
	Font.lfWeight = FW_THIN ;
	Font.lfUnderline=true;//带下划线
	Message_Bottom.CreateFontIndirect(&Font);

	//显示信息用字体,不带下划线,不加粗
	Font.lfHeight = -1 * MulDiv(8, cyPixels, 72);
	Font.lfWeight = FW_BOLD ;
	Font.lfUnderline=FALSE;//带下划线
	Message_Font.CreateFontIndirect(&Font);
	
	//竖排显示字体
	//Font.lfWeight = FW_THIN ;
	Font.lfWeight =FW_BOLD;
	Font.lfOrientation = 900; // Rotate font 90 degree for x axis
	Font.lfEscapement = 900;  
	Font.lfUnderline=false;//不带下划线
	Font.lfHeight = -1 * MulDiv(8, cyPixels, 70);
	Label_Font_Y.CreateFontIndirect(&Font);

}

//获得各个区域所对应的矩形
//格式:CRect(left,top,right,bottom)
void DrawDlgResp::DistributeRect()
{
	CRect r;
	GetClientRect(&r);//先得到整个窗口的大小
	int nWidth=r.Width();//得到整个窗口的宽度
	int nHeigh=r.Height();//得到整个窗口的高度
	/*分五个矩形出来,左边是y向标定矩形,中间是绘图矩形,上为标题矩形,
	下为x向标定矩形,右为信息显示矩形*/
	int label_w=nWidth/16;//调整y向标定矩形的宽度
	int title_h=nHeigh/10;//调整标题矩形的高度
	int label_h=nHeigh/10;//调整x向标定矩形的高度
	int message_w=nWidth/7;//调整信息显示矩形的宽度

	m_LabelRect_Y=CRect(r.left,r.top,r.left+label_w,r.bottom-label_h);
	m_LabelRect_X=CRect(r.left,r.bottom-label_h,r.right,r.bottom);
	m_TitleRect  =CRect(r.left+label_w,r.top,r.right-message_w,r.top+title_h);
	m_TextOutRect=CRect(r.right-message_w,r.top,r.right,r.bottom-label_h);
	m_PlotRect   =CRect(r.left+label_w,r.top+title_h,r.right-message_w,r.bottom-label_h);
}

//画网格
void DrawDlgResp::DrawGrid(CDC *pDC)
{
pDC->FillRect(m_PlotRect,&CBrush(RGB(255,255,255)));
	CPen pen2(PS_SOLID,1,RGB(0,0,0));//画网格边界的画笔
	CPen pen1(PS_SOLID,1,RGB(192,192,192));//画网格内部的画笔
	CPen* oldpen=pDC->SelectObject(&pen1);
	int i;
	double GridHeigth=1.0*(m_PlotRect.bottom-m_PlotRect.top)/(m_GridY+1);
	double GridWidth=1.0*(m_PlotRect.right-m_PlotRect.left)/(m_GridX+1);
	if(m_bGridOn)
	{
		for (i=1;i<m_GridY+1;i++)
		{
			pDC->MoveTo(m_PlotRect.left,m_PlotRect.top+int(i*GridHeigth));
			pDC->LineTo(m_PlotRect.right,m_PlotRect.top+int(i*GridHeigth));
		}
		for (i=1;i<m_GridX+1;i++)
		{
			pDC->MoveTo(m_PlotRect.left+int(i*GridWidth),m_PlotRect.top);
			pDC->LineTo(m_PlotRect.left+int(i*GridWidth),m_PlotRect.bottom);
		}
		pDC->SelectObject(&pen2);
		//画边框
		pDC->MoveTo(m_PlotRect.left,m_PlotRect.top);
		pDC->LineTo(m_PlotRect.right-1,m_PlotRect.top);
		pDC->LineTo(m_PlotRect.right-1,m_PlotRect.bottom-1);
		pDC->LineTo(m_PlotRect.left,m_PlotRect.bottom-1);
		pDC->LineTo(m_PlotRect.left,m_PlotRect.top);
	}
	pDC->SelectObject(oldpen);

}

//绘制数据,并得到读数信息
void DrawDlgResp::DrawData(CDC *pDC)
{
if (m_bDataReady)	//数据获取成功
	{
	pDC->FillRect(m_PlotRect,&CBrush(m_BkColor));
	//设置画笔
	CPen LinePen,ReadPen;
	LinePen.CreatePen(PS_SOLID,m_LineWidth,m_LineColor);
	ReadPen.CreatePen(PS_SOLID,m_LineWidth,RGB(0,255,255));
	CPen* oldpen=pDC->SelectObject(&LinePen);

	long ny_T=m_PlotRect.bottom-m_PlotRect.top;
	long nx_T=m_PlotRect.right-m_PlotRect.left;
	/*
	CString str;
	str.Format("%d,%d",ny_T,nx_T);
	MessageBox(str);
	*/
	/*设置绘图诸元*/
	//各轴放大倍数	
	double scale_X;		//X轴放大倍数	
	double scale_Y;		//Y轴放大倍数
	//各轴实际数据间距
	double disdant_X;		//X轴实际数据间距	
	double disdant_Y;		//Y轴实际数据间距
	//Y轴标定最值	
	double m_max_Y;  //标定幅值最大值
	double m_min_Y;  //标定幅值最小值
	
	m_max_Y = 15;
	m_min_Y = -1;

	disdant_Y=fabs(m_max_Y-m_min_Y);//Y轴实际数据间距
	//sdant_Y=fabs(1.5-0);//Y轴实际数据间距
	if(disdant_Y<=0)
		disdant_Y=1;
	scale_Y=ny_T/disdant_Y;		//Y轴放大倍数
//	double m_scale_Y=1.0*Heigh_T/(m_max_Y-m_min_Y);//放缩的倍数
	//X轴标定最值
	double m_max_X=m_dMax_X;  //标定幅值最大值
	double m_min_X=m_dMin_X;  //标定幅值最小值
	//disdant_X=fabs(m_max_X-m_min_X);//X轴实际数据间距
	disdant_X=fabs(21-1);//X轴实际数据间距
	if(disdant_X<=0)
		disdant_X=1;
	scale_X=nx_T/disdant_X;		//X轴放大倍数
// 	double m_scale_X=1.0*Width_T/(m_max_X-m_min_X);//放缩的倍数
	//绘制坐标轴标题及刻度	
	DrawLabelX(&m_pMemory,m_max_X,m_min_X);	
	DrawLabelY(&m_pMemory,m_max_Y,m_min_Y);	
	//DrawLabelY(&m_pMemory,1.5,0);
	DrawGrid(&m_pMemory);//画网格

	//绘制实时曲线
	long x_temp;
	long y_temp;
	long i;
	y_temp=(long) (m_PlotRect.bottom-(dataShow_Y[0]-m_min_Y)*scale_Y);
	x_temp=(long) (m_PlotRect.left+(dataShow_X[0]-m_min_X)*scale_X);	
	//y_temp=m_PlotRect.bottom;
	//x_temp=m_PlotRect.left;
	pDC->MoveTo(x_temp,y_temp);
	for(i=1;i<m_Y_Long;i++)
	{
		y_temp=(long) (m_PlotRect.bottom-(dataShow_Y[i]-m_min_Y)*scale_Y);
		x_temp=(long) (m_PlotRect.left+(dataShow_X[i]-m_min_X)*scale_X);
		pDC->LineTo(x_temp,y_temp);
	}
	pDC->SelectObject(oldpen);
 }
 else
	 MessageBox("没有获取到实时数据!");
}

//画标题区域
void DrawDlgResp::DrawTitle(CDC *pDC)
{
	pDC->FillRect(m_TitleRect,&CBrush(m_LabelColor));
	//pDC->FillRect(m_TitleRect,&CBrush(RGB(255,255,255)));
	
	CFont *OldFont;
	OldFont=pDC->SelectObject(&Title_Font);
	pDC->SetBkMode(TRANSPARENT);//设置为透明
	pDC->SetTextAlign(TA_CENTER);//设置为字体居中
	int a_t=(m_TitleRect.left+m_TitleRect.right)/2;
	int b_t=m_TitleRect.bottom-m_TitleRect.Height()/2-5;
	//MessageBox("1");
	pDC->TextOut(a_t,b_t,Title);
	pDC->SelectObject(OldFont);

}

//显示X坐标轴名称和单位
void DrawDlgResp::DrawTitleX(CDC *pDC)
{
	pDC->FillRect(m_LabelRect_X,&CBrush(m_LabelColor));	
	//设置字体
	CFont *pOldfond=NULL;
	pDC->SetTextAlign(TA_CENTER);//中间靠齐
	pDC->SetBkMode(TRANSPARENT);
	pDC->SelectObject(&Title_Font_X);

	CString str;
	CPoint pp;

	//画坐标轴名称
	pp.x=m_PlotRect.left+m_PlotRect.Width()/2-5;
	pp.y=m_LabelRect_X.bottom-m_LabelRect_X.Height()/2-2;
	str.Format("%s",Uint_X);
	pDC->TextOut(pp.x,pp.y,str);
	pDC->SelectObject(pOldfond);
}

//显示Y坐标轴名称和单位

⌨️ 快捷键说明

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