Use mouse to draw a polygon : Polygon « 2D Graphics « VB.Net Tutorial

Use mouse to draw a polygon
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Collections

public class DrawPolygon
   public Shared Sub Main
        Application.Run(New FrmPolygon)
   End Sub
End class

Public Class FrmPolygon
   Inherits System.Windows.Forms.Form

   ' polygon type options
   Friend WithEvents filledPolygonRadio As RadioButton
   Friend WithEvents lineRadio As RadioButton
   Friend WithEvents polygonRadio As RadioButton

   ' command buttons
   Friend WithEvents cmdClear As Button
   Friend WithEvents cmdNewColor As Button

   Friend WithEvents drawWindow As Panel

   Friend WithEvents typeGroup As GroupBox

#Region " Windows Form Designer generated code "

   Public Sub New()

      'This call is required by the Windows Form Designer.

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is Nothing) Then
         End If
      End If
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.Container

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.typeGroup = New System.Windows.Forms.GroupBox()
      Me.filledPolygonRadio = New System.Windows.Forms.RadioButton()
      Me.lineRadio = New System.Windows.Forms.RadioButton()
      Me.polygonRadio = New System.Windows.Forms.RadioButton()
      Me.drawWindow = New System.Windows.Forms.Panel()
      Me.cmdClear = New System.Windows.Forms.Button()
      Me.cmdNewColor = New System.Windows.Forms.Button()
      Me.typeGroup.Controls.AddRange(New System.Windows.Forms.Control() {Me.filledPolygonRadio, Me.lineRadio, Me.polygonRadio})
      Me.typeGroup.Location = New System.Drawing.Point(232, 8)
      Me.typeGroup.Name = "typeGroup"
      Me.typeGroup.Size = New System.Drawing.Size(128, 104)
      Me.typeGroup.TabIndex = 0
      Me.typeGroup.TabStop = False
      Me.typeGroup.Text = "Select Type"
      Me.filledPolygonRadio.Location = New System.Drawing.Point(8, 80)
      Me.filledPolygonRadio.Name = "filledPolygonRadio"
      Me.filledPolygonRadio.Size = New System.Drawing.Size(112, 16)
      Me.filledPolygonRadio.TabIndex = 2
      Me.filledPolygonRadio.Text = "Filled Polygon"
      Me.lineRadio.Location = New System.Drawing.Point(8, 24)
      Me.lineRadio.Name = "lineRadio"
      Me.lineRadio.Size = New System.Drawing.Size(112, 16)
      Me.lineRadio.TabIndex = 1
      Me.lineRadio.Text = "Lines"
      Me.polygonRadio.Location = New System.Drawing.Point(8, 48)
      Me.polygonRadio.Name = "polygonRadio"
      Me.polygonRadio.Size = New System.Drawing.Size(112, 24)
      Me.polygonRadio.TabIndex = 0
      Me.polygonRadio.Text = "Polygon"
      Me.drawWindow.BackColor = System.Drawing.SystemColors.Window
      Me.drawWindow.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
      Me.drawWindow.Name = "drawWindow"
      Me.drawWindow.Size = New System.Drawing.Size(232, 264)
      Me.drawWindow.TabIndex = 1
      Me.cmdClear.Location = New System.Drawing.Point(240, 160)
      Me.cmdClear.Name = "cmdClear"
      Me.cmdClear.Size = New System.Drawing.Size(112, 24)
      Me.cmdClear.TabIndex = 2
      Me.cmdClear.Text = "Clear"
      Me.cmdNewColor.Location = New System.Drawing.Point(240, 192)
      Me.cmdNewColor.Name = "cmdNewColor"
      Me.cmdNewColor.Size = New System.Drawing.Size(112, 24)
      Me.cmdNewColor.TabIndex = 3
      Me.cmdNewColor.Text = "Change Color"
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(360, 269)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdNewColor, Me.cmdClear, Me.drawWindow, Me.typeGroup})
      Me.Name = "FrmPolygon"
      Me.Text = "Drawing Polygons"

   End Sub

#End Region

   Private mPoints As ArrayList = New ArrayList()

   Dim mPen As Pen = New Pen(Color.DarkBlue)
   Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue)

   Private Sub drawWindow_MouseDown(ByVal sender _
      As Object, ByVal e As _
      System.Windows.Forms.MouseEventArgs) _
      Handles drawWindow.MouseDown

      mPoints.Add(New Point(e.X, e.Y))
      drawWindow.Invalidate() ' refresh panel
   End Sub ' drawWindow_MouseDown

   Private Sub drawWindow_Paint(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.PaintEventArgs) _
      Handles drawWindow.Paint

      Dim graphicsObject As Graphics = e.Graphics

      If mPoints.Count < 1 Then
      End If   
      Dim pointArray() As Point = mPoints.ToArray(mPoints(0).GetType())

      If polygonRadio.Checked Then 
         graphicsObject.DrawPolygon(mPen, pointArray)

      ElseIf lineRadio.Checked Then
         graphicsObject.DrawLines(mPen, pointArray)

      ElseIf filledPolygonRadio.Checked Then 
         graphicsObject.FillPolygon(mBrush, pointArray)
      End If

   End Sub 

   Private Sub cmdClear_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles cmdClear.Click

   End Sub 
   Private Sub polygonRadio_CheckedChanged(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles polygonRadio.CheckedChanged

   End Sub 

   Private Sub lineRadio_CheckedChanged(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles lineRadio.CheckedChanged

   End Sub 
   Private Sub filledPolygonRadio_CheckedChanged(ByVal sender _
      As System.Object, ByVal e As System.EventArgs) _
      Handles filledPolygonRadio.CheckedChanged

   End Sub 

   Private Sub cmdNewColor_Click(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles cmdNewColor.Click

      Dim colorBox As ColorDialog = New ColorDialog()

      Dim result As DialogResult = colorBox.ShowDialog()

      If result = DialogResult.Cancel Then
      End If

      mPen.Color = colorBox.Color
      mBrush.Color = colorBox.Color 
   End Sub 

End Class

17.6.1.Draw a PolygonDraw a Polygon
17.6.2.Use mouse to draw a polygonUse mouse to draw a polygon