draw a line as in MSPaint

  • Thread starter Thread starter kerpal
  • Start date Start date
K

kerpal

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.
 
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
 
Hi Armin,

Is this original by you?
(I add that when I give this code to some else you know)
(waiting if you would do that of course or in a newsgroup where you are not
active)

And thanks for that hind about chrismass.

I say merry chrismas to you on a later time of the day.

:-)

Cor
 
Cor said:
Hi Armin,

Is this original by you?
yes

(I add that when I give this code to some else you know)

Why only to someone else I know? *g*
(waiting if you would do that of course or in a newsgroup where you
are not active)

yes yes, of course
And thanks for that hind about chrismass.

I say merry chrismas to you on a later time of the day.

:-)

Yes, merry ChrisTmas!

:-)))
 
Hi Armin,

Lol about all lines.
Yes, merry ChrisTmas!

Now you can see I do not use a spellchecker.
Nock nock on my head.
I know your normal answer; there is no need to send it.

:-)))

Cor
 
Back
Top