How Mouse Input Works in Windows Forms
Receiving and handling mouse input is an important part of every Windows application. You can handle mouse events to perform an action in your application, or use mouse location information to perform hit testing or other actions. In addition, you can change the way the controls in your application handle mouse input. This topic describes these mouse events in detail, and how to obtain and change system settings for the mouse. For more information about the data provided with the mouse events and the order in which the mouse click events are raised, see Mouse Events in Windows Forms.
Mouse Location and Hit-Testing
When the user moves the mouse, the operating system moves the mouse pointer. The mouse pointer contains a single pixel, called the hot spot, which the operating system tracks and recognizes as the position of the pointer. When the user moves the mouse or presses a mouse button, the Control that contains the HotSpot raises the appropriate mouse event. You can obtain the current mouse position with the Location property of the MouseEventArgs when handling a mouse event or by using the Position property of the Cursor class. You can subsequently use mouse location information to perform hit-testing, and then perform an action based on the location of the mouse. Hit-testing capability is built in to several controls in Windows Forms such as the ListView, TreeView, MonthCalendar and DataGridView controls. Used with the appropriate mouse event, MouseHover for example, hit-testing is very useful for determining when your application should perform a specific action.
Mouse Events
The primary way to respond to mouse input is to handle mouse events. The following table shows the mouse events and describes when they are raised.
Mouse Event | Description |
---|---|
Click | This event occurs when the mouse button is released, typically before the MouseUp event. The handler for this event receives an argument of type EventArgs. Handle this event when you only need to determine when a click occurs. |
MouseClick | This event occurs when the user clicks the control with the mouse. The handler for this event receives an argument of type MouseEventArgs. Handle this event when you need to get information about the mouse when a click occurs. |
DoubleClick | This event occurs when the control is double-clicked. The handler for this event receives an argument of type EventArgs. Handle this event when you only need to determine when a double-click occurs. |
MouseDoubleClick | This event occurs when the user double-clicks the control with the mouse. The handler for this event receives an argument of type MouseEventArgs. Handle this event when you need to get information about the mouse when a double-click occurs. |
MouseDown | This event occurs when the mouse pointer is over the control and the user presses a mouse button. The handler for this event receives an argument of type MouseEventArgs. |
MouseEnter | This event occurs when the mouse pointer enters the border or client area of the control, depending on the type of control. The handler for this event receives an argument of type EventArgs. |
MouseHover | This event occurs when the mouse pointer stops and rests over the control. The handler for this event receives an argument of type EventArgs. |
MouseLeave | This event occurs when the mouse pointer leaves the border or client area of the control, depending on the type of the control. The handler for this event receives an argument of type EventArgs. |
MouseMove | This event occurs when the mouse pointer moves while it is over a control. The handler for this event receives an argument of type MouseEventArgs. |
MouseUp | This event occurs when the mouse pointer is over the control and the user releases a mouse button. The handler for this event receives an argument of type MouseEventArgs. |
MouseWheel | This event occurs when the user rotates the mouse wheel while the control has focus. The handler for this event receives an argument of type MouseEventArgs. You can use the Delta property of MouseEventArgs to determine how far the mouse has scrolled. |
Changing Mouse Input and Detecting System Settings
You can detect and change the way a control handles mouse input by deriving from the control and using the GetStyle and SetStyle methods. The SetStyle method takes a bitwise combination of ControlStyles values to determine whether the control will have standard click or double-click behavior or if the control will handle its own mouse processing. In addition, the SystemInformation class includes properties that describe the capabilities of the mouse and specify how the mouse interacts with the operating system. The following table summarizes these properties.
Property | Description |
---|---|
DoubleClickSize | Gets the dimensions, in pixels, of the area in which the user must click twice for the operating system to consider the two clicks a double-click. |
DoubleClickTime | Gets the maximum number of milliseconds that can elapse between a first click and a second click for the operating system to consider the mouse action a double-click. |
MouseButtons | Gets the number of buttons on the mouse. |
MouseButtonsSwapped | Gets a value indicating whether the functions of the left and right mouse buttons have been swapped. |
MouseHoverSize | Gets the dimensions, in pixels, of the rectangle within which the mouse pointer has to stay for the mouse hover time before a mouse hover message is generated. |
MouseHoverTime | Gets the time, in milliseconds, that the mouse pointer has to stay in the hover rectangle before a mouse hover message is generated. |
MousePresent | Gets a value indicating whether a mouse is installed. |
MouseSpeed | Gets a value indicating the current mouse speed, from 1 to 20. |
MouseWheelPresent | Gets a value indicating whether a mouse with a mouse wheel is installed. |
MouseWheelScrollDelta | Gets the amount of the delta value of the increment of a single mouse wheel rotation. |
MouseWheelScrollLines | Gets the number of lines to scroll when the mouse wheel is rotated. |
See also
.NET Desktop feedback