|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
For Each obj In m_DrawingObjects
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)
' If this is the left button,
' start drawing the new object.
' Set drawing parameters for drawing
' the new object.
.DrawMode = vbInvert
.FillStyle = vbFSTransparent
.DrawWidth = 1
m_Drawing = True
m_FirstX = X
m_FirstY = Y
m_LastX = X
m_LastY = Y
' 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
If i > 0 Then
Set ObjectAt = m_DrawingObjects(i)
Set ObjectAt = Nothing
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.