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 line "symbiote" to help users draw rubberband lines
DescriptionThis example shows how to make a line "symbiote" to help users draw rubberband lines in Visual Basic 6.
Keywordsline symbiote, rubberband line, draw
CategoriesGraphics
 
This example uses a control that I call a symbiote. It works in conjunction with a PictureBox to let the user draw a rubberband line.

The control is represented by a small icon and is invisible at run time, much as a Timer is. Its InvisibleAtRuntime property is set to True and at control design time it was given a background image. The Resize event handler ensures that the control always fits this image.

 
Private Sub UserControl_Resize()
    UserControl.Width = UserControl.ScaleX(32, vbPixels, _
        vbTwips)
    UserControl.Height = UserControl.ScaleY(32, vbPixels, _
        vbTwips)
End Sub
 
The Canvas property gives the control on which the symbiote draws. Its m_Canvas variable is declared WithEvents so the control can receive its events easily. The main program should set this property to a control when it wants to let the user select a rubberband line and it should set this to Nothing to prevent the user from selecting a line.
 
' The PictureBox on which we will draw.
Private WithEvents m_Canvas As PictureBox

' Return the PictureBox on which we will draw.
Public Property Get Canvas() As PictureBox
    Set Canvas = m_Canvas
End Property

' Set the PictureBox on which we will draw.
Public Property Set Canvas(ByVal New_Canvas As PictureBox)
    Set m_Canvas = New_Canvas
End Property
 
The MouseDown, MouseMove, and MouseUp event handlers draw the line. When the user releases the mouse, the control raises its LineSelected event.
 
Public Event LineSelected(ByVal x0 As Single, ByVal y0 As _
    Single, ByVal x1 As Single, ByVal y1 As Single)

' Start rubberband drawing.
Private Sub m_Canvas_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Button <> vbLeftButton Then Exit Sub
    m_Drawing = True

    m_X0 = X
    m_Y0 = Y
    m_X1 = X
    m_Y1 = Y

    m_DrawMode = m_Canvas.DrawMode
    m_Canvas.DrawMode = vbInvert
    m_Canvas.Line (m_X0, m_Y0)-(m_X1, m_Y1)
End Sub

' Continue drawing.
Private Sub m_Canvas_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not m_Drawing Then Exit Sub

    m_Canvas.Line (m_X0, m_Y0)-(m_X1, m_Y1)
    m_X1 = X
    m_Y1 = Y
    m_Canvas.Line (m_X0, m_Y0)-(m_X1, m_Y1)
End Sub

' Finish drawing.
Private Sub m_Canvas_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not m_Drawing Then Exit Sub
    m_Drawing = False

    m_Canvas.Line (m_X0, m_Y0)-(m_X1, m_Y1)

    m_Canvas.DrawMode = m_DrawMode

    RaiseEvent LineSelected(m_X0, m_Y0, m_X1, m_Y1)
End Sub
 
For more information on building ActiveX controls in VB 5/6, and for the source code for 101 pre-defined controls, see my book Custom Controls Library.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated