See Also Example
Applies To
Form (not MDI form), check box
Description
Occurs when the user moves the mouse.
Syntax
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)
Remarks
The MouseMove event uses these arguments:
Argument Description
Index Uniquely identifies a control if it is in a control array .
Button The state of the mouse buttons, in which a bit is set if the button is down. The Button argument is a bit field , 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 ScaleHeight , Scale Width , ScaleLeft , and ScaleTop 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 constants These mouse button constants have the following values:
Constant Value
LEFT_BUTTON 1
RIGHT_BUTTON 2
MIDDLE_BUTTON 4
SHIFT_MASK 1
CTRL_MASK 2
ALT_MASK 4
These are in CONSTANT.TXT. The constants act as bit masks 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 And 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 MouseUp
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.
Help:
Click Event
DblClick Event
MousePointer Property
MouseDown, MouseUp Events
Programmer's Guide:
Chapter 12, "Responding to Mouse Events"
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