The mouse events can be combined with graphics methods and any number of customized
drawing or paint applications can be created. The following application combines
MouseMove and MouseDown events, and illustrates a drawing program.
Open a new Standard EXE project and save the Form as Draw.frm
and save the Project as Draw.vbp. Name the caption of the as Drawing. Add command
button control and name the caption of it as Clear
Enter the following code in the Form_MouseDown ( ) procedure, Form_MouseMove
( ) procedure and cmdClear_Click ( ) procedures respectively.
Private Sub cmdClear_Click()
frmDraw.Cls
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer,
X As Single, Y As Single)
frmDraw.CurrentX = X
frmDraw.CurrentY = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
If Button = 1 Then
Line (frmDraw.CurrentX, frmDraw.CurrentY)-(X, Y)
End If
End Sub
Button value 1 indicates that the left mouse button is clicked. The code written
in the MouseDown event changes the CurrentX and CurrentY to the coordinates where
the mouse button was just clicked.
Run the application. You can notice that when the mouse is clicked and moved
in the Form a line is drawn corresponding to the mouse movement. Following figure
illustrates the combined action of MouseDown and MouseMove.

The program uses two graphics related Visual Basic concepts, the Line method
and the CurrentX and CurrentY properties. Line method is preferred to draw a line
in a Form. The following statement draws a line from the coordinates X = 2500,
Y = 2000, X = 5000, Y = 5500
Line (2500, 2000) - (5000, 5500)
The CurrentX and CurrentY properties are not visible in the properties window
of the Form because it cannot be set at the design time. After using the Line
method to draw a line in a Form, Visual Basic automatically assigns the coordinate
of the line's end point to the CurrentX and CurrentY properties of the Form on
which the line is drawn.
( Download the source code )
MouseMove application
Visual Basic does not generate a MouseMove event
for every pixel the mouse moves over and a limited number of mouse messages are
generated per second by the operating environment. The following application illustrates
how often the Form_MouseMove ( ) event is executed.
Open a new standard EXE project and save the form as MouseMove.frm and save
the Project as MouseMOve.vbp. Place a CommandButton control
and name the caption as Clear and set the name as cmdClear.
The following code is entered in the cmdClear_Click ( )
and Form_MouseMove ( ) events respectively.
Private Sub cmdClear_Click()
frmMouseMove.Cls
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Circle (X, Y), 70
End Sub
The above procedure simply draws small circles at the mouse's current location
using the Circle method. The parameter x, y represent the centre of the circle,
and the second parameter represents the radius of the circle.
Save the application and run. You can notice that when the mouse is moved inside
the Form, circles are drwan along the path of the mouse movement as shown in below
figure. And also you can notice the circles are widely spaced when the mouse is
moved quickly. Each small circle is an indication that the MouseMove event occured
and the Form_MouseMove ( ) procedure was executed.

( Download the source code )