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
 
 
 
 
 
 
TitleMake a scribble application using classes
Keywordsscribble, draw, classes
CategoriesGraphics
 
This program stores each scribbled curve in a Segment class object with the following definition.
 
Private PtX As Collection
Private PtY As Collection

' Add a point to the points collections.
Public Sub AddPoint(ByVal X As Single, ByVal Y As Single)
Dim num As Integer

    ' If this is the same as the last point,
    ' do nothing.
    num = PtX.Count
    If num > 0 Then
        If (X = PtX(num)) And (Y = PtY(num)) _
            Then Exit Sub
    End If

    ' Add the point.
    PtX.Add X
    PtY.Add Y
End Sub

' Draw on this form.
Public Sub Draw(ByVal frm As Form)
Dim i As Integer

    If PtX.Count < 2 Then Exit Sub

    frm.CurrentX = PtX(1)
    frm.CurrentY = PtY(1)
    For i = 2 To PtX.Count
        frm.Line -(PtX(i), PtY(i))
    Next i
End Sub
 
When you press the mouse down, the MouseDown event handler creates a new Segment object and adds a point to it. When you move the mouse, the MouseMove event hadler adds a point to the segment. The MouseUp event handler sets the NewSegment object to Nothing so future mouse movements don't add points to the segment.
 
Private Segments As Collection

Private NewSegment As Segment

' Start drawing.
Private Sub Form_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    ' Create the new segment.
    Set NewSegment = New Segment

    ' Add the new point.
    NewSegment.AddPoint X, Y

    ' Move to this point.
    CurrentX = X
    CurrentY = Y
End Sub

' Continue drawing.
Private Sub Form_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    ' Do nothing if we are not drawing.
    If NewSegment Is Nothing Then Exit Sub

    ' Add the new point.
    NewSegment.AddPoint X, Y

    ' Draw to this point.
    Line -(X, Y)
End Sub

' Stop drawing.
Private Sub Form_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    Set NewSegment = Nothing
End Sub
 
When the form needs to be redrawn, the Paint event handler runs through the collection containing all of the Segment objects and makes them all draw themselves.
 
' Draw the segments.
Private Sub Form_Paint()
Dim seg As Segment

    Cls
    For Each seg In Segments
        seg.Draw Me
    Next seg
End Sub
 
Because the program stores the Segment objects, it can redraw them when necessary. You could add a lot more functionality to this program. For example, you could let the user click to select an object, rearrange the objects, delete objects selectively, provide drawing attributes such as color and line width, and so forth.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated