VB.NET lines

  • Thread starter Thread starter Capuchin
  • Start date Start date
C

Capuchin

Is it possible to write code to draw lines on a form? if so...how?
thanks in advance for any help with this
Capuchin
 
Add the following code to a functino inside a form:
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)

g.DrawLine(Pens.Black, 0, 0, 100, 100)

g.Dispose()

If your code is inside the Paint event, the graphics object is already
available and you don't have to create it.

-Rob Teixeira [MVP]
 
Rob,
Rather then:
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)

I would recommend:
Dim g As Graphics = Me.CreateGraphics()

As its "cleaner".

However as you stated, putting the code in the Paint event is generally
better, and the Graphics object is supplied.

Just a thought
Jay
 
Ha! That's what I was thinking of :-)

Been wading waste-deep in security code for so long, i'm losing the "fun"
stuff.

-Rob Teixeira [MVP]
 
* "Capuchin said:
Is it possible to write code to draw lines on a form? if so...how?
thanks in advance for any help with this

\\\
Private m_ptStartPosition As Point
Private m_ptPosition As Point
Private m_blnMoving As Boolean

Private Sub Form1_MouseDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs _
) Handles MyBase.MouseDown
m_blnMoving = True
m_ptStartPosition = New Point(e.X, e.Y)
End Sub

Private Sub Form1_MouseUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs _
) Handles MyBase.MouseUp
m_blnMoving = False
End Sub

Private Sub Form1_MouseMove( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs _
) Handles MyBase.MouseMove
m_ptPosition = New Point(e.X, e.Y)
Me.Invalidate()
End Sub

Private Sub Form1_Paint( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs _
) Handles MyBase.Paint
e.Graphics.DrawLine(Pens.Blue, m_ptStartPosition, m_ptPosition)
End Sub
///
 
I feel it should also be stated that if you use the graphics object
provided in the paint event, then do NOT dispose of it yourself.
 
Back
Top