GDI+ Persistency
I am trying to draw a shapes on one of the control on the form (like panel or picturebox ) by mouse clicks. I am wanting to drawing lines as in the Microsoft Paint; mouse down for starting point, mouse up for end point. I tried to do very simple program as (not by points, by stating their coordinates in public variables):
Imports System.Drawing
Imports System.IO
Public Class Form1
Public x1, y1, x2, y2 As Integer
Public drawclick As Boolean
Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
Dim grap As Graphics = pe.Graphics
MyBase.OnPaint(pe)
' I also add the drawing code here! But no success
If drawclick = True Then
grap.DrawLine(Pens.Blue, x1, y1, x2, y2)
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
x1 = e.X
y1 = e.Y
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
x2 = e.X
y2 = e.Y
drawclick = True
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If drawclick = True Then
Dim grap As Graphics = e.Graphics
grap.DrawLine(Pens.Blue, x1, y1, x2, y2)
End If
End Sub
End Class
and when i debug the program i noticed that, although program successfully enter the code inside the paint event of picturebox control, no line was drawn. I could not understand the reason, i controlled the variables x1,y1,x2 and y2 inside the paint event, they are correctly transferred but no drawing can be seen? In addition, when i minimize and maximize the form, only the last drawn line drawn, not lines drawn before. Can anyone help me about this situation? Where is the problem? I read some forums about onpaint event, but i do not understand the "mybase.onpaint", i only want to draw a line on the picturebox control, in object browser, onpaint event for picturebox stated as "ByVal pe As System.Windows.Forms.PaintEventArgs", different than "e". What is the difference i do not understand? Can VB understand that, "pe" for picturebox? If yes, how? I am a beginner to VB, and i use VB 2005, thanks for any help.