![]() ![]() | ||
You can handle mouse events—such as mouse movements—in forms and controls; here are the possible events for the Control class, which is a base class for controls and forms:
MouseDown— Happens when the mouse pointer is over the control and a mouse button is pressed.
MouseEnter— Happens when the mouse pointer enters the control.
MouseHover— Happens when the mouse pointer hovers over the control.
MouseLeave— Happens when the mouse pointer leaves the control.
MouseMove— Happens when the mouse pointer is moved over the control.
MouseUp— Happens when the mouse pointer is over the control and a mouse button is released.
MouseWheel— Happens when the mouse wheel moves while the control has focus.
Here are the properties of the MouseEventArgs object passed to the mouse event handler (not all properties will be filled for all mouse events):
Button— Indicates which mouse button was pressed (see below).
Clicks— The number of times the mouse button was pressed and released.
Delta— A signed count of the number of detents the mouse wheel has rotated. A detent is the rotation of the mouse wheel one notch.
X— The x-coordinate of a mouse click.
Y— The y-coordinate of a mouse click.
The Buttons property holds one of these members of the MouseButtons enumeration:
Left— The left mouse button was pressed.
Middle— The middle mouse button was pressed.
None— No mouse button was pressed.
Right— The right mouse button was pressed.
XButton1— The first XButton was pressed (Microsoft IntelliMouse Explorer).
XButton2— The second XButton was pressed (Microsoft IntelliMouse Explorer).
Here's an example (Mouser on the CD-ROM) that checks for all mouse events in a form (note that for those events that involve button presses, it checks only the left mouse button) and reports on them in a text box. If you play around with this example, you'll see that when you handle some mouse events they virtually cover up others; for example, if you handle mouse move events, you'll rarely see mouse hovering events:
Public Class Form1 Inherits System.Windows.Forms.Form 'Windows Form Designer generated code Private Sub Form1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseDown If e.Button = MouseButtons.Left Then TextBox1.Text = "Mouse down at " + CStr(e.X) + ", " + CStr(e.Y) End If End Sub Private Sub Form1_MouseEnter(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.MouseEnter TextBox1.Text = "Mouse entered." End Sub Private Sub Form1_MouseHover(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.MouseHover TextBox1.Text = "Mouse is hovering." End Sub Private Sub Form1_MouseLeave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.MouseLeave TextBox1.Text = "Mouse left." End Sub Private Sub Form1_MouseMove(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseMove TextBox1.Text = "Mouse moved: " + CStr(e.X) + ", " + CStr(e.Y) End Sub Private Sub Form1_MouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseUp If e.Button = MouseButtons.Left Then TextBox1.Text = "Mouse up at " + CStr(e.X) + ", " + CStr(e.Y) End If End Sub Private Sub Form1_MouseWheel(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyBase.MouseWheel TextBox1.Text = "Mouse rotated " + CStr(e.Delta) + " detents" End Sub End Class
You can see the Mouser example at work in Figure 4.25, where it's reporting a MouseDown event.
![]() ![]() | ||