Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLet the user draw polygons in Visual Basic .NET
DescriptionThis example shows how to det the user draw polygons in Visual Basic .NET.
Keywordsgraphics, polygons, draw polygons, drawing, Visual Basic .NET, VB.NET
CategoriesGraphics
 

This program represents each polygon as a list of points. It stores all of the finished polygons in a list of lists of points named Polygons.

When you are making a new polygon, the program stores it in a list of points named NewPolygon. Finally the current mouse position, which is where the next point would be if you clicked the mouse, is stored in variable NewPoint.

 
' Each polygon is represented by a List(Of Point).
Private Polygons As New List(Of List(Of Point))()

' Points for the new polygon.
Private NewPolygon As List(Of Point) = Nothing

' The current mouse position while drawing a new polygon.
Private NewPoint As Point
 
The PictureBox's MouseDown event handler starts drawing a new polygon or adds a point to the current one.
 
Private Sub picCanvas_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseDown
    ' See if we are already drawing a polygon.
    If (NewPolygon IsNot Nothing) Then
        ' We are already drawing a polygon.
        ' If it's the right mouse button, finish this
        ' polygon.
        If (e.Button = MouseButtons.Right) Then
            ' Finish this polygon.
            If (NewPolygon.Count > 2) Then _
                Polygons.Add(NewPolygon)
            NewPolygon = Nothing
        Else
            ' Add a point to this polygon.
            If (NewPolygon(NewPolygon.Count - 1) <> _
                e.Location) Then
                NewPolygon.Add(e.Location)
            End If
        End If
    Else
        ' Start a new polygon.
        NewPolygon = New List(Of Point)()
        NewPoint = e.Location
        NewPolygon.Add(e.Location)
    End If

    ' Redraw.
    picCanvas.Invalidate()
End Sub
 
The PictureBox's MouseDown event handler does one of three things:

  • If you are currently drawing a polygon and you pressed the right mouse button, the code finishes the new polygon. If the polygon has more than 2 points, it adds it to the Polygons list.
  • If you are currently drawing a polygon and you pressed the left mouse button, the program adds a new point to the new polygon.
  • If you are not currently drawing a polygon, the code starts a new polygon.

The event handler finishes by invalidating the PictureBox so it redraws.

When you move the mouse, the PictureBox's MouseMove event handler updates the NewPoint position and invalidates the PictureBox to make it redraw.

 
' Move the next point in the new polygon.
Private Sub picCanvas_MouseMove(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseMove
    If (NewPolygon Is Nothing) Then Exit Sub
    NewPoint = e.Location
    picCanvas.Invalidate()
End Sub
 
The PictureBox's Paint event handler draws the old polygons in blue and the new one in green.
 
' Redraw old polygons in blue. Draw the new polygon in
' green.
' Draw the final segment dashed.
Private Sub picCanvas_Paint(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles _
    picCanvas.Paint
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    e.Graphics.Clear(picCanvas.BackColor)

    ' Draw the old polygons.
    For Each polygon As List(Of Point) In Polygons
        e.Graphics.FillPolygon(Brushes.White, _
            polygon.ToArray())
        e.Graphics.DrawPolygon(Pens.Blue, polygon.ToArray())
    Next polygon

    ' Draw the new polygon.
    If (NewPolygon IsNot Nothing) Then
        ' Draw the new polygon.
        If (NewPolygon.Count > 1) Then
            e.Graphics.DrawLines(Pens.Green, _
                NewPolygon.ToArray())
        End If

        ' Draw the newest edge.
        If (NewPolygon.Count > 0) Then
            Using dashed_pen As New Pen(Color.Green)
                dashed_pen.DashPattern = New Single() {3, 3}
                e.Graphics.DrawLine(dashed_pen, _
                    NewPolygon(NewPolygon.Count - 1), _
                    NewPoint)
            End Using
        End If
    End If
End Sub
 
First the event handler loops through the Polygons list, filling and drawing each of the polygons. Then if you are currently drawing a new polygon, the code uses the DrawLines method to draw the polygon so far. Finally the code draws a line between the new polygon's last point and the current mouse position to show where the next point would be if you clicked the mouse there.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated