Visual Basic Concepts
MouseDown is the most frequently used of the three mouse events. It can be used to reposition controls on a form at run time or to create graphical effects, for instance. The MouseDown event is triggered when a mouse button is pressed.
Note The mouse events are used to recognize and respond to the various mouse states as separate events and should not be confused with the Click and DblClick events. The Click event recognizes when a mouse button has been pressed and released, but only as a single action — a click. The mouse events also differ from the Click and DblClick events in that they enable you to distinguish between the left, right, and middle mouse buttons and the SHIFT, CTRL, and ALT keys.
The MouseDown event is combined with the Move method to move a command button to a different location on a form. The new location is determined by the position of the mouse pointer: When the user clicks anywhere on the form (except on the control), the control moves to the cursor location.
A single procedure, Form_MouseDown, performs this action:
Private Sub Form_MouseDown (Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Command1.Move X, Y
End Sub
The Move method places the command button control's upper-left corner at the location of the mouse pointer, indicated by the x and y arguments. You can revise this procedure to place the center of the control at the mouse location:
Private Sub Form_MouseDown (Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Command1.Move (X - Command1.Width / 2), _
(Y - Command1.Height / 2)
End Sub
The Click-A-Line sample application responds to a mouse click by drawing a line from the previous drawing location to the new position of the mouse pointer. This application uses the MouseDown event and the Line method. Using the following syntax, the Line method will draw a line from the last point drawn to the point (x2, y2):
Line – (x2, y2)
Click-A-Line uses a blank form with one procedure, Form_MouseDown:
Private Sub Form_MouseDown (Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Line -(X, Y)
End Sub
The first line starts at the upper-left corner, which is the default origin. Thereafter, whenever the mouse button is pressed, the application draws a straight line extending from the previous line to the present location of the mouse pointer. The result is a series of connected lines, as shown in Figure 11.1.
Figure 11.1 Connecting lines are drawn whenever MouseDown is invoked
For More Information See "MouseDown Event" in the Language Reference.