"kerpal" <l@m.n> schrieb
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.A llPaintingInWmP aint Or _
ControlStyles.D oubleBuffer Or _
ControlStyles.R esizeRedraw Or _
ControlStyles.U serPaint, _
True)
End Sub
Protected Overrides Sub OnMouseDown( _
ByVal e As System.Windows. Forms.MouseEven tArgs)
If Not m_IsPainting AndAlso e.Button = MouseButtons.Le ft Then
m_StartPos.X = e.X
m_StartPos.Y = e.Y
m_EndPos = m_StartPos
m_IsPainting = True
End If
MyBase.OnMouseD own(e)
End Sub
Protected Overrides Sub OnMouseMove( _
ByVal e As System.Windows. Forms.MouseEven tArgs)
If m_IsPainting Then
If (e.Button And MouseButtons.Le ft) = MouseButtons.Le ft Then
m_EndPos.X = e.X
m_EndPos.Y = e.Y
Else
m_IsPainting = False
End If
Invalidate()
End If
MyBase.OnMouseM ove(e)
End Sub
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows. Forms.PaintEven tArgs)
If m_IsPainting Then
e.Graphics.Draw Line(Pens.White , m_StartPos, m_EndPos)
End If
End Sub
Protected Overrides Sub OnMouseUp( _
ByVal e As System.Windows. Forms.MouseEven tArgs)
If m_IsPainting AndAlso e.Button = MouseButtons.Le ft 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
--
Armin
http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html