How to Create Rubber-Band Lines/Boxes in Visual Basic

ID Number: Q71488

1.00

WINDOWS

Summary:

Creating rubber bands within Visual Basic can be done using the

DrawMode property. Rubber bands are lines that stretch as you move the

mouse cursor from a specified point to a new location. This can be

very useful in graphics programs and when defining sections of the

screen for clipping routines.

This information applies to Microsoft Visual Basic Programming System

version 1.00 for Windows.

More Information:

The theory of drawing a rubber-band box is as follows:

1. Draw a line from the initial point to the location of the mouse

cursor using:

[form].DrawMode = 6. {INVERT}

2. Move the mouse cursor.

3. Save the DrawMode.

4. Set the [form].DrawMode to 6. {INVERT}

5. Draw the same line that was drawn in step 1. This will restore the

image underneath the line.

6. Set the [form].DrawMode back to the initial DrawMode saved in step

3.

7. Repeat the cycle again.

DrawMode equal to INVERT allows the line to be created using the

inverse of the background color. This allows the line to be always

displayed on all colors.

The sample below will demonstrate the rubber-band line and the

rubber-band box. Clicking on the command buttons will allow the user

to select between rubber-band line or a rubber-band box. The user will

also be able to select a solid line or a dashed line.

Create and set the following controls and properties:

CtlName Caption Picture

------- ------- -------

Form1 Form1 c:\windows\chess.bmp

Command1 RubberBand

Command2 RubberBox

Command3 Dotted

Command4 Solid

In the general section of your code, define the following constants:

Const INVERSE = 6 '*Characteristic of DrawMode property(XOR).

Const SOLID = 0 '*Characteristic of DrawStyle property.

Const DOT = 2 '*Characteristic of DrawStyle property.

Const TRUE = -1

Const FALSE = 0

Dim DrawBox As Integer '*Boolean-whether drawing Box or Line

Dim OldX, OldY, StartX, StartY As Single '* Mouse locations

In the appropriate procedures, add the following code:

Sub Form_MouseDown (Button As Integer, Shift As Integer, X As

Single, Y As Single)

'* Store the initial start of the line to draw.

StartX = X

StartY = Y

'* Make the last location equal the starting location

OldX = StartX

OldY = StartY

End Sub

Sub Form_MouseMove (Button As Integer, Shift As Integer, X As

Single, Y As Single)

'* If the button is depressed then...

If Button Then

'* Erase the previous line.

Call DrawLine(StartX, StartY, OldX, OldY)

'* Draw the new line.

Call DrawLine(StartX, StartY, X, Y)

'* Save the coordinates for the next call.

OldX = X

OldY = Y

End If

End Sub

Sub DrawLine (X1, Y1, X2, Y2 As Single)

'* Save the current mode so that you can reset it on

'* exit from this sub routine. Not needed in the sample

'* but would need it if you are not sure what the

'* DrawMode was on entry to this procedure.

SavedMode% = DrawMode

'* Set to XOR

DrawMode = INVERSE

'*Draw a box or line

If DrawBox Then

Line (X1, Y1)-(X2, Y2), , B

Else

Line (X1, Y1)-(X2, Y2)

End If

'* Reset the DrawMode

DrawMode = SavedMode%

End Sub

Sub Form_MouseUp (Button As Integer, Shift As Integer, X As Single,

Y As Single)

'* Stop drawing lines/boxes.

StartEvent = FALSE

End Sub

Sub Command2_Click ()

'* Boolean value to determine whether to draw a line or box.

DrawBox = TRUE

End Sub

Sub Command1_Click ()

'* Boolean value to determine whether to draw a line or box.

DrawBox = FALSE

End Sub

Sub Command3_Click ()

'* Create a dotted line

Form1.DrawStyle = DOT

End Sub

Sub Command4_Click ()

'* Create a solid line.

Form1.DrawStyle = SOLID

End Sub