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 a polygon's points with grab handles, snapping the points to a grid
Keywordspolygon, snap to, grid, draw, drag
CategoriesGraphics
 
Use mouse event handlers to let the user drag points. The key is the SnapToGrid routine. It snaps a point to the nearest grid point. The program uses this routine to snap the point being dragged to the grid in the MouseMove event handler.
 
Private Const GRID_SPACING = 120

' The grab handle we are dragging. This is 0
' when we are not dragging any handle.
Private m_DraggingHandle As Integer

' The data points.
Private m_NumPoints As Single
Private m_PointX() As Single
Private m_PointY() As Single

Private Const HANDLE_WIDTH = 80
Private Const HANDLE_HALF_WIDTH = HANDLE_WIDTH / 2

' Draw the positioning grid.
Private Sub DrawGrid()
Dim X As Single
Dim Y As Single

    picCanvas.Line (0, 0)-(picCanvas.ScaleWidth, _
        picCanvas.ScaleHeight), _
        picCanvas.BackColor, BF

    Y = 0
    Do While Y <= picCanvas.ScaleHeight
        X = 0
        Do While X <= picCanvas.ScaleWidth
            picCanvas.PSet (X, Y)
            X = X + GRID_SPACING
        Loop
        Y = Y + GRID_SPACING
    Loop

    ' Make the image permanent.
    picCanvas.Picture = picCanvas.Image
End Sub

Private Sub DrawPolygon()
Dim i As Integer

    picCanvas.Cls
    If m_NumPoints < 1 Then Exit Sub

    ' Start at the last point.
    picCanvas.CurrentX = m_PointX(m_NumPoints)
    picCanvas.CurrentY = m_PointY(m_NumPoints)

    ' Connect the points.
    For i = 1 To m_NumPoints
        picCanvas.Line -(m_PointX(i), m_PointY(i))
    Next i

    ' Draw grab handles as white squares with
    ' black edges.
    FillColor = vbWhite
    FillStyle = vbFSSolid
    For i = 1 To m_NumPoints
        picCanvas.Line (m_PointX(i) - HANDLE_HALF_WIDTH, _
            m_PointY(i) - _
            HANDLE_HALF_WIDTH)-Step(HANDLE_WIDTH, _
            HANDLE_WIDTH), vbWhite, BF
        picCanvas.Line (m_PointX(i) - HANDLE_HALF_WIDTH, _
            m_PointY(i) - _
            HANDLE_HALF_WIDTH)-Step(HANDLE_WIDTH, _
            HANDLE_WIDTH), , B
    Next i
End Sub

' Snap the point to the grid.
Private Sub SnapToGrid(ByRef X As Single, ByRef Y As Single)
    X = picCanvas.ScaleLeft + GRID_SPACING * CInt((X - _
        picCanvas.ScaleLeft) / GRID_SPACING)
    Y = picCanvas.ScaleTop + GRID_SPACING * CInt((Y - _
        picCanvas.ScaleTop) / GRID_SPACING)
End Sub

' See if we are over a grab handle.
Private Sub picCanvas_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim i As Integer
Dim dx As Single
Dim dy As Single

    For i = 1 To m_NumPoints
        If Abs(m_PointX(i) - X) < HANDLE_HALF_WIDTH And _
           Abs(m_PointY(i) - Y) < HANDLE_HALF_WIDTH _
        Then
            ' We are over this grab handle.
            ' Start dragging.
            m_DraggingHandle = i
            picCanvas.MousePointer = vbCrosshair
            Exit For
        End If
    Next i
End Sub

' Move the drag handle.
Private Sub picCanvas_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    ' Do nothing if we are not dragging.
    If m_DraggingHandle = 0 Then Exit Sub

    ' Move the handle.
    m_PointX(m_DraggingHandle) = X
    m_PointY(m_DraggingHandle) = Y

    SnapToGrid _
        m_PointX(m_DraggingHandle), _
        m_PointY(m_DraggingHandle)

    ' Redraw.
    DrawPolygon
End Sub

' Stop dragging.
Private Sub picCanvas_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    picCanvas.MousePointer = vbDefault
    m_DraggingHandle = 0
End Sub
 
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated