The MouseDown, MouseMove, and MouseUp event handlers draw the line. When the control needs to clear the previous line, it raises the Redraw event. The main program should redraw the PictureBox.
When the user releases the mouse, the control raises its LineSelected event.
|
Public Event Redraw()
Public Event LineSelected(ByVal x0 As Single, ByVal y0 As _
Single, ByVal x1 As Single, ByVal y1 As Single)
' Start drawing.
Private Sub m_Canvas_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
m_Canvas.MouseDown
If e.Button <> MouseButtons.Left Then Exit Sub
m_Drawing = True
' Save the point.
m_X0 = e.X
m_Y0 = e.Y
m_X1 = e.X
m_Y1 = e.Y
' Draw the line.
m_Canvas.CreateGraphics.DrawLine(Pens.Black, m_X0, _
m_Y0, m_X1, m_Y1)
End Sub
' Continue drawing.
Private Sub m_Canvas_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
m_Canvas.MouseMove
If Not m_Drawing Then Exit Sub
' Erase the previous line.
RaiseEvent Redraw()
' Save the new point.
m_X1 = e.X
m_Y1 = e.Y
' Draw the line.
m_Canvas.CreateGraphics.DrawLine(Pens.Black, m_X0, _
m_Y0, m_X1, m_Y1)
End Sub
' Finish drawing.
Private Sub m_Canvas_MouseUp(ByVal sender As Object, ByVal _
e As System.Windows.Forms.MouseEventArgs) Handles _
m_Canvas.MouseUp
If Not m_Drawing Then Exit Sub
m_Drawing = False
' Raise the LineSelected event.
' The main program can refresh the canvas if desired.
RaiseEvent LineSelected(m_X0, m_Y0, m_X1, m_Y1)
End Sub
|