Making drawn lines stay on screen

Gluttard

Active member
Joined
Jan 9, 2008
Messages
44
Programming Experience
Beginner
Hey, I'm hoping that you guys can help me on this, I could probably find the answer if I probed hard enough but I'm a beginner and I don't know what keywords to search for, so maybe if you answer this you could help me out with that too.

I made a drawing application that draws lines, you click down and it records where your mouse is and draws a line to where you 'unclick' the mouse. Whenever I draw a new line, the previous one disappears, so I want to know how I can prevent this.

PHP:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawLine(Color, PointX1, PointY1, PointX2, PointY2)
    End Sub
That draws the line (I took out some unrelated information), so maybe after it draws it could I store all of the lines drawn and then re-draw them after I make a new one?
Any help would be great.
 
Another way: each time you draw a new line draw it to an image, display this image.
 
Graphics.FromImage method.
VB.NET:
Dim r As New Random
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    If Me.BackgroundImage Is Nothing Then
        Me.BackgroundImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
    End If
    Dim bmp As New Bitmap(Me.BackgroundImage)
    Dim g As Graphics = Graphics.FromImage(bmp)
    Dim pt1 As New Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height))
    Dim pt2 As New Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height))
    g.DrawLine(Pens.Blue, pt1, pt2)
    g.Dispose()
    Me.BackgroundImage = bmp
End Sub
 
I'm sorry, but I just can't figure this out myself.
Where do I put the code pictured above? Here's what I have right now:
VB.NET:
Public Class Form1
    Dim PointX1 As Integer
    Dim PointX2 As Integer
    Dim PointY1 As Integer
    Dim PointY2 As Integer
    Dim Color As Pen = Pens.Black
    Dim Ready1 As Integer = 0
    Dim Ready As Integer = 0
    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Timer1.Enabled = True
        If Black.Checked = True Then
            Color = Pens.Black
        End If
        If Red.Checked = True Then
            Color = Pens.Red
        End If
        If Green.Checked = True Then
            Color = Pens.Green
        End If
        If White.Checked = True Then
            Color = Pens.White
        End If
        If Blue.Checked = True Then
            Color = Pens.Blue
        End If
        If Purple.Checked = True Then
            Color = Pens.Purple
        End If
        PointX1 = Cursor.Position.X - Me.Location.X - 4
        PointY1 = Cursor.Position.Y - Me.Location.Y - 30
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        Timer1.Enabled = False
        PointX2 = Cursor.Position.X - Me.Location.X - 4
        PointY2 = Cursor.Position.Y - Me.Location.Y - 30
        Ready1 = 1
        Me.Refresh()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Ready1 = 1 Then
            e.Graphics.DrawLine(Color, PointX1, PointY1, PointX2, PointY2)
            Ready1 = 0
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Label1.Text = Cursor.Position.X - Me.Location.X - 3 & " , " & Cursor.Position.Y - Me.Location.Y - 30
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Ready = 1
        Me.Refresh()
    End Sub

    Private Sub Form1_RubberPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Ready = 1 Then
            e.Graphics.DrawLine(Color, PointX1, PointY1, Cursor.Position.X - Me.Location.X - 3, Cursor.Position.Y - Me.Location.Y - 31)
            Ready = 0
        End If
    End Sub
End Class

Eh that might be tough to look through, but the general gist of it is that I want to draw a line, store everything drawn as 'bmp' and then when I draw another line, i'll display that bmp and have everything appear...
Right now it draws a line, then the first line vanishes and a new one is drawn.
 
MouseUp event is where you have provided yourself a new line, this is where you have to take action.
 
Um, when you program colours or shapes don't they stay on the form? While debugged of course?
 
Um, when you program colours or shapes don't they stay on the form? While debugged of course?
When you program something it does exactly what you have specified.
 
Okay, now how do I convert an integer into a point?
I want to get the cursor position relative to the bitmap stored as a point, but how do I do that? It gives me an error if I try and put:
PHP:
Dim Point1X as new point
...
...
Point1X = Cursor.Position.X - Me.Location.X
 
A Point is a coordinate system value, it has X and Y properties.
VB.NET:
dim pt as point = cursor.position
pt.X+=1
pt.Y-=123
Anyway, use e parameter of the mouse events to get the coordinate values. There is e.Location and also e.X/e.Y should you only need one of these, but they are the same as e.Location.X/Y. These coordinates are relative to the client area of the control.
 
Back
Top