VB中实时曲线的显示_第1页
VB中实时曲线的显示_第2页
VB中实时曲线的显示_第3页
VB中实时曲线的显示_第4页
VB中实时曲线的显示_第5页
全文预览已结束

下载本文档

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

文档简介

1、VB 中实时曲线的显示在使用 VB 开发工控软件 (HMI) 时,经常需要对工艺参数进行趋势曲线的显示,这通常 需要使用控件来实现, 自然有第三方提供的控件, 但那是需要付费的, 并且有的使用情况并 不理想,自己开发的话又差强人意, 这里提供一个实时曲线显示的程序,给大家以启发。通 过对程序的修改,可以很方便的应用到实际工程中去。首先建立一个名为 DrawLine 的类模块,代码如下:Public HorzSplits As LongPublic VertSplits As LongPublic Max As SinglePrivate ValueArray() As Single '

2、 存放数据的数组Private LineColor As LongPrivate GridColor As LongPrivate ShowGrid As BooleanPrivate pBox As PictureBoxPrivate pBoxHeight As LongPrivate pBoxWidth As LongPrivate MovingGrid As BooleanPrivate StartPosition As LongPrivate GridPosition As LongPublic Enum DrawLineTypeTYPE_LINE = 0 TYPE_POINT = 1

3、 End EnumPublic LineType As DrawLineType ' 划线的类型:线或点Const const_tolerance = 0.0001 ' 误差Public Function InitDrawLine(pB As PictureBox, LColor As Long, SGrid As Boolean, Optional GColor As Variant, Optional MoveGrid As V ariant) pB.ScaleMode = vbPixelsLineColor = Lcolor ShowGrid = Sgrid pBoxHe

4、ight = pB.ScaleHeight pBoxWidth = pB.ScaleWidthIf IsMissing(GColor) ThenGridColor = RGB(0, 130, 0) ' 默认值绿色Else:GridColor = GcolorEnd IfIf IsMissing(MoveGrid) ThenMovingGrid = False ' 如果用户未定 MoveGrid 值则默认为关。Else:MovingGrid = MoveGridEnd IfSet pBox = pB'分配数组ReDim ValueArray(pBoxWidth - 1)S

5、tartPositi on = pBoxWidth -1GridPosition = 0End FunctionPublic Sub AddValue(value As Single)Dim l As Long ' 检查 InitDrawline 是否被执行,失败则退出If pBox Is Nothing ThenExit SubEnd If'将数组所有值移动一位。For l = 1 To pBoxWidth -1ValueArray(l - 1) = ValueArray(l)NextIf Max <= 0 Then Max = 1'把新的值添加到数组的最后一个

6、元素。ValueArray(l - 1) = pBoxHeight - (value / Max) * pBoxHeight)If StartPosition >= 1 Then StartPosition = StartPosition-1GridPosition = GridPosition -1End SubPublic Sub RePaint()Dim x As SingleDim y As SingleDim l As LongIf pBox Is Nothing ThenExit SubEnd If'首先清除图片,然后画网格( 如果有的话 ),最后画线。pBox.Cl

7、sIf (ShowGrid) ThenpBox.ForeColor = GridColorIf (MovingGrid) ThenFor x = GridPosition To pBoxWidth - 1 Step (pBoxWidth - 1) / (VertSplits + 1) - const_tolerance pBox.Line (x, 0)-(x, pBoxHeight)NextElse:For x = 0 To pBoxWidth - 1 Step (pBoxWidth - 1) / (VertSplits + 1) - const_tolerance pBox.Line (x,

8、 0)-(x, pBoxHeight)NextEnd IfFor y = 0 To pBoxHeight - 1 Step (pBoxHeight - 1) / (HorzSplits + 1) - const_tolerance pBox.Line (0, y)-(pBoxWidth, y)Next'网格复位If GridPosition <= -Int(pBoxWidth - 1 / (HorzSplits + 1) Then GridPosition = 0End IfEnd IfIf StartPosition <= pBoxWidth - 1 Then pBox.

9、ForeColor = LineColor Select Case DiagramType Case TYPE_LINEFor l = StartPositi on + 1 To pBoxWidth -2 pBox.Line (l, ValueArray(l)-(l + 1, ValueArray(l + 1) NextCase TYPE_POINTFor l = StartPositi on + 1 To pBoxWidth -2 pBox.PSet (l + 1, ValueArray(l + 1) NextEnd SelectEnd IfEnd Sub然后在窗体中添加四个 picture

10、box 控件,添加代码如下:Public LDraw1 As New DrawLinePublic LDraw2 As New DrawLinePublic PDraw1 As New DrawLinePublic PDraw2 As New DrawLinePublic tancounter As SinglePrivate Sub Command1_Click()'.InitDrawLine picturebox, lcolor, sgrid, gcolor, movegrid 'picturebox = 要划线的 picturebox'lcolor = 线的颜色&

11、#39;sgrid = 是否使用网格'gcolor = optional 网格颜色 (默认值为绿色 )'movegrid = optional 网格是否移动 (默认值不移动 )With LDraw1.InitDrawLine Picture_line, vbWhite, True.Max = 10 .HorzSplits = 9 .VertSplits = 9.LineType = TYPE_LINE.RePaintEnd WithWith PDraw1.InitDrawLine Picture_point, vbRed, True .Max = 20.HorzSplits =

12、 9.VertSplits = 9 .LineType = TYPE_POINT .RePaint End With With LDraw2 .InitDrawLine Picture_line2, vbGreen, True, , True .Max = 5 .HorzSplits = 9 .VertSplits = 9.LineType = TYPE_LINE .RePaint End With With PDraw2.InitDrawLine Picture_point2, vbYellow, True, RGB(100, 100, 0), True .Max = 10.HorzSpli

13、ts = 9 .VertSplits = 9 .LineType = TYPE_POINT .RePaintEnd With End Sub Private Sub Picture_line_Paint() LDraw1.RePaint End SubPrivate Sub Picture_line2_Click() LDraw2.RePaintEnd SubPrivate Sub Picture_point_Paint() PDraw1.RePaintEnd SubPrivate Sub Picture_point2_Click() PDraw1.RePaintEnd SubPrivate Sub Timer1_Timer()Dim value As Single tancounter = tancounter + 0.1 value = Sin(tancounter) + 2 LDraw1.AddValue value LDra

温馨提示

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

评论

0/150

提交评论