Track mouse events in your VB6 programs by capturing the mouse

The way Visual Basic 6 handles mouse events is called capturing the mouse. Find out what happens when a user captures the mouse, and then learn how you can verify this behavior yourself.

The phrase "capture the mouse" refers to the way VB6 handles mouse events. By default, VB forms and controls capture the mouse, and this behavior can affect the way your users make use of the mouse.

If you're wondering what "capture" really means in this context, here are two sample scenarios that will make it clearer.

Scenario 1: Suppose that the user presses and releases the mouse button while the pointer is over a specific control. The control fires the following events in order: MouseDown, MouseUp, Click.

Scenario 2: Now imagine that the user presses the mouse button while the pointer is on the control, moves the pointer off the control onto the form, and then releases the button. You might expect that the control would receive a MouseDown event and then the form would receive a MouseUp event. It sounds reasonable, but that's not what happens.

When the mouse button is pressed on a form or control, the mouse is captured, and the form or control receives all mouse events including MouseMove events up to and including the next MouseUp—no matter where the pointer is moved.

Thus, in Scenario 2, the PictureBox receives the MouseDown event, a series of MouseMove events, and the final MouseUp event; the form receives no mouse events at all. There is no Click event in this situation.

You can verify this behavior for yourself by creating a simple project with one control on the form and then using Debug. Print statements in the various mouse event procedures to track the events that occur.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

Editor's Picks

Free Newsletters, In your Inbox