在现在在写一个实时曲线的程序,我在网上看了一篇关于VB画实时曲线的文章,是“应用VB4.0实现工业控制的实时曲线和历史曲线”
    该文章的“实时曲线”的原文如下:
    1、选择需要显示的窗体Form1,加入图片框Picture1,根据实际需要设置图片的大小并移到合适的位置,并在图片的外面画好量程----时间坐标系;
    2、 在全局模块中定义位块传输API函数BitBlt( )和全局变量:
Declare Function BitBlt Lib "GDI32" (ByVal hDestDC AS Long,ByVal X As 
Long,ByVal Y As Long,ByVal nWidth As Long,ByVal nHeight As Long,ByVal 
hSrcDC As Long,ByVal xSrc As Long,ByVal ySrc As Long,ByVal DrawStyle As 
Long) As Long
Gobal S As Long `量程
Gobal L As Long `上一次的纵坐标值
Gobal T As Integer `上一次时间值(分)
3、 Private Sub Form1_Load()
Picture1.AutoRedraw = False `曲线不重画
Picture1.ScaleMode = 3 `以象素方式(Pixel)
L = -1 `设置初值
S = 400
T = -1
End Sub
4、 根据现场数据采集的采样频率,设置定时器Timer1的定时值,曲线移动就在
Timer1实现:
Private Sub Timer1_Time()
Dim w As Long, h As Long, y1 As Long, Data As Long 
Dim hBmp As hDC, ShowMode As Long, ii As Long, t1 As Integer
w = Picture1.Width
h = Picture1.Height
hBmp = Picture1.hDC
ShowMode = &HCC0020 `ROP模式(复制)
Data = Get_RealDatabase( ) `从实时数据库取当前监控值
y1 = (S - Data) / S * h `根据量程转变成具体坐标 
ii = BitBlt(hBmp,0,0,w-1,h,hBmp,1,0,ShowMode) `整个曲线右移一个像素点
Picture1.Line (w - 1,y) - (w,y1) , RGB(0,255,0) 
y = y1
t1 = Val(Mid$(Time$,3,2))
If T < > t1 Then `在曲线下方显示时间(用分表示)
Picture1.CurrentX = w - 16
Picture1.CurrentY = h - 8
Picture1.Print Mid$(Time$,1,5)
T = t1
End If
End Sub
         但是我按文章所使用的方法写程序时,在程序运行时所画的线是有问题的。
     我的源程序如下:
Private Sub Timer1_Timer()
Dim w As Long, h As Long, y1 As Long, data As Long
Dim hBmp As Long, ShowMode As Long, ii As Long, t1 As Integer
w = Picture1.ScaleWidth
h = Picture1.ScaleHeight
hBmp = Picture1.hDC
ShowMode = &HCC0020      'ROP模式(复制)data = CInt(Trim(Text1.Text))                '从实时数据库取当前监控值
y1 = (S - data) / S * h                  '根据量程转变成具体坐标
ii = BitBlt(hBmp, 0, 0, w - 1, h, hBmp, 1, 0, ShowMode)  '整个曲线右移一个像素点
Picture1.Line (w - 1, y)-(w, y1), vbRed
y = y1
t1 = Val(Mid$(Time$, 3, 2))
If T <> t1 Then                             '在曲线下方显示时间(用分表示)
Picture1.CurrentX = w - 15
Picture1.CurrentY = h - 8
Picture1.Print Mid$(Time$, 1, 5)
T = t1
End If      希望各位高手帮忙找出原因,小弟在此多谢了!!

解决方案 »

  1.   

    本人正在熟悉MSCHART的使用,对这玩意还没搞太明白~
    不懂就要顶
      

  2.   

    同样的问题我也遇到过,直到十几天前。这里有一个斑竹写的cpu监视器的程序,其中就有实时曲线,好好研究以下,不要被代码量吓住啊,真真实现这个功能只有几句话。
    http://www.sijiqing.com/vbgood/download/code/CS_CPU_Monitor.zip
      

  3.   


    谢谢大家的回复  
       该程序执行时所画的线有一点怪,在画一条直线时,它总是在PictureBox控件的底部画一条很粗的直线.在数据改变时它所画的线也不是曲线,而是一段一段的直线。最好请大家运行一下这个源程序,帮忙找出原因。
      

  4.   

    To laviewpbt(pbt)
     谢谢你的回复
     你所给的下载网址我怎么不下载,请问一下还有没有别的下载网址
      

  5.   

    to laviewpbt(pbt)
      我看了一下这个程序,它的方法和我的不太一样,我的程序是用BitBlt API函数来实现的。每次画线时,先用BitBlt API函数把Picture box的整个图向左移一个像素,然后再根据实时数据来画线。
      

  6.   

    不知道下面的例子对你有没有帮助。两个picturebox,四个optionbutton,两个commandbutton,一个HScroll1,max=10,其中一个picturebox中载入一副带有网格线的图片,做为背景用,(运行时这个picturebox看不见)
    optionbutton的标题是rnd,sin,x^2,pulse.Option Explicit
    Private Declare Function BitBlt& Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long)
    Private Const PS_SOLID& = 0         '实线
    Private Const SRCCOPY& = &HCC0020
    Private Declare Function DeleteObject& Lib "gdi32" (ByVal hObject As Long)
    Private Declare Function SelectObject& Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long)
    Private Declare Function CreatePen& Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long)
    Private Declare Function Rectangle& Lib "gdi32" (ByVal hDC As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long)
    Private Declare Function MoveToEx& Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal lpPoint As Long)
    Private Declare Function LineTo& Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long)
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private dX As Long
    Private dy As Long
    Private cuX As Long
    Private cuCopyX As Long
    Private picHeigth As Integer
    Private picWidth
    Private isGrap As Integer
    Private MyTimeEnabld As Boolean
    Private wait As IntegerPrivate Sub Command1_Click()
       End
    End SubPrivate Sub Command2_Click()
        isStop = Not isStop
    End Sub
    Private Sub Command4_Click()
        If Command4.Caption = "Start" Then
           Command4.Caption = "Stop"
        Else
           Command4.Caption = "Start"
        End If
        If MyTimeEnabld = False Then
            MyTimeEnabld = True
            MyTimer
        Else
            MyTimeEnabld = False
        End If
    End SubPrivate Sub Form_Load()
        Height = 3300: Width = 5475
        Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
        setValue
        isGrap = 1
        HScroll1.Value = 5
       
    End Sub
    Sub setValue()
        Dim dl&
        
        form1.ScaleMode = vbPixels
        Picture1.ScaleMode = vbPixels
        Picture2.ScaleMode = vbPixels
        Picture1.AutoRedraw = True
        Picture2.AutoRedraw = False    picHeigth = Picture2.Height
        picWidth = Picture2.Width - 5
        dy = Picture2.Height \ 2
        dX = picWidth
        cuCopyX = 0
        dl& = MoveToEx(Picture1.hDC, dX, dy, 0&)
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
       End
    End Sub
    Private Sub HScroll1_Change()
    wait = HScroll1.Value
    Label1.Caption = HScroll1.Value
    End Sub
    Private Sub Option1_Click(Index As Integer)
       Picture1.Cls
       setValue
       isGrap = Index
    End Sub
    Private Sub MyTimer()
         Do
            Select Case isGrap
            Case 0
               Grap MyRnd()
            Case 1
               Grap MySin()
            Case 2
               Grap Xx()
            Case 3
               Grap Pulse()
            End Select
            Sleep wait
            DoEvents
            If MyTimeEnabld = False Then
                    Exit Do
            End If
         Loop
    End SubSub Grap(cuY As Long)
        Dim dl&
        Dim pen&, oldpen&
        If cuY < 0 Then cuY = 0
        If cuY > picHeigth Then cuY = picHeigth
        cuX = dX + 1
        cuCopyX = cuCopyX + 1
        If cuCopyX > picWidth Then
            dX = picWidth
            cuX = dX + 1
            cuCopyX = 1
            Picture1.Cls
            dl& = MoveToEx(Picture1.hDC, dX, dy, 0&)
               dl& = BitBlt(Picture1.hDC, 0, 0, picWidth, picHeigth, Picture2.hDC, 0, 0, SRCCOPY)
        End If
        
        pen& = CreatePen(PS_SOLID, 1, RGB(255, 0, 0))
        oldpen& = SelectObject(Picture1.hDC, pen&)
        dl& = LineTo(Picture1.hDC, cuX, cuY)
        dl& = SelectObject(Picture1.hDC, oldpen&)
        dl& = DeleteObject(pen&)
        dl& = BitBlt(Picture2.hDC, 0, 0, picWidth, picHeigth, Picture1.hDC, cuCopyX, 0, SRCCOPY)
        dy = cuY: dX = dX + 1
        If dy > 200 Then Stop
    End Sub
    '/////////////////////////////////
    Function MyRnd() As Long
       MyRnd = Rnd() * picHeigth
    End Function
    Function MySin()
        Static Radim As Integer
            MySin = Sin(Sin(Radim * 3.1426 / 180)) * picHeigth \ 2 + picHeigth \ 2
            Radim = Radim + 4
            If Radim > 360 Then Radim = 0
    End Function
    Function Xx()
        Static x As Integer
        Xx = x * x / 50
        x = x + 1
        If x = 100 Then x = 0
    End Function
    Function Pulse()
        Static x As Integer
        Static y As Integer
        If x < 10 Then
            y = y + 1
            Pulse = 25
            If y > 50 Then
                x = 20
            End If
        Else
            y = y - 1
            Pulse = 125
            If y < 0 Then
                x = 0
            End If
        End If
        End Function