Share via


Visual Basic Reference

MouseDown, MouseUp Events Example

This example demonstrates a simple paint application. The MouseDown event procedure works with a related MouseMove event procedure to enable painting when any mouse button is pressed and dragged. The MouseUp event procedure disables painting. To try this example, paste the code into the Declarations section of a form; and then press F5, click the form, and move the mouse while the mouse button is pressed.

  Dim PaintNow As Boolean
Private Sub Form_MouseDown (Button As Integer, Shift As Integer, X As 
Single, Y As Single)
   PaintNow = True   ' Enable painting.
End Sub

Private Sub Form_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)
   PaintNow = False   ' Disable painting.
End Sub

Private 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

Private Sub Form_Load ()
   DrawWidth = 10      ' Use wider brush.
   ForeColor = RGB(0, 0, 255)   ' Set drawing color.
End Sub