MouseDown, MouseUp Events

See Also29SHUZY              Example19ER8QL>Low

Apply To

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

Description

Occur when the user presses (MouseDown) or releases (MouseUp) a mouse button.

Syntax

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

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

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

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

Remarks

The MouseDown and MouseUp events use these arguments:

Argument     Description

 

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

Button            The button was pressed (MouseDown) or released (MouseUp) to cause the event.  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.  Only one of the bits is set, indicating which button caused the event.

Shift              The state of the Shift, Ctrl and Alt keys when the button specified in the Button argument was pressed or released.  A 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 Width3J8U7ZN, ScaleLeft, and ScaleTop138Z3ME properties of the object.

 

Use a MouseDown or MouseUp procedure to specify actions to occur when a given mouse button is pressed or released.  Unlike the Click68UQAKP and DblClick2UG5QK1 events, MouseDown and MouseUp events allow you to distinguish between the left, right, and middle mouse buttons.  You can also write code for mouse-keyboard combinations that use the Shift, Ctrl, and Alt keyboard modifiers198ZVV6.

The following applies to both Click and DblClick events:

         If a mouse button is pressed while the pointer is over a form or control, that object "captures" the mouse and receives all mouse events up to and including the last MouseUp event.  This implies that the X, Y mouse-pointer coordinates given by a mouse event may not always be in the client area of the object that receives them.

         If mouse buttons are pressed in succession, the object that captures the mouse after the first press receives all mouse events until all buttons are released.

 

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

RIGHT_BUTTON        2

MIDDLE_BUTTON      4

SHIFT_MASK              1

CTRL_MASK               2

ALT_MASK                 4

 

These constants are in CONSTANT.TXT.  The constants then 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.

 

Note   You can use a MouseMove1D70SW5 procedure to respond to an event caused by moving the mouse.  The Button argument for MouseDown and MouseUp differs from the Button argument used for MouseMove.  For MouseDown or MouseUp, the Button argument indicates exactly one button per event; for MouseMove, it indicates the current state of all buttons.

 


See Also

Help:

Click Event68UQAKP

DblClick Event2UG5QK1

MouseMove Event1D70SW5

MousePointer Property1VSV6UM

 

Programmer's Guide:

Chapter 12, "Responding to Mouse Events"


MouseDown, MouseUp Events 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 and dragged.  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 depressed.

Dim PaintNow As Integer

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, Shift 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