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 move and modify multiple polygons with grab handles
Keywordspolygons, drag, grab handles, object-oriented design, classes, objects
CategoriesGraphics
 
Each polygon is represented by a DraggablePolygon object. When the drawing PictureBox receives a MouseDown event, it calls each DraggablePolygon's IsOverVertex function to see if the point is over a vertex. If it is, the event handler makes the polygon move its vertex. More on that in a moment.

If the point is not over any vertex, the event handler calls each DraggablePolygon's IsOverEdge function to see if the point is over a polygon edge. If it is, the event handler makes the polygon move itself.


The DraggablePolygon's DragVertex routine is shown in the following code. It sets the variable m_DragVertexCanvas to the PictureBox on which the polygon lies. That variable is declared WithEvents so the DraggablePolygon object receives the PictureBox's events.

When the DraggablePolygon receives the m_DragVertexCanvas_MouseMove event, the user has moved the mouse over the canvas. The event handler updates the position of the vertex being dragged and redraws the polygon. All drawing during the drag is in invert mode so the program can erase each temporary polygon.

When the DraggablePolygon receives the m_DragVertexCanvas_MouseUp event, the user has released the mouse. The object sets the DrawMode for m_DragVertexCanvas back to its normal setting and then sets m_DragVertexCanvas to Nothing so it no longer receives any PictureBox events. It raises the VertexMoved event so the main program can take action. In particular, the main program redraws all of the polygons with the vertex moved.

 
' Used to get events while dragging a vertex.
Private WithEvents m_DragVertexCanvas As PictureBox

' Draw the selected vertex.
Public Sub DragVertex(ByVal canvas As PictureBox, ByVal X _
    As Long, ByVal Y As Long)
    ' Set the vertex drag PictureBox so we can
    ' catch its mouse events.
    Set m_DragVertexCanvas = canvas
    m_DragVertexCanvas.DrawMode = vbInvert

    ' Redraw in invert mode.
    Draw m_DragVertexCanvas
End Sub

' Continue dragging the vertex.
Private Sub m_DragVertexCanvas_MouseMove(Button As Integer, _
    Shift As Integer, X As Single, Y As Single)
    ' Erase the previous invert mode image.
    Draw m_DragVertexCanvas

    ' Move the handle.
    With m_Vertices(m_OverPoint)
        .X = X
        .Y = Y
    End With

    ' Redraw.
    Draw m_DragVertexCanvas
End Sub

' Stop dragging the vertex.
Private Sub m_DragVertexCanvas_MouseUp(Button As Integer, _
    Shift As Integer, X As Single, Y As Single)
    m_DragVertexCanvas.DrawMode = vbCopyPen
    Set m_DragVertexCanvas = Nothing

    RaiseEvent VertexMoved
End Sub
 
Moving a polygon is similar. The main program calls the polygon's DragPolygon method to start the drag. That routine saves a reference to the drawing surface in a variable declared WithEvents. It receives mouse events and moves the polygon accordingly. When the user releases the mouse, the object raises the PolygonMoved event.

See the code for further details.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated