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

下载本文档

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

文档简介

1、usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Windows.Forms;namespacecurve/<summary>/实时动态曲线图/代码编写:方志洪/编写时间:2007年2月7日/E-Mail:/BLOG:http:/</summary>publicclassCurveControl:System.Windows.Forms.UserControlprivateSystem.Window

2、s.Forms.PictureBoxpicCurveShow;privateSystem.Windows.Forms.LabellabShowView;/<summary>/必需的设计器变量。/</summary>InitializeComponent();/TODO:在InitializeComponent调用后添加任何初始化/<summary>/清理所有正在使用的资源。/</summary>base.Dispose(disposing);#regiorffi件设计器生成的代码/<summary>/设计器支持所需的方法-不要使用代码

3、编辑器/修改此方法的内容。/</summary>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.

4、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

5、_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.labSh

6、owView.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.T

7、abIndex=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.Drawin

8、g.Size(384,264);this.Load+=newSystem.EventHandler(this.CurveControl_Load);this.ResumeLayout(false);#endregion#region曲线数据定义/<summary>/定义曲线窗体标题/</summary>privatestringtitle="方志洪曲线代码测试DEMO"/<summary>/定义曲线标题颜色/</summary>privateColortitleColor=Color.Yellow;/<summary&g

9、t;/</summary>privateboolshowTime=true;/<summary>/定义显示系统时间颜色/</summary>privateColorshowTimeColor=Color.Red;/<summary>/定义坐标零点向右偏移量/</summary>privatefloatcoordinate=50F;/<summary>/定义曲线Y轴最大值/</summary>privatefloatyMaxValue=100F;/<summary>/定义曲线Y轴最大值提示文字/<

10、;/summary>privatestringyMaxString="最大值:;/<summary>/定义曲线Y轴最小值/</summary>privatefloatyMinValue=0F;/<summary>/定义曲线Y轴最小值提示文字/</summary>privatestringyMinString="最小值:;/<summary>/定义曲线Y轴正常值(上限)/</summary>privatefloatyUpper=70F;/<summary>/定义曲线Y轴正常值(上限)提示

11、文本/</summary>privatestringyUpperString="上限:;/<summary>/定义曲线Y轴正常值(下限)/</summary>privatefloatyLower=20F;/<summary>/定义曲线Y轴正常值(下限)提示文本/</summary>privatestringyLowerString="下限:;/<summary>/定义曲线正常值上下限线段颜色/</summary>privateColoryUpperAndLowerColor=Color.Li

12、me;/<summary>/定义曲线正常值上下限线段宽度/</summary>privatefloatyUpperAndLowerPenWidth=1F;/<summary>/定义背景颜色/</summary>privateColorbackGroundColor=Color.Black;/<summary>/定义是否滚动网格线/</summary>privateboolremoveGrid=true;/<summary>/定义背景网格线颜色/</summary>privateColorgridCo

13、lor=Color.DarkGreen;/<summary>/定义背景网格文字颜色/</summary>privateColorgridForeColor=Color.Yellow;/<summary>/定义背景网格(分隔线)宽度/</summary>privatefloatgridCompart=1F;/<summary>/定义背景网格文字大小/</summary>privatefloatgridFontSize=9;/<summary>/定义背景网格线画笔宽度/</summary>private

14、floatgridPenWidth=1F;/<summary>/定义背景网格线单元格宽度/</summary>privatefloatgridWidth=10F;/<summary>/定义背景网格线单元格高度/</summary>privatefloatgridHeight=30F;/<summary>/定义曲线颜色/</summary>privateColorcurveColor=Color.White;/<summary>/定义曲线画笔宽度/</summary>privatefloatcurve

15、PenWidth=1;/<summary>/定义曲线移动距离/</summary>privateintcurveRemove=1;/<summary>/</summary>privatefloatrectangleWidth=1F;/<summary>/定义正方形颜色/</summary>privateColorrectangleColor=Color.White;/<summary>/定义显示节点数值鼠标X,Y轴容差精度/</summary>privatefloatxYPrecision=4F;/

16、<summary>/曲线节点数据最大存储量/</summary>privateintmaxNote=1000;#endregion#region公共属性/<summary>/定义曲线窗体标题/</summary>publicstringCarveTitlegetreturnthis.title;setthis.title=value;/<summary>/定义标题颜色/</summary>publicColorCarveTitleColorgetreturnthis.titleColor;setthis.titleColo

17、r=value;/<summary>/定义是否显示系统时间/</summary>publicboolCarveShowTimegetreturnthis.showTime;setthis.showTime=value;/<summary>/定义显示系统时间颜色/</summary>publicColorCarveShowTimeColorgetreturnthis.showTimeColor;setthis.showTimeColor=value;/<summary>/定义坐标零点向右偏移量/</summary>publi

18、cfloatCarveCoordinategetreturnthis.coordinate;setthis.coordinate=value;/<summary>/定义曲线Y轴最大值/</summary>publicfloatCarveYMaxValuegetreturnthis.yMaxValue;setthis.yMaxValue=value;/<summary>/定义曲线Y轴最大值提示文字/</summary>publicstringCarveYMaxStringgetreturnthis.yMaxString;setthis.yMaxSt

19、ring=value;/<summary>/定义曲线Y轴最小值/</summary>publicfloatCarveYMinValuegetreturnthis.yMinValue;setthis.yMinValue=value;/<summary>/定义曲线Y轴最小值提示文字/</summary>publicstringCarveYMinStringgetreturnthis.yMinString;setthis.yMinString=value;/<summary>/定义曲线Y轴正常值(上限)/</summary>pu

20、blicfloatCarveYUppergetreturnthis.yUpper;setthis.yUpper=value;/<summary>/定义曲线Y轴正常值(上限)提示文本/</summary>publicstringCarveYUpperStringgetreturnthis.yUpperString;setthis.yUpperString=value;/<summary>/定义曲线Y轴正常值(下限)/</summary>publicfloatCarveYLowergetreturnthis.yLower;setthis.yLower

21、=value;/<summary>/定义曲线Y轴正常值(下限)提示文本/</summary>publicstringCarveYLowerStringgetreturnthis.yLowerString;setthis.yLowerString=value;/<summary>/定义曲线正常值上下限线段颜色/</summary>publicColorCarveYUpperAndLowerColorgetreturnthis.yUpperAndLowerColor;setthis.yUpperAndLowerColor=value;/<sum

22、mary>/定义曲线正常值上下限线段宽度/</summary>publicfloatCarveYUpperAndLowerPenWidthgetreturnthis.yUpperAndLowerPenWidth;setthis.yUpperAndLowerPenWidth=value;/<summary>/定义背景颜色/</summary>publicColorCarveBackGroundColorgetreturnthis.backGroundColor;setthis.backGroundColor=value;/<summary>/

23、定义是否滚动网格线/</summary>publicboolCarveRemoveGridgetreturnthis.removeGrid;setthis.removeGrid=value;/<summary>/定义背景网格线颜色/</summary>publicColorCarveGridColorgetreturnthis.gridColor;setthis.gridColor=value;/<summary>/定义背景网格文字颜色/</summary>publicColorCarveGridForeColorgetreturnt

24、his.gridForeColor;setthis.gridForeColor=value;/<summary>/定义背景网格(分隔线)宽度/</summary>publicfloatCarveGridCompartgetreturnthis.gridCompart;setthis.gridCompart=value;/<summary>/定义背景网格文字大小/</summary>publicfloatCarveGridFontSizegetreturnthis.gridFontSize;setthis.gridFontSize=value;/&

25、lt;summary>/定义背景网格线画笔宽度/</summary>publicfloatCarveGridPenWidthgetreturnthis.gridPenWidth;setthis.gridPenWidth=value;/<summary>/定义背景网格线单元格宽度/</summary>publicfloatCarveGridWidthgetreturnthis.gridWidth;setthis.gridWidth=value;/<summary>/定义背景网格线单元格高度/</summary>publicfloa

26、tCarveGridHeightgetreturnthis.gridHeight;setthis.gridHeight=value;/<summary>/定义曲线颜色/</summary>publicColorCarveCurveColorgetreturnthis.curveColor;setthis.curveColor=value;/<summary>/定义曲线画笔宽度/</summary>publicfloatCarveCurvePenWidthgetreturnthis.curvePenWidth;setthis.curvePenWid

27、th=value;/<summary>/定义曲线移动距离/</summary>publicintCarveCurveRemovegetreturnthis.curveRemove;setthis.curveRemove=value;/<summary>/定义数值节点正方形宽度/</summary>publicfloatCarveRectangleWidthgetreturnthis.rectangleWidth;setthis.rectangleWidth=value;/<summary>/定义正方形颜色/</summary&g

28、t;publicColorCarveRectangleColorgetreturnthis.rectangleColor;setthis.rectangleColor=value;/<summary>/定义显示节点数值鼠标X,Y轴容差精度/</summary>publicfloatCarveXYPrecisiongetreturnthis.xYPrecision;setthis.xYPrecision=value;/<summary>/曲线节点数据最大存储量/</summary>publicintCarveMaxNotegetreturnthis

29、.maxNote;setthis.maxNote=value;#endregion#region全局变量/<summary>/背景方格移动量/</summary>privatefloatgridRemoveX=1;/<summary>/鼠标X,Y坐标值,及该点坐标记录值、记录时间(数组)/</summary>privateCoordinatesValuenoteMessages;/<summary>/当前鼠标X,Y坐标记录数组下标值/</summary>privateintnoteNow=0;/<summary>

30、;/系统上次读取的值/</summary>privatefloatlastTimeValue=0F;/<summary>/系统窗体高度临时值,用于窗体变形时刷新数组坐标。/</summary>privateintlastTimeSystemWindowHeight=0;/<summary>/系统窗体宽度临时值,用于窗体变形时刷新数组坐标。/</summary>privateintlastTimeSystemWindowWidth=0;#endregion#region曲线数据显示#region绘制背景网格/<summary>

31、;/刷新背景网格线,并返回背景图片(背景不判断是否滚动)/</summary>/<returns>返回背景图片</returns>privateSystem.Drawing.BitmapRefBackground()if(this.picCurveShow.Height<1|this.picCurveShow.Width<1)returnnull;/创建网格背景线画笔System.Drawing.PengridPen=newPen(this.gridColor,this.gridPenWidth);/创建网格文字画笔System.Drawing.

32、PengridCompartPen=newPen(this.gridForeColor,this.gridCompart);System.Drawing.Bitmapbitmap=newBitmap(this.picCurveShow.Width,this.picCurveShow.Height);System.Drawing.GraphicsbackGroundImage=System.Drawing.Graphics.FromImage(bitmap);/坐标原点偏移backGroundImage.TranslateTransform(this.coordinate,0);/填充背景色ba

33、ckGroundImage.Clear(this.backGroundColor);/绘制表格背景线/绘制背景横轴线for(floati=this.picCurveShow.Height;i>=0;i=i-this.gridHeight)System.Drawing.PointFpointFBegin=newPointF(0F,i);System.Drawing.PointFpointFEnd=newPointF(this.picCurveShow.Width,i);backGroundImage.DrawLine(gridPen,pointFBegin,pointFEnd);/绘制背景

34、纵轴线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,poi

35、ntFEnd);绘制分隔线。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);/判断上下线值

36、是否有效(非零)System.Drawing.PenupperAndLowerPen=newPen(this.yUpperAndLowerColor,this.yUpperAndLowerPenWidth);upperAndLowerPen.DashStyle=System.Drawing.Drawing2D.DashStyle.Dash;if(this.yLower>0)floatyHeight=(float)this.picCurveShow.Height-(this.yLower/(this.yMaxValue-this.yMinValue)*(float)this.picCurv

37、eShow.Height;backGroundImage.DrawLine(upperAndLowerPen,0,yHeight,this.picCurveShow.Width,yHeight);/绘制下限文字backGroundImage.DrawString(this.yLowerString,backGroundFont,brush,-this.coordinate,yHeight-fontHight/2);if(this.yUpper>0)floatyHeight=(float)this.picCurveShow.Height-(this.yUpper/(this.yMaxVal

38、ue-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,ba

39、ckGroundFont,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

40、/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;/<summary>/刷新背景网

41、格线,并返回背景图片(背景判断是否滚动)/</summary>/<returns>返回背景图片</returns>privateSystem.Drawing.BitmapRefAndRemoveBackground()/J断是否需要移动纵轴线if(this.removeGrid)if(this.gridRemoveX>=this.gridWidth)this.gridRemoveX=1;elsethis.gridRemoveX=this.gridRemoveX+curveRemove;returnthis.RefBackground();#endreg

42、ion/<summary>/刷新背景网格线,显示曲线/</summary>publicvoidShowCurve()窗体高度发生变化,先刷新数组Y坐标值if(this.picCurveShow.Height!=this.lastTimeSystemWindowHeight)|(this.picCurveShow.Width!=this.lastTimeSystemWindowWidth)this.RefurbishArray();System.Drawing.Bitmapbitmap=this.RefAndRemoveBackground();System.Drawin

43、g.Graphicsgraphics=System.Drawing.Graphics.FromImage(bitmap);/绘制曲线/判断数组中是否有两个以上的数值/绘制直线if(this.noteNow>1)/声明绘制曲线的Point数组System.Drawing.PointpointsTemp=newPointthis.noteNow;System.Drawing.Pointpoints;/数组下标;intpointI=0;for(inti=0;i<=this.noteNow-1;i+)if(this.noteMessagesi.X>=this.coordinate)p

44、ointsTemppointI.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.curve

45、PenWidth),points);/this.picCurveShow.Image=bitmap;/<summary>/刷新背景网格线,显示曲线,自动添加即时数值/</summary>/<paramname="Value">即时数值</param>publicvoidShowCurve(floatValue)this.AddNewValue(Value);this.ShowCurve();#endregion#region自动将最新采样数值添加到数组/<summary>/自动将最新采样数值添加到数组/</s

46、ummary>/<paramname="newValue">最新采样数值</param>privatevoidAddNewValue(floatnewValue)/先判断数组下标if(this.noteNow>=this.maxNote-1)/数组已经存满数值for(inti=0;i<this.noteNow;i+)this.noteMessagesi=this.noteMessagesi+1;this.noteMessagesi.X=this.noteMessagesi.X-curveRemove;this.noteMessage

47、sthis.noteNow.Value=newValue;this.noteMessagesthis.noteNow.time=System.DateTime.Now;this.noteMessagesthis.noteNow.X=this.picCurveShow.Width;this.noteMessagesthis.noteNow.Y=(int)(this.picCurveShow.Height-(newValue/(this.yMaxValue-this.yMinValue)*this.picCurveShow.Height);else/微组未存满数值for(inti=0;i<t

48、his.noteNow;i+)this.noteMessagesi.X=this.noteMessagesi.X-curveRemove;this.noteMessagesthis.noteNow.Value=newValue;this.noteMessagesthis.noteNow.time=System.DateTime.Now;this.noteMessagesthis.noteNow.X=this.picCurveShow.Width;System.Randomr=newRandom();this.noteMessagesthis.noteNow.Y=(int)(this.picCu

49、rveShow.Height-(newValue/(this.yMaxValue-this.yMinValue)*this.picCurveShow.Height);this.noteNow+;#endregion#region窗体大小变化,自动刷新窗体Y轴值/<summary>/窗体大小变化,自动刷新窗体Y轴值/</summary>privatevoidRefurbishArray()if(this.noteNow<=0)return;for(inti=0;i<=this.noteNow;i+)this.noteMessagesi.X=this.noteM

50、essagesi.X+(this.picCurveShow.Width-this.lastTimeSystemWindowWidth);this.noteMessagesi.Y=(int)(this.picCurveShow.Height-(this.noteMessagesi.Value/(this.yMaxValue-this.yMinValue)*this.picCurveShow.Height);改变窗体大小时自动修改窗体高度临时值this.lastTimeSystemWindowHeight=this.picCurveShow.Height;this.lastTimeSystemWi

51、ndowWidth=this.picCurveShow.Width;#endregion#region显示鼠标当前点坐标值/<summary>/显示鼠标当前点坐标值/</summary>/<paramname="X">鼠标X坐标</param>/<paramname="Y">鼠标Y坐标</param>privatevoidShowMouseCoordinateMessage(intX,intY)floatx=(int)X;floaty=(int)Y;/鼠标位置在偏移量右侧时发生if(x>=this.coordinate)foreach(CoordinatesValuevalueTempinthis.no

温馨提示

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

评论

0/150

提交评论