真正实时曲线控件(1)

在网上查了多少回,有的说是实时曲线,但是没有一个是真正的实时曲线,今天我决定写一个真的实时曲线 ,你可以稍加修改就可利用。

1、添加一个自定义控件,添加两个PICTUREBOX控件,PICSHOW .VISIBLE设置成TRUE,另一个设置成不可见。首先在PICSHOW中进行绘图,绘图超过X轴范围时,转成PICHIDE中再绘图并等到绘图完毕,再显示出来。这样看起来就是连续的。

写 下代码

 

Public Class UcRealCurs
    Dim MaxPlotNo As Long  '最长的X轴范围

    Dim SDataPonit() As PointF  '显示图像
    Dim HdataPonit(20) As PointF

    Dim DId As Long      '序号

    Dim YMin As Single = 99999.9, YMax As Single = -99999.9
    Dim ScaleFactorX, ScaleFactorY As Single
    Dim pScalex, pScaley As Single

    Dim wid As Integer         '图像的宽度
    Dim hgt As Integer         '图像的高度
    Dim bm As Image
    Dim gr As Graphics

    '在隐形窗体上作画,画好以后再显示在显示
    Private Function PlotData(ByVal PointArray() As PointF) As Boolean

        Dim RedPen As New Pen(Color.Red), MPen As New Pen(Color.Blue), LPointArray(), LBaseArray(1) As PointF

        Dim bm2 As New Bitmap(wid, hgt)  '建立背景绘图区
        Dim gr2 As Graphics = Graphics.FromImage(bm2) '建立绘图的画布

        Dim pen As Pen = New Pen(Color.Green)
        pen.DashStyle = Drawing2D.DashStyle.Dot

        For i As Integer = 0 To hgt Step 20
            gr2.DrawLine(pen, 0, i, wid, i)
        Next

        For i As Integer = 0 To wid Step 20
            gr2.DrawLine(pen, i, 0, i, hgt)
        Next

        ReDim LPointArray(PointArray.GetUpperBound(0)) '重订区域阵列变数范围
        '最小X值
        'LblStartX.Text = DId - MaxPlotNo + PointArray(PointArray.GetLowerBound(0)).X.ToString("0.00")
        ''最大X值
        'LblEndX.Text = DId - MaxPlotNo + PointArray(PointArray.GetUpperBound(0)).X.ToString("0.00")

        '取得阵列的极限值
        For i As Integer = PointArray.GetLowerBound(0) To PointArray.GetUpperBound(0)
            If PointArray(i).Y > YMax Then YMax = PointArray(i).Y
            If PointArray(i).Y < YMin Then YMin = PointArray(i).Y
        Next

        'LblStartY.Text = YMin.ToString("0")  '最小Y值
        'LblEndY.Text = YMax.ToString("0")  '最大Y值

        '填入阵列元素
        For i As Integer = PointArray.GetLowerBound(0) To PointArray.GetUpperBound(0)
            LPointArray(i).X = PointArray(i).X * pScalex
            '和上方Y零点的距离
            LPointArray(i).Y = PointArray(i).Y * pScaley
        Next

        gr2.DrawLines(RedPen, LPointArray) '绘图
        PicHide.Image = bm2  '将背景绘图资料放到图片框中
        PicHide.Refresh()  '重新显示
        gr2.Dispose()  '清除画布
        RedPen.Dispose() '清除资源

    End Function

    Sub Plot(ByVal StartX As Single)

        ScaleFactorX = MaxPlotNo
        ScaleFactorY = 300 ' YMax - YMin
        pScalex = PicShow.Width / ScaleFactorX        'X轴的比例系数
        pScaley = (PicShow.Height * 0.97) / ScaleFactorY   'Y轴的比例系数

        ReDim Preserve SDataPonit(DId)
        SDataPonit(DId).X = DId * pScalex
        SDataPonit(DId).Y = StartX * pScaley

        If DId < MaxPlotNo Then

            If DId > 1 Then
                gr.DrawLine(Pens.Red, SDataPonit(DId - 1), SDataPonit(DId))
            End If
            PicShow.BringToFront()
            PicShow.Image = bm
            PicShow.Refresh()

        Else

            PicShow.Visible = False
            PicHide.Visible = True

            For i As Integer = 0 To MaxPlotNo

                HdataPonit(i).X = i ' + DId - 20               
 HdataPonit(i).Y = SDataPonit(i + DId - 20).Y

            Next

            PlotData(HdataPonit)

        End If

        DId = DId + 1

        ' gr.Dispose()

    End Sub

    Private Sub UcRealCurs_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        wid = PicShow.ClientSize.Width
        hgt = PicShow.ClientSize.Height
        bm = New Bitmap(wid, hgt)  '建立背景绘图区
        gr = Graphics.FromImage(bm) '建立绘图的画布
        MaxPlotNo = 20
        Dim pen As Pen = New Pen(Color.Green)
        pen.DashStyle = Drawing2D.DashStyle.Dot

        For i As Integer = 0 To hgt Step 20
            gr.DrawLine(pen, 0, i, wid, i)
        Next

        For i As Integer = 0 To wid Step 20
            gr.DrawLine(pen, i, 0, i, hgt)
        Next

    End Sub
End Class

图片


这里只是设定一个20个数据的宽度,但是这个宽度是可以调整的,不过如果想在子线程来运行,就需要对其它的几个控件进行委托来调用显示,这将再以后几节中为说明。

源程序到我的资源里下载

 

http://download.csdn.net/detail/wl58796351/4167857

你可能感兴趣的:(编程技巧,编程随想,日常工作)