Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLet the user draw a Bezier curve in VB .NET
DescriptionThis example shows how to let the user draw a Bezier curve in VB .NET. When the user clicks four points, the program draws the Bezier curve defined by them.
KeywordsBezier, Bezier curve, curve, VB.NET
CategoriesVB.NET, Graphics
 
If the program already had four points, the program's MouseDown event handler clears the previous Bezier curve. The event handler then saves the new point and redraws the data.

Subroutine Redraw draws the data. If the program has saved four points, the routine uses he Graphics object's DrawBezier method to draw the curve defined by the points. Then it draws boxes around the points it has so you can see them.

 
Private m_Points(3) As Point
Private m_MaxPoint As Integer = -1

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    MyBase.MouseDown
    If m_MaxPoint = 3 Then
        Me.CreateGraphics.Clear(Me.BackColor)
        m_MaxPoint = 0
    Else
        m_MaxPoint += 1
    End If

    m_Points(m_MaxPoint).X = e.X
    m_Points(m_MaxPoint).Y = e.Y

    Redraw(Me.CreateGraphics)
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    Redraw(e.Graphics)
End Sub

Private Sub Redraw(ByVal gr As Graphics)
    If m_MaxPoint = 3 Then
        ' Draw the Bezier curve.
        gr.DrawBezier(Pens.Black, _
            m_Points(0), m_Points(1), _
            m_Points(2), m_Points(3))
    End If

    ' Draw the control points.
    For i As Integer = 0 To m_MaxPoint
        Dim rect As New Rectangle( _
            m_Points(i).X - 2, m_Points(i).Y - 2, 5, 5)
        gr.FillRectangle(Brushes.White, rect)
        gr.DrawRectangle(Pens.Black, rect)
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated