Monitoring Mouse Activity
Visual Basic responds to various mouse events, which are recognized by most of the controls.
The main events are MouseDown, MouseUp and MouseMove.
Mouse Event Procedures
MouseDown User clicks one of the mouse button
MouseUp User releases the mouse button
MouseMove User moves the mouse pointer to a control or to a blank area of
the form
Syntax
Mouse left, right or middle button Shift/alt/ctrl keys in keyboard
Objectname_mouseevent(Button As integer, Shift As Integer, X as single, Y as
single)
X position y position on screen
The first argument is an integer called Button. The value of the argument indicates
whether the left, right or middle mouse button was clicked.
The second argument in an integer called shift. The value of this argument indicates
whether the mouse button was clicked simultaneously with the Shift key, Ctrl key or Alt
key.
The third and fourth arguments X and Y are the coordinates of the mouse location at the
time the mouse button was clicked.
Eg.
Form_mousedown
As the Form_MouseDown( ) is executed automatically whenever the mouse button is clicked
inside the Form's area the X, Y co-ordinates are referenced to the form.
Circle(x,y),75
Example:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Circle (X, Y), 150, vbYellow
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Circle (X, Y), 500, QBColor(Rnd * 15)
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Circle (X, Y), 300, vbRed
End Sub
Eg circle
Circle (X, Y), 1000, vbRed ' normal circle
Circle (X, Y), 1000, vbBlue, , , 0.5 'horizontal oval
Circle (X + 1000, Y + 1000), 1000, vbGreen, , , 1.5 ' vertical oval
Circle (X + 2000, Y + 2000), 1000, vbCyan, 1, 0.5 ' arc
Drawing Lines
Draw lines from one position (x1,y1) to another position (x2,y2)
syntax
Line(x1,y1) –(x2,y2)
X1,y1 – starting position
X2,y2- ending position
Line(x1,y1) –(x2,y2), color
Line(x1,y1) –(x2,y2), color, B -> line will be considered as Box
Line(x1,y1) –(x2,y2), color, BF -> line will be considered as Filled Box
For filled box, user can apply fillstyle and fillcolor.
Eg.
Line (X, Y)-(X + 1000, Y + 1000), QBColor(Rnd * 15), BF
PSet
This is used draw a point on the screen.
Syntax
Pset (x,y) , color
X,y – positions and color is any vbcolor
Eg.
Drawwidth=5
Pset(x,y),vbgreen
Using the button argument
Button constant value
Left vbleftbutton 1
Right vbrightbutton 2
Middle vbmiddlebutton 4
Combine the keyboard and the mouse (shift parameter)
Action constant Bit set and value
Shift key down vbshiftmask bit 0: value=1
Ctrl key down vbctrlmask bit 1: value=2
Alt key down vbaltmask bit 2: value=4
Eg. Mouse button
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim color As Integer
Randomize
color = Int(Rnd * 15)
Select Case Button
Case vbLeftButton
FillStyle = 5
Circle (X, Y), 500, QBColor(color)
FillColor = vbYellow
Case vbRightButton
FillStyle = 2
FillColor = QBColor(color)
Circle (X, Y), 400
End Select
End Sub
Example for shift parameter
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case Shift
Case vbShiftMask
Print "you pressed the shift key"
Case vbCtrlMask
Print "you pressed the ctrl key"
Case vbAltMask
Print "you pressed the alt key"
Case vbShiftMask + vbAltMask
Print "you pressed the shift and alt keys"
End Select
End Sub
Drag and drop operations for controls
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.move x,y
End Sub
Events, properties and methods for dragging and dropping
properties
Dragmode – manual (=0) and automatic (=1)
Dragicon - change from gray rectangle to a custom icon when dragging
Event
Dragdrop – generated when source is dropped on the target control
Dragover – source control passes over during dragging
Method
Drag – start or stop dragging when drag mode is set to manual.
Manual dragging
Control.drag type of action
Control.drag 0 cancel dragging
Control.drag 1 begin dragging
Control.drag 2 drop the control