I am writing a small screen saver program to draw lines on screen using a timer.
For this, two data points A & B are read from a file and the (x,y) coordinates in pixels for A & B computed.
The line from point A to B is drawn using graphics.drawline method. So far it works fine.
The next line from point B to C is drawn similarly.
The problem is that when line BC is drawn, the line AB is not retained onscreen.
How can I draw line segments BC, CD, DE and so on using a timer while retaining the previous ones ?
I am using VB 2010.
Essentially, I'm drawing a line made up of numerous segments, where each segment is to to be drawn using timer tick event.
What I have tried:
I've tried without using 'refresh' in the timer.tick sub.
here's the code
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports System.Math
Imports System.Drawing.Drawing2D
Public Class frmscr
Dim greenPen2 As New Pen(Color.Green, 5)
Dim grayPen1 As New Pen(Color.Gray, 1)
Dim WithEvents tmrClock As New Timer
Public point1 As New Point
Public point2 As New Point
Private Sub frmscr_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Read input data file,reads array of GR
Call ReadTxtInputFile()
'Get screen size in pixels
scrW = Screen.PrimaryScreen.Bounds.Width.ToString
scrH = Screen.PrimaryScreen.Bounds.Height.ToString
Windows.Forms.Cursor.Hide()
'initialize variable i
i = 1
'Compute screen x,y of start point in pixels
'where xo1p is x offset in pixels
'gr(0) is 1st point of array
't1Wp is width of 1st vertical track
'grmax & grmin are max & min values of parameter array value gr(i)
xGR0 = xo1p + t1Wp * (GR(0) - GRmin) / (GRmax - GRmin)
yGR0 = 0
'set initial values of second point & its backup value
xGR1 = xGR0
xGR2 = xGR0
yGR1 = yGR0
yGR2 = yGR0
' Set Pen Styles
greenPen2.DashStyle = Drawing2D.DashStyle.Solid
grayPen1.DashStyle = Drawing2D.DashStyle.DashDotDot
'timer
tmrClock.Enabled = True
tmrClock.Interval = 50
tmrClock.Start()
End Sub
Private Sub frmscr_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Windows.Forms.Cursor.Show()
End Sub
Private Sub frmscr_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Me.Close()
End Sub
Private Sub frmscr_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Me.Close()
End Sub
Private Sub frmscr_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Me.Close()
End Sub
Private Sub frmscr_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
Me.Close()
End Sub
Private Sub frmscr_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Static OldX As Integer
Static OldY As Integer
'Determines whether the mouse was moved and whetherthe movement was large.
''If so, the screen saver is ended.
If (OldX > 0 And OldY > 0) And (Math.Abs(e.X - OldX) > 3 Or Math.Abs(e.Y - OldY) > 3) Then
Me.Close()
End If
''Assigns the current X and Y locations to OldX and OldY.
OldX = e.X
OldY = e.Y
End Sub
Private Sub DrawGrids(ByVal e As PaintEventArgs)
' Define Vertical grid points : track width in % : default values
t1W = 15
'track width in pixels
t1Wp = t1W * scrW / 100
'track x offset in pixels
xo1p = 0
' Create points that define line.
'vertical grid
Dim ii As Integer
For ii = 1 To 4 Step 1
Dim point1 As New Point(t1Wp / 10 * ii, 0)
Dim point2 As New Point(t1Wp / 10 * ii, scrH)
e.Graphics.DrawLine(grayPen1, point1, point2)
Next ii
For ii = 6 To 9 Step 1
Dim point1 As New Point(t1Wp / 10 * ii, 0)
Dim point2 As New Point(t1Wp / 10 * ii, scrH)
e.Graphics.DrawLine(grayPen1, point1, point2)
Next ii
For ii = 0 To 10 Step 5
Dim point1 As New Point(t1Wp / 10 * ii, 0)
Dim point2 As New Point(t1Wp / 10 * ii, scrH)
e.Graphics.DrawLine(Pens.Blue, point1, point2)
Next ii
'horizontal grid
nhThk = scrH / 75
For ii = 1 To nhThk Step 1
Dim point1 As New Point(0, 75 * ii)
Dim point2 As New Point(t1Wp, 75 * ii)
e.Graphics.DrawLine(grayPen1, point1, point2)
Next ii
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
'routine to draw the grid
DrawGrids(e)
'routine to draw the curve
DrawCurve(e)
End Sub
Private Sub DrawCurve(ByVal e As PaintEventArgs)
e.Graphics.DrawLine(greenPen2, xGR1, yGR1, xGR2, yGR2)
'stores
xGR1 = xGR2
yGR1 = yGR2
'clear screen after line reaches screen bottom
If i >= (scrH / 3.75) - 1 Then
i = 1
ClearForm(e)
yGR1 = 0
End If
End Sub
Private Sub ClearForm(ByVal e As PaintEventArgs)
'To clear the screen before continuing drawing
e.Graphics.Clear(Color.Black)
End Sub
Private Sub tmrClock_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles tmrClock.Tick
'increment variable i to compute screen location of next point
i = i + 1
'computes screen location (x,y) of next point
xGR2 = xo1p + t1Wp * (GR(i) - GRmin) / (GRmax - GRmin)
yGR2 = (DEPT(i) - DepthFirst) * 25
Refresh()
End Sub
End Class