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

Last reviewed: June 21, 1995
Article ID: Q71489
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

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.

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 OldXSub As Single, OldY As Single, StartX As Single, 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 As Single, Y1 As Single, X2 As Single, 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


Additional reference words: 1.00 2.00 3.00
KBCategory: kbgraphic kbprg kbcode
KBSubcategory: APrgGrap


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.