| 
            
           | 
          
            | 
          
          
          
          
            
              
              
                
                  | Title | Let the user modify a polygon with grab handles in VB .NET |  
                  | Keywords | polygon, grab handles, draw, drag |  
                  | Categories | Graphics, VB.NET |  
                 
               |  
              
               
  |  
              | 
Keep the list of polygon vertices in arrays. In the MouseDown event handler, search the vertices to see if the point is close to a vertex. If it is, start dragging.
               |  
              
               
  |  
              
                
                  ' The grab handle we are dragging. This is 
' -1 when we are not dragging any handle.
Private m_DraggingHandle As Integer = -1
' The data points.
Private m_Points() As Point
Private Const HANDLE_WIDTH As Integer = 6
Private Const HANDLE_HALF_WIDTH As Integer = HANDLE_WIDTH \ _
    2
' Create some initial point data.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Load
    ' Make room.
    ReDim m_Points(4)
    ' Set initial points.
    m_Points(0).X = 67 : m_Points(0).Y = 189
    m_Points(1).X = 62 : m_Points(1).Y = 111
    m_Points(2).X = 123 : m_Points(2).Y = 100
    m_Points(3).X = 190 : m_Points(3).Y = 183
    m_Points(4).X = 130 : m_Points(4).Y = 214
End Sub
' Draw the polygon.
Private Sub DrawPolygon(ByVal gr As Graphics)
    gr.Clear(picCanvas.BackColor)
    gr.DrawPolygon(Pens.Black, m_Points)
    ' Draw grab handles as white squares with
    ' black edges.
    Dim rectangles() As Rectangle
    ReDim rectangles(m_Points.GetUpperBound(0))
    For i As Integer = 0 To m_Points.GetUpperBound(0)
        With rectangles(i)
            .X = m_Points(i).X - HANDLE_HALF_WIDTH
            .Y = m_Points(i).Y - HANDLE_HALF_WIDTH
            .Width = HANDLE_WIDTH
            .Height = HANDLE_WIDTH
        End With
    Next i
    gr.FillRectangles(Brushes.White, rectangles)
    gr.DrawRectangles(Pens.Black, rectangles)
End Sub
Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.PaintEventArgs) Handles _
    picCanvas.Paint
    DrawPolygon(e.Graphics)
End Sub
' See if we are over a grab handle.
Private Sub picCanvas_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseDown
    Dim dx As Single
    Dim dy As Single
    For i As Integer = 0 To m_Points.GetUpperBound(0)
        If Abs(m_Points(i).X - e.X) < HANDLE_HALF_WIDTH And _
            _
           Abs(m_Points(i).Y - e.Y) < HANDLE_HALF_WIDTH _
        Then
            ' We are over this grab handle.
            ' Start dragging.
            m_DraggingHandle = i
            picCanvas.Cursor = Cursors.Cross
            Exit For
        End If
    Next i
End Sub
' Move the drag handle.
Private Sub picCanvas_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseMove
    ' Do nothing if we are not dragging.
    If m_DraggingHandle = -1 Then Exit Sub
    ' Move the handle.
    m_Points(m_DraggingHandle).X = e.X
    m_Points(m_DraggingHandle).Y = e.Y
    ' Redraw.
    DrawPolygon(picCanvas.CreateGraphics())
End Sub
' Stop dragging.
Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseUp
    picCanvas.Cursor = Cursors.Default
    m_DraggingHandle = -1
End Sub
               |  
              
               
  |  
              | 
For more information on graphics programming in Visual Basic 6, see my book Visual Basic Graphics Programming.
               |  
              
              
              
              |   |  
               
 |  
              |   |  
              
           |  
          
          
          
          
             
           |