Link to home
Create AccountLog in
Avatar of schenkp
schenkp

asked on

EKG Lines

HI,
Im interested in drawing some lines in realtime on a form over and over agian, from one point to another.  It might look the way an EKG looks, not sure where to begin though.

Thanks

Peter
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) Begin with the Paint event for the form.  

2) Look at GDI+.  

3) There are options to draw lines, curves, beziers, rectangles, etc.

Bob
For an EKG type graph where things scroll to the left and then disappear you could use the BitBlt API.  Basically you start by shifting your "graph" one pixel to the left via BitBlt().  Then you simply draw the new points at the very rightmost X position in your graph.  Repeat...

This works because the stuff that scrolls to the left doesn't change amplitude...it just changes X position.  The only new things that need to be drawn are the very rightmost points.

I haven't done this in C# but it have used it with great success in VB6...
Hi, Mikey :)

Bob
G'Afternoon Bob!

I'm off to lunch with the kids (it's spring break)...

=)
Avatar of schenkp
schenkp

ASKER

Idle_Mind
Do you have an example of a vb project or some vb code?
Here is a VB6 example.  Create a New Project and add a PictureBox and a Timer control.  Set the forms BorderStyle property to 1- Fixed Single:

Option Explicit

Private Const SRCCOPY = &HCC0020

Private Declare Function BitBlt Lib "gdi32" ( _
    ByVal hDestDC As Long, _
    ByVal x As Long, ByVal y As Long, _
    ByVal nWidthMax As Long, ByVal nHeightMax As Long, _
    ByVal hSrcDC As Long, _
    ByVal xSrc As Long, ByVal ySrc As Long, _
    ByVal dwRop As Long) As Long

Private cnt As Integer
Private lastValue As Single
Private baseline As Long

Private Sub Form_Load()
    Randomize Timer
    Me.Width = 9900
    Me.Height = 3855
    Me.Caption = "Real Time Graph with Grid Demo"
    Picture1.ScaleMode = vbPixels
    Picture1.AutoRedraw = True
    Picture1.Appearance = 0 ' flat
    Picture1.BorderStyle = 0 ' none
    Picture1.BackColor = vbBlack
    Picture1.Left = 0
    Picture1.Top = 0
    Picture1.Width = Me.ScaleWidth
    Picture1.Height = Me.ScaleHeight
    baseline = RGB(255 * 0.75, 255 * 0.75, 255 * 0.75)
    Timer1.Interval = 25
    Timer1.Enabled = True
    lastValue = Picture1.ScaleHeight / 2 ' start in the middle
    cnt = -1
End Sub

Private Sub Timer1_Timer()
    ' move everything to the left one pixel
    Call BitBlt(Picture1.hDC, -1, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, Picture1.hDC, 0, 0, SRCCOPY)
   
    ' clear the last column
    Picture1.Line (Picture1.ScaleWidth - 1, 0)-(Picture1.ScaleWidth - 1, Picture1.ScaleHeight), Picture1.BackColor
   
    ' draw the grid first
    cnt = cnt + 1
    If cnt Mod 25 = 0 Then
        cnt = 0
        Picture1.Line (Picture1.ScaleWidth - 1, 0)-(Picture1.ScaleWidth - 1, Picture1.ScaleHeight), vbWhite
    Else
        Dim i As Integer
        For i = 25 To Picture1.ScaleHeight Step 25
            Picture1.PSet (Picture1.ScaleWidth - 1, i), vbWhite
        Next
    End If
   
    ' make the plot go up and down randomly
    Dim delta As Single
    Dim chance As Single
    Dim modifier As Single
    Dim newValue As Single
    Dim newValue2 As Single
   
    Select Case lastValue
        Case Is < Picture1.ScaleHeight * 1 / 3
            modifier = (Picture1.ScaleHeight * 1 / 3 - lastValue) / Picture1.ScaleHeight * 1 / 3 * 0.5
            chance = 0.5 + modifier
            delta = (1 - chance) * (CSng(Picture1.ScaleHeight) / CSng(25) + 1) * Rnd
            delta = IIf(Rnd() <= chance, delta, -1 * delta)
           
        Case Is > Picture1.ScaleHeight * 2 / 3
            modifier = (lastValue - Picture1.ScaleHeight * 2 / 3) / Picture1.ScaleHeight * 1 / 3 * 0.5
            chance = 0.5 + modifier
            delta = (1 - chance) * (CSng(Picture1.ScaleHeight) / CSng(25) + 1) * Rnd
            delta = IIf(Rnd() <= chance, -1 * delta, delta)
           
        Case Else
            chance = 0.5
            delta = (1 - chance) * (CSng(Picture1.ScaleHeight) / CSng(25) + 1) * Rnd
            delta = IIf(Rnd() < chance, -1 * delta, delta)
           
    End Select
    newValue = lastValue + delta
    If newValue < 0 Then
        newValue = 0
    ElseIf newValue > Picture1.ScaleHeight - 1 Then
        newValue = Picture1.ScaleHeight - 1
    End If
           
    ' change the color based on its amplitude
    Dim p As Single
    Dim clr As Long
    p = lastValue / Picture1.ScaleHeight
    clr = RGB(255, (1 - p) * 192, (1 - p) * 192)
   
    ' draw a line from the last point to the new point
    Picture1.Line (Picture1.ScaleWidth - 2, lastValue)-(Picture1.ScaleWidth - 1, newValue), clr
   
    ' store the new point
    lastValue = newValue
End Sub
Avatar of schenkp

ASKER

Idle_Mind,
Could you please email mail me this project to <email deleted>, I am having issues getting this to work.

Thanks

Peter

TheLearnedOne
Page Editor
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of schenkp

ASKER

I checked agian still has not come through?
Avatar of schenkp

ASKER

GOT IT :)