C#绘制动态曲线.doc_第1页
C#绘制动态曲线.doc_第2页
C#绘制动态曲线.doc_第3页
C#绘制动态曲线.doc_第4页
C#绘制动态曲线.doc_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Windows.Forms;namespacecurve/实时动态曲线图/代码编写:方志洪/编写时间:2007年2月7日/E-Mail:zhihongf#(请将#改为)/BLOG:/publicclassCurveControl:System.Windows.Forms.UserControlprivateSystem.Windows.Forms.PictureBoxpicCurveShow;privateSystem.Windows.Forms.LabellabShowView;/必需的设计器变量。/privateSystem.ComponentModel.Containercomponents=null;publicCurveControl()/该调用是Windows.Forms窗体设计器所必需的。InitializeComponent();/TODO:在InitializeComponent调用后添加任何初始化/清理所有正在使用的资源。/protectedoverridevoidDispose(booldisposing)if(disposing)if(components!=null)components.Dispose();base.Dispose(disposing);#region组件设计器生成的代码/设计器支持所需的方法-不要使用代码编辑器/修改此方法的内容。/privatevoidInitializeComponent()this.picCurveShow=newSystem.Windows.Forms.PictureBox();this.labShowView=newSystem.Windows.Forms.Label();this.SuspendLayout();/picCurveShow/this.picCurveShow.BackColor=System.Drawing.Color.Black;this.picCurveShow.Dock=System.Windows.Forms.DockStyle.Fill;this.picCurveShow.Location=newSystem.Drawing.Point(0,0);this.picCurveShow.Name=picCurveShow;this.picCurveShow.Size=newSystem.Drawing.Size(384,264);this.picCurveShow.TabIndex=0;this.picCurveShow.TabStop=false;this.picCurveShow.Resize+=newSystem.EventHandler(this.picCurveShow_Resize);this.picCurveShow.DoubleClick+=newSystem.EventHandler(this.picCurveShow_DoubleClick);this.picCurveShow.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.picCurveShow_MouseMove);this.picCurveShow.MouseLeave+=newSystem.EventHandler(this.picCurveShow_MouseLeave);/labShowView/this.labShowView.AutoSize=true;this.labShowView.BackColor=System.Drawing.Color.Black;this.labShowView.ForeColor=System.Drawing.Color.Red;this.labShowView.Location=newSystem.Drawing.Point(192,96);this.labShowView.Name=labShowView;this.labShowView.Size=newSystem.Drawing.Size(42,17);this.labShowView.TabIndex=1;this.labShowView.Text=label1;this.labShowView.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;this.labShowView.Visible=false;/CurveControl/this.Controls.Add(this.labShowView);this.Controls.Add(this.picCurveShow);this.Name=CurveControl;this.Size=newSystem.Drawing.Size(384,264);this.Load+=newSystem.EventHandler(this.CurveControl_Load);this.ResumeLayout(false);#endregion#region曲线数据定义/定义曲线窗体标题/privatestringtitle=方志洪曲线代码测试DEMO;/定义曲线标题颜色/privateColortitleColor=Color.Yellow;/定义是否显示系统时间/privateboolshowTime=true;/定义显示系统时间颜色/privateColorshowTimeColor=Color.Red;/定义坐标零点向右偏移量/privatefloatcoordinate=50F;/定义曲线Y轴最大值/privatefloatyMaxValue=100F;/定义曲线Y轴最大值提示文字/privatestringyMaxString=最大值:;/定义曲线Y轴最小值/privatefloatyMinValue=0F;/定义曲线Y轴最小值提示文字/privatestringyMinString=最小值:;/定义曲线Y轴正常值(上限)/privatefloatyUpper=70F;/定义曲线Y轴正常值(上限)提示文本/privatestringyUpperString=上限:;/定义曲线Y轴正常值(下限)/privatefloatyLower=20F;/定义曲线Y轴正常值(下限)提示文本/privatestringyLowerString=下限:;/定义曲线正常值上下限线段颜色/privateColoryUpperAndLowerColor=Color.Lime;/定义曲线正常值上下限线段宽度/privatefloatyUpperAndLowerPenWidth=1F;/定义背景颜色/privateColorbackGroundColor=Color.Black;/定义是否滚动网格线/privateboolremoveGrid=true;/定义背景网格线颜色/privateColorgridColor=Color.DarkGreen;/定义背景网格文字颜色/privateColorgridForeColor=Color.Yellow;/定义背景网格(分隔线)宽度/privatefloatgridCompart=1F;/定义背景网格文字大小/privatefloatgridFontSize=9;/定义背景网格线画笔宽度/privatefloatgridPenWidth=1F;/定义背景网格线单元格宽度/privatefloatgridWidth=10F;/定义背景网格线单元格高度/privatefloatgridHeight=30F;/定义曲线颜色/privateColorcurveColor=Color.White;/定义曲线画笔宽度/privatefloatcurvePenWidth=1;/定义曲线移动距离/privateintcurveRemove=1;/定义数值节点正方形宽度/privatefloatrectangleWidth=1F;/定义正方形颜色/privateColorrectangleColor=Color.White;/定义显示节点数值鼠标X,Y轴容差精度/privatefloatxYPrecision=4F;/曲线节点数据最大存储量/privateintmaxNote=1000;#endregion#region公共属性/定义曲线窗体标题/publicstringCarveTitlegetreturnthis.title;setthis.title=value;/定义标题颜色/publicColorCarveTitleColorgetreturnthis.titleColor;setthis.titleColor=value;/定义是否显示系统时间/publicboolCarveShowTimegetreturnthis.showTime;setthis.showTime=value;/定义显示系统时间颜色/publicColorCarveShowTimeColorgetreturnthis.showTimeColor;setthis.showTimeColor=value;/定义坐标零点向右偏移量/publicfloatCarveCoordinategetreturnthis.coordinate;setthis.coordinate=value;/定义曲线Y轴最大值/publicfloatCarveYMaxValuegetreturnthis.yMaxValue;setthis.yMaxValue=value;/定义曲线Y轴最大值提示文字/publicstringCarveYMaxStringgetreturnthis.yMaxString;setthis.yMaxString=value;/定义曲线Y轴最小值/publicfloatCarveYMinValuegetreturnthis.yMinValue;setthis.yMinValue=value;/定义曲线Y轴最小值提示文字/publicstringCarveYMinStringgetreturnthis.yMinString;setthis.yMinString=value;/定义曲线Y轴正常值(上限)/publicfloatCarveYUppergetreturnthis.yUpper;setthis.yUpper=value;/定义曲线Y轴正常值(上限)提示文本/publicstringCarveYUpperStringgetreturnthis.yUpperString;setthis.yUpperString=value;/定义曲线Y轴正常值(下限)/publicfloatCarveYLowergetreturnthis.yLower;setthis.yLower=value;/定义曲线Y轴正常值(下限)提示文本/publicstringCarveYLowerStringgetreturnthis.yLowerString;setthis.yLowerString=value;/定义曲线正常值上下限线段颜色/publicColorCarveYUpperAndLowerColorgetreturnthis.yUpperAndLowerColor;setthis.yUpperAndLowerColor=value;/定义曲线正常值上下限线段宽度/publicfloatCarveYUpperAndLowerPenWidthgetreturnthis.yUpperAndLowerPenWidth;setthis.yUpperAndLowerPenWidth=value;/定义背景颜色/publicColorCarveBackGroundColorgetreturnthis.backGroundColor;setthis.backGroundColor=value;/定义是否滚动网格线/publicboolCarveRemoveGridgetreturnthis.removeGrid;setthis.removeGrid=value;/定义背景网格线颜色/publicColorCarveGridColorgetreturnthis.gridColor;setthis.gridColor=value;/定义背景网格文字颜色/publicColorCarveGridForeColorgetreturnthis.gridForeColor;setthis.gridForeColor=value;/定义背景网格(分隔线)宽度/publicfloatCarveGridCompartgetreturnthis.gridCompart;setthis.gridCompart=value;/定义背景网格文字大小/publicfloatCarveGridFontSizegetreturnthis.gridFontSize;setthis.gridFontSize=value;/定义背景网格线画笔宽度/publicfloatCarveGridPenWidthgetreturnthis.gridPenWidth;setthis.gridPenWidth=value;/定义背景网格线单元格宽度/publicfloatCarveGridWidthgetreturnthis.gridWidth;setthis.gridWidth=value;/定义背景网格线单元格高度/publicfloatCarveGridHeightgetreturnthis.gridHeight;setthis.gridHeight=value;/定义曲线颜色/publicColorCarveCurveColorgetreturnthis.curveColor;setthis.curveColor=value;/定义曲线画笔宽度/publicfloatCarveCurvePenWidthgetreturnthis.curvePenWidth;setthis.curvePenWidth=value;/定义曲线移动距离/publicintCarveCurveRemovegetreturnthis.curveRemove;setthis.curveRemove=value;/定义数值节点正方形宽度/publicfloatCarveRectangleWidthgetreturnthis.rectangleWidth;setthis.rectangleWidth=value;/定义正方形颜色/publicColorCarveRectangleColorgetreturnthis.rectangleColor;setthis.rectangleColor=value;/定义显示节点数值鼠标X,Y轴容差精度/publicfloatCarveXYPrecisiongetreturnthis.xYPrecision;setthis.xYPrecision=value;/曲线节点数据最大存储量/publicintCarveMaxNotegetreturnthis.maxNote;setthis.maxNote=value;#endregion#region全局变量/背景方格移动量/privatefloatgridRemoveX=1;/鼠标X,Y坐标值,及该点坐标记录值、记录时间(数组)/privateCoordinatesValuenoteMessages;/当前鼠标X,Y坐标记录数组下标值/privateintnoteNow=0;/系统上次读取的值/privatefloatlastTimeValue=0F;/系统窗体高度临时值,用于窗体变形时刷新数组坐标。/privateintlastTimeSystemWindowHeight=0;/系统窗体宽度临时值,用于窗体变形时刷新数组坐标。/privateintlastTimeSystemWindowWidth=0;#endregion#region曲线数据显示#region绘制背景网格/刷新背景网格线,并返回背景图片(背景不判断是否滚动)/返回背景图片privateSystem.Drawing.BitmapRefBackground()if(this.picCurveShow.Height1|this.picCurveShow.Width=0;i=i-this.gridHeight)System.Drawing.PointFpointFBegin=newPointF(0F,i);System.Drawing.PointFpointFEnd=newPointF(this.picCurveShow.Width,i);backGroundImage.DrawLine(gridPen,pointFBegin,pointFEnd);/绘制背景纵轴线for(floati=this.picCurveShow.Width;i=0;i=i-this.gridWidth)System.Drawing.PointFpointFBegin;System.Drawing.PointFpointFEnd;if(i-gridRemoveX=0)pointFBegin=newPointF(i-gridRemoveX,0F);pointFEnd=newPointF(i-gridRemoveX,this.picCurveShow.Height);backGroundImage.DrawLine(gridPen,pointFBegin,pointFEnd);/绘制分隔线。backGroundImage.DrawLine(gridCompartPen,0,0,0,this.picCurveShow.Height);/绘制正常值上限,下限/绘制分隔线刻度文字System.Drawing.FontbackGroundFont=newFont(Arial,this.gridFontSize);floatfontHight=backGroundFont.GetHeight();System.Drawing.SolidBrushbrush=newSolidBrush(this.gridForeColor);/判断上下线值是否有效(非零)System.Drawing.PenupperAndLowerPen=newPen(this.yUpperAndLowerColor,this.yUpperAndLowerPenWidth);upperAndLowerPen.DashStyle=System.Drawing.Drawing2D.DashStyle.Dash;if(this.yLower0)floatyHeight=(float)this.picCurveShow.Height-(this.yLower/(this.yMaxValue-this.yMinValue)*(float)this.picCurveShow.Height;backGroundImage.DrawLine(upperAndLowerPen,0,yHeight,this.picCurveShow.Width,yHeight);/绘制下限文字backGroundImage.DrawString(this.yLowerString,backGroundFont,brush,-this.coordinate,yHeight-fontHight/2);if(this.yUpper0)floatyHeight=(float)this.picCurveShow.Height-(this.yUpper/(this.yMaxValue-this.yMinValue)*(float)this.picCurveShow.Height;backGroundImage.DrawLine(upperAndLowerPen,0,yHeight,this.picCurveShow.Width,yHeight);/绘制下限文字backGroundImage.DrawString(this.yUpperString,backGroundFont,brush,-this.coordinate,yHeight-fontHight/2);/绘制最大值文字backGroundImage.DrawString(this.yMaxString,backGroundFont,brush,-this.coordinate,0);/绘制最小值文字backGroundImage.DrawString(this.yMinString,backGroundFont,brush,-this.coordinate,(float)this.picCurveShow.Height-fontHight);/绘制曲线窗体标题brush=newSolidBrush(this.titleColor);backGroundImage.DrawString(this.title,backGroundFont,brush,(this.picCurveShow.Width/2-this.title.Length*this.gridFontSize),0);/绘制系统时间if(this.showTime)brush=newSolidBrush(this.showTimeColor);backGroundImage.DrawString(System.DateTime.Now.ToString(T),backGroundFont,brush,(this.picCurveShow.Width-105),this.picCurveShow.Height-fontHight);returnbitmap;/刷新背景网格线,并返回背景图片(背景判断是否滚动)/返回背景图片privateSystem.Drawing.BitmapRefAndRemoveBackground()/判断是否需要移动纵轴线if(this.removeGrid)if(this.gridRemoveX=this.gridWidth)this.gridRemoveX=1;elsethis.gridRemoveX=this.gridRemoveX+curveRemove;returnthis.RefBackground();#endregion/刷新背景网格线,显示曲线/publicvoidShowCurve()/窗体高度发生变化,先刷新数组Y坐标值if(this.picCurveShow.Height!=this.lastTimeSystemWindowHeight)|(this.picCurveShow.Width!=this.lastTimeSystemWindowWidth)this.RefurbishArray();System.Drawing.Bitmapbitmap=this.RefAndRemoveBackground();System.Drawing.Graphicsgraphics=System.Drawing.Graphics.FromImage(bitmap);/绘制曲线/判断数组中是否有两个以上的数值/绘制直线if(this.noteNow1)/声明绘制曲线的Point数组System.Drawing.PointpointsTemp=newPointthis.noteNow;System.Drawing.Pointpoints;/数组下标;intpointI=0;for(inti=0;i=this.coordinate)pointsTemppointI.X=this.noteMessagesi.X;pointsTemppointI.Y=this.noteMessagesi.Y;pointI+;points=newPointpointI;Array.Copy(pointsTemp,0,points,0,pointI);graphics.DrawLines(newPen(this.curveColor,this.curvePenWidth),points);/绘制曲线/if(this.noteNow=2)/graphics.DrawCurve(newPen(this.curveColor,this.curvePenWidth),points);/this.picCurveShow.Image=bitmap;/刷新背景网格线,显示曲线,自动添加即时数值/即时数值publicvoidShowCurve(floatValue)this.AddNewValue(Value);this.ShowCurve();#endregion#region自动将最新采样数值添加到数组/自动将最新采样数值添加到数组/最新采样数值privatevoidAddNewValue(floatnewValue)/先判断数组下标if(this.noteNow=this.maxNote-1)/数组已经存满数值for(inti=0;ithis.noteNow;i+)th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论