MouseMove Event

See Also053LPT                 Example19GOGG2>Low

Applies To

Form14TJ2LN (not MDI form), check box9P3BU5, command buttonXJSPC0, data control2E1FEX3, directory list boxO9U5A0, file list box1M6S8UX, frame1KX6ZP8, grid2VGT0PT, image9A4FCA, label3MNIZ8D, list boxG11UCK, OLE control2HQDVVU, option buttonJYBO08, picture box31MYIWX, text boxYPYZDG.


Occurs when the user moves the mouse.


Sub Form_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single)

Sub ctlname_MouseMove ([Index As Integer,] Button As Integer, Shift As Integer, X As Single, Y As Single)


The MouseMove event uses these arguments:

Argument        Description


Index                Uniquely identifies a control if it is in a control array8G7Y0UU.

Button              The state of the mouse buttons, in which a bitDEFBIT is set if the button is down.  The Button argument is a bit field3JH793R, with bits corresponding to the left button (bit 0), right button (bit 1), and middle button (bit 2)values 1, 2, and 4, respectively.  It indicates the complete state of the mouse buttons; some, all, or none of these three bits can be set, indicating that some, all, or none of the buttons is pressed.

Shift                 The state of the Shift, Ctrl and Alt keys.  Bit is set if the key is down.  The Shift argument is a bit field, with the least-significant bits corresponding to the Shift key (bit 0), the Ctrl key (bit 1), and the Alt key (bit 2 ).  These bits correspond to the values 1, 2, and 4, respectively.  Shift indicates the state of these keys.  Some, all, or none of the bits can be set, indicating that some, all, or none of the keys is pressed.  For example, if both Ctrl and Alt were pressed, the value of Shift would be 6.

X ,Y                  The current location of the mouse pointer.  X and Y are always expressed in terms of the coordinate system set by the ScaleHeight3J8U7ZN, Scale Width3J8U7ZN, ScaleLeft138Z3ME, and ScaleTop138Z3ME properties of the object.


The MouseMove event is generated continually as the mouse pointer moves across objects.  Unless another object has captured the mouse, an object recognizes a MouseMove event whenever the mouse position is within its borders.

If you need to test for the Button or Shift arguments, you can declare constantsTD9WSV that define the bits within the argument by loading the CONSTANT.TXT file into a module.  These mouse button constants have the following values:

Constant               Value


LEFT_BUTTON        1



SHIFT_MASK           1

CTRL_MASK            2

ALT_MASK               4


These are in CONSTANT.TXT.  The constants act as bit masks17EJ9RH you can use to test for any combination of buttons without having to figure out the unique bit field value for each combination.

You test for a condition by first assigning each result to a temporary integer variable and then comparing the Button or Shift arguments to a bit mask.  Use the AndLANAND operator with each argument to test if the condition is greater than zero, indicating the key or button is pressedfor example:

LeftDown = (Button And LEFT_BUTTON) > 0
CtrlDown = (Shift And CTRL_MASK) > 0

Then, in a procedure, you can test for any combination of conditionsfor example:

If LeftDown And CtrlDown Then


Note   You can use MouseDown and MouseUpD0VNMJ event procedures to respond to events caused by pressing and releasing mouse buttons.

The Button argument for MouseMove differs from the Button argument for MouseDown and MouseUp.  For MouseMove, the Button argument indicates the current state of all buttons; a single MouseMove event can indicate that some, all, or no button is pressed.  For MouseDown or MouseUp, the Button argument indicates exactly one button per event.

Any time you move a window inside a MouseMove event, it can cascade.  MouseMove events are generated when the window moves underneath the pointer.  A MouseMove event can be generated even if the mouse is perfectly stationary.


See Also


Click Event68UQAKP

DblClick Event2UG5QK1

MousePointer Property1VSV6UM

MouseDown, MouseUp EventsD0VNMJ


Programmer's Guide:

Chapter 12, "Responding to Mouse Events"

MouseMove Event Example

The example demonstrates a simple paint application.  The MouseDown procedure works with a related MouseMove procedure to paint when any mouse button is pressed.  The MouseUp procedure turns off the paintbrush.  To try this example, paste the code into the Declarations section of a form.  Then press F5, click the form and move the mouse while the mouse button is pressed.

Dim PaintNow As Integer              ' Declare variable.
Sub Form_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
  PaintNow = True                    ' Paintbrush on.
End Sub

Sub Form_MouseUp (Button As Integer, X As Single, Y As Single)
  PaintNow = False                   ' Turn off painting.
End Sub

Sub Form_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single)
  If PaintNow Then
    PSet (X, Y)                      ' Draw a point.
  End If
End Sub

Sub Form_Load ()
  DrawWidth = 10                     ' Use wider paintbrush.
  ForeColor = RGB(0, 0, 255)         ' Set drawing color.
End Sub