Drawing a Rubber Band line in Visual Basic (VB6)

We have an example project for VB 6 that shows how to use the csXImage ActiveX control to draw a line at the position of the mouse cursor. It uses the technique known as "Rubber Banding" where the line is drawn temporarily and can be moved with the mouse until the button is released. Download the project code.

The demo uses the trial version of csXImage, which must must be registered on the development computer and the licence file must be in the same folder as the .ocx control. Running our installer will have performed the registration and unpacking of files.

Some global variables are used to record the start and end of the line and Boolean variables are needed to indicate whether the line is being drawn as the mouse moves.

Private DrawLineStart As Boolean
Private DrawingLine As Boolean

Private X1 As Integer
Private Y1 As Integer
Private X2 As Integer
Private Y2 As Integer

When the form is loaded a blank image is drawn in the control to use in the demo, and the PenMode property is set to pmNotXor. This setting for the pen is what allows the line to be drawn and rubbed out again because it draws a line by inverting the pixels, rather than drawing a line of a specific colour.

Private Sub Form_Load()

ImageBox1.NewImage 360, 280

' This PenMode means that a line can be erased by re-drawing on top of it
ImageBox1.PenMode = pmNotXor

End Sub

The button that starts the line drawing process sets the DrawLineStart Boolean variable to true. The button is then disabled until the line is finished.

Private Sub cmdDrawLine_Click()

' This flag indicates to the OnMouseDown event that drawing will start
DrawLineStart = True

' Disable this button
cmdDrawLine.Enabled = False

End Sub

The OnMouseDown event will be triggered regardless of whether the Draw Line button has been clicked so the value of the DrawLineStart variable must be checked first. If a line is being drawn the coordinates are recorded. OnMouseDown provides the mouse coordinates as parameters. The DrawingLine Boolean variable is set to be true for use by the OnMouseMove event.

Private Sub ImageBox1_OnMouseDown(ByVal Button As Integer, ByVal ShiftState As Integer, ByVal X As Long, ByVal Y As Long)

If DrawLineStart Then

  ' Record the start coordinates of the line
  X1 = X
  Y1 = Y
  X2 = X
  Y2 = Y

  ' Reset the start flag
  DrawLineStart = False

  ' This flag indicates to the OnMouseMove event that drawing is in progress.
  DrawingLine = True

End If

End Sub

OnMouseMove will be triggered whenever the mouse moves but action only needs to be taken when a line is being drawn. That is the purpose of the Boolean variables DrawingLine and DrawLineStart. When drawing is in progress the mouse cursor is changed to a cross hair.

When DrawLine is true the old line must be erased by drawing over it using the pmNotXor setting for PenMode. The start and previous end point coordinates have been stored previously. After deleting the old line the new end point coordinates are set and a new line is drawn.

Private Sub ImageBox1_OnMouseMove(ByVal ShiftState As Integer, ByVal X As Long, ByVal Y As Long)

If DrawingLine Or DrawLineStart Then

  ' Use the crosshair cursor when drawing
  Screen.MousePointer = vbCrosshair

End If

If DrawingLine Then

  ' Erase the previous line
  ' (note the PenMode setting in Form_Load)
  ImageBox1.DrawLine X1, Y1, X2, Y2

  ' Set the new line end coordinates and draw a new line
  X2 = X
  Y2 = Y
  ImageBox1.DrawLine X1, Y1, X2, Y2

End If

End Sub

Finally, the drawing is stopped when the mouse button is released. The cursor is changed back to the default and the Draw Line button is enabled.

Private Sub ImageBox1_OnMouseUp(ByVal Button As Integer, ByVal ShiftState As Integer, ByVal X As Long, ByVal Y As Long)

' Stop drawing & re-enable the Draw button
DrawingLine = False
cmdDrawLine.Enabled = True
Screen.MousePointer = vbDefault

End Sub


This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.