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
|