kerpal said:
hi all,
I'm trying to draw lines from an origin to the coordinate of the
mouse. As the mouse moves, the line will follow the mouse's movement
and previous lines are then erased.
any suggestions on how to achieve this efficiently? Thanks in
advance.
Not 1:1 what you need, but you can change it to fit your needs:
Private Class PaintControl
Inherits Control
Private m_IsPainting As Boolean
Private m_StartPos, m_EndPos As Point
Public Sub New()
setstyle( _
ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.DoubleBuffer Or _
ControlStyles.ResizeRedraw Or _
ControlStyles.UserPaint, _
True)
End Sub
Protected Overrides Sub OnMouseDown( _
ByVal e As System.Windows.Forms.MouseEventArgs)
If Not m_IsPainting AndAlso e.Button = MouseButtons.Left Then
m_StartPos.X = e.X
m_StartPos.Y = e.Y
m_EndPos = m_StartPos
m_IsPainting = True
End If
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseMove( _
ByVal e As System.Windows.Forms.MouseEventArgs)
If m_IsPainting Then
If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
m_EndPos.X = e.X
m_EndPos.Y = e.Y
Else
m_IsPainting = False
End If
Invalidate()
End If
MyBase.OnMouseMove(e)
End Sub
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
If m_IsPainting Then
e.Graphics.DrawLine(Pens.White, m_StartPos, m_EndPos)
End If
End Sub
Protected Overrides Sub OnMouseUp( _
ByVal e As System.Windows.Forms.MouseEventArgs)
If m_IsPainting AndAlso e.Button = MouseButtons.Left Then
m_IsPainting = False
Invalidate()
End If
End Sub
Public ReadOnly Property IsPainting() As Boolean
Get
Return m_IsPainting
End Get
End Property
End Class