How to Create Flashing/Rotating Rubber-Band Box in VB

ID Number: Q71489

1.00

WINDOWS

Summary:

Several programs, such as Excel, create a flashing border (which

appears to rotate) when selecting items of the windows when using the

Edit Copy selection of the menu system. You can create a flashing,

rotating border with the DrawMode and DrawStyle properties of a Visual

Basic form.

This information applies to Microsoft Visual Basic Programming System

version 1.00 for Windows.

More Information:

By drawing a dashed line on the form and then within a timer event

creating a solid line on the dashed line with DrawMode set to INVERSE,

you can create a special effect of a flashing border that appears to

rotate.

You can draw a rotating rubber-band box as follows:

1. Draw a line using:

DrawStyle = 2 {Dot}

2. Save the [form].DrawMode and the [form].DrawStyle.

3. Set the [form].DrawMode = 6 {Inverse}.

4. Set [form].DrawStyle = 0 {Solid}.

5. Draw the same line as in step 1.

6. Reset the properties saved in step 2.

7. Delay some time interval.

8. Repeat starting at step 2.

The following code demonstrates the rotating (flashing) border.

Pressing the mouse button and then dragging the cursor some distance

will create a dotted line. Releasing the button will display a

rotating rubber-band box.

In VB.EXE, create a form called Form1. On Form1, create a timer

control with the name Timer1 and with an interval of 100.

Duplicate the following code within the general declaration section of

your code window:

Const INVERSE = 6 'Characteristic of DrawStyle property(Inverse).

Const SOLID = 0 'Characteristic of DrawMode property.

Const DOT = 2 'Characteristic of DrawMode property.

Const TRUE = -1

Const FALSE = 0

Dim OldX, OldY, StartX, StartY As Single

Add the following code in the appropriate event procedures for Form1:

Sub Form_Load ()

'* Must draw a dotted line to create effect. Load a bitmap. Not

required but shows full extent of line drawing.

DrawStyle = DOT

End Sub

Sub Timer1_Timer ()

SavedDrawStyle% = DrawStyle

'* Solid is need to create the inverse of the dashed line.

DrawStyle = SOLID

'* Invert the dashed line.

Call DrawLine(StartX, StartY, OldX, OldY)

'* Restore the DrawStyle back to what it was previously.

DrawStyle = SavedDrawStyle%

End Sub

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

Single, Y As Single)

' The above Sub statement must be on just one line.

'* Don't add effect as you draw box.

Timer1.Enabled = FALSE

'* Save the start locations.

StartX = X

StartY = Y

'* Set the last coord. to start locations.

OldX = StartX

OldY = StartY

End Sub

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

Single, Y As Single)

' (The above Sub statement must be on just one line.)

'* If button is depress then...

If Button Then

'* Restore previous lines background.

Call DrawLine(StartX, StartY, OldX, OldY)

'* Draw new line.

Call DrawLine(StartX, StartY, X, Y)

'* Save coordinates for 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

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

'* Reset the DrawMode

DrawMode = SavedMode%

End Sub

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

Y As Single)

' (The above Sub statement must be on just one line.)

StartEvent = FALSE

Timer1.Enabled = TRUE

End Sub