Private Class Line
Public ptA As PointF
Public ptB As PointF
End Class
Dim startPoint As Point
Dim endPoint As Point
Dim lines As New ArrayList
Dim prevClip As Rectangle
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
startPoint = New Point(e.X, e.Y)
endPoint = startPoint
ControlPaint.DrawReversibleLine(Panel1.PointToScreen(startPoint), Panel1.PointToScreen(endPoint), Color.Black)
prevClip = Cursor.Clip
Cursor.Clip = Me.RectangleToScreen(New Rectangle(Panel1.Location, Panel1.Size))
End If
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
ControlPaint.DrawReversibleLine(Panel1.PointToScreen(startPoint), Panel1.PointToScreen(endPoint), Color.Black)
endPoint = New Point(e.X, e.Y)
ControlPaint.DrawReversibleLine(Panel1.PointToScreen(startPoint), Panel1.PointToScreen(endPoint), Color.Black)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim i As New Line
i.ptA = New Point(startPoint.X / Panel1.Width, startPoint.Y / Panel1.Height)
i.ptB = New Point(endPoint.X / Panel1.Width, endPoint.Y / Panel1.Height)
lines.Add(i)
Cursor.Clip = prevClip
End If
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
Dim g As Graphics = e.Graphics
Dim p As New Pen(Color.Black, 5.0F)
Dim i As Line
For Each i In lines
g.DrawLine(p, Panel1.Width * i.ptA.X, Panel1.Height * i.ptA.Y, _
Panel1.Width * i.ptB.X, Panel1.Height * i.ptB.Y)
Next
End Sub