|
|
Title | Draw simple objects (rectangle, circle, etc.) and let the user click them |
Keywords | drawing, object, rectangle, circle |
Categories | Graphics, Software Engineering |
|
|
Use a different class for each type of object. Store them in a Collection.
Each class should provide a Draw method. The following code shows how the program draws the objects.
|
|
' Redraw all objects.
Private Sub Redraw()
Dim obj As Object
picCanvas.Cls
For Each obj In m_DrawingObjects
obj.Draw
Next obj
End Sub
|
|
Each object should also provide an IsAt function that returns True if the object is at a specific point. How IsAt works depends on the type of shape. The following code shows how the program finds the object the user clicks.
|
|
Private Sub picCanvas_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
' See what we are doing.
Select Case m_State
Case state_None ' Not drawing.
' Select the object at (X, Y).
SelectObject ObjectAt(X, Y)
Case Else
' If this is the left button,
' start drawing the new object.
' Set drawing parameters for drawing
' the new object.
With picCanvas
.DrawMode = vbInvert
.FillStyle = vbFSTransparent
.DrawWidth = 1
End With
m_Drawing = True
m_FirstX = X
m_FirstY = Y
m_LastX = X
m_LastY = Y
End Select
End Sub
' Find the object at this position.
Private Function ObjectAt(ByVal X As Single, ByVal Y As _
Single) As Object
Dim i As Integer
' See if an object is here. Look from top
' to bottom.
For i = m_DrawingObjects.Count To 1 Step -1
If m_DrawingObjects(i).IsAt(X, Y) Then Exit For
Next i
If i > 0 Then
Set ObjectAt = m_DrawingObjects(i)
Else
Set ObjectAt = Nothing
End If
End Function
|
|
Other routines manipulate the selected object (for example, delete it or change its position in the stacking order), draw objects, etc. See the code for details.
|
|
|
|
|
|