实时曲线的绘制和保存

来源:互联网 发布:专业流程图软件 编辑:程序博客网 时间:2024/06/14 15:09
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

       在VB中绘制实时曲线是比较难的,一般要应用第三方控件或是Windows API函数来完成,但是如果你对实时曲线的要求不是很高,只要能表示出当前的一般情况的话,我们可以直接应用VB提供给我们的空间来完成.

       原则上讲,直接在Form里绘制曲线都是可以的,MSDN上面很多例程就是直接在Form里面绘制图形的,Form作为绘制图形的容器,不过一般应用中Form中不可避免的会有很多其他控件,所以我们选择PictureBox作为绘制曲线的容器.

       实时曲线绘制一般借助于Timer控件来完成,使用Timer控件,定期将串口或是其他仪器中监测到的数据送往PictureBox1,而曲线的绘制一般画成折线图,采用PictureBox1的Line方法绘制.具体实现如下:

       1.选择需要显示的窗体Picture1,加入图片框Picture1,根据实际需要设置图片的大小并移到合适的位置,并在图片的外面画好量程----时间坐标系;然后加上Timer控件以及两个CommandButton,界面就基本设置好了.

     2.建立坐标系,根据Picture1的大小和高度设置画出坐标系的X轴和Y轴:

Picture1 .ScaleMode = 1  ‘以VB的基本单位作为建立坐标轴以及绘制图形的单位;
Picture1.Refresh
Picture1.CurrentX = Picture1.ScaleLeft +100
Picture1.CurrentY = Picture1.ScaleTop
Picture1.Print  Picture1.ScaleHeight - 100
Picture1.Line(Picture1.ScaleLeft+100,Picture1.ScaleTop+100)-(Picture1.ScaleLeft+100, Picture1.ScaleHeight - 100)
Picture1.CurrentX = Picture1.ScaleLeft +100
Picture1.CurrentY = Picture1.ScaleHeight
Picture1.Print  “(0,0)”
Picture1.Line (Picture1.ScaleLeft + 100, Picture1.ScaleHeight - 100)-(Picture1.ScaleWidth - 100, Picture1.ScaleHeight - 100)
Picture1.CurrentX = Picture1.ScaleWidth
Picture1.CurrentY = Picture1.ScaleHeight
Picture1.Print  Picture1.ScaleWidth-100
Picture1.AutoRedraw = True        ‘必要时,用存储在内存中的图象进行重绘

       3.绘制曲线并保存,我们这里以正弦曲线作为绘制曲线的数据来源,具体应用是可以采用由串口或其他仪器采集得到的数据.首先我们绘制一条中线,然后在Timer控件的Time事件中绘制曲线:

Picture1.Line (Picture1.ScaleLeft, CInt(Picture1.ScaleHeight / 2))-(Picture1.ScaleWidth, CInt(Picture1.ScaleHeight / 2)) ‘绘制中线
Private Sub Timer1_Timer()
Dim y1 As Integer
y1 = CInt(Sin((x - Picture1.Left) / 20 / 180 * pi) * Picture1.ScaleHeight / 2)
y1 = CInt((Picture1.ScaleHeight + 1000) / 2) - y1
Picture1.Line (x, y)-(x + 20, y1)
x = x + 20
y = y1
If x >= Picture1.ScaleWidth Then
    SavePicture Picture1.Image, "c:sin.bmp"    ‘保存图画,可以根据实际需要命名图片
    x = 0
    y = Picture1.ScaleHeight / 2
    Picture1.Cls   ‘清屏重画
   End If
End Sub

       从上面的过程可以看出,其实对于要求不高的实时曲线绘制还是比较简单的,在这里我采用的是清屏重画图像,如果要实现图像往左移动,图像仍然保留的效果,可以采用Windows的Bitblt函数,可以让图像每次移动一个象素或是多少个twip,具体实现可以参见<<应用VB4.0实现工业控制的实时曲线和历史曲线>>( http://www.swm.com.cn/yingyong/rj-98-yy4/98-y4-yy6.htm).
 

       以上只是实时曲线绘制的一点简单说明,具体应用中可能需要花更多的功夫修饰图像,显示时间(可以依据上面的代码中的Picture1.Print实现),但是基本原理大同小异.

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击