Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Overview of using the mouse

Feedback

In this article

Receiving and handling mouse input is an important part of every Windows application. You can handle mouse events to carry out an action in your application, or use mouse location information to perform hit testing or other actions. Also, you can change the way the controls in your application handle mouse input. This article describes these mouse events in detail, and how to obtain and change system settings for the mouse.

In Windows Forms, user input is sent to applications in the form ofWindows messages. A series of overridable methods process these messages at the application, form, and control level. When these methods receive mouse messages, they raise events that can be handled to get information about the mouse input. In many cases, Windows Forms applications can process all user input simply by handling these events. In other cases, an application can override one of the methods that process messages to intercept a particular message before it's received by the application, form, or control.

Mouse Events

All Windows Forms controls inherit a set of events related to mouse and keyboard input. For example, a control can handle theMouseClick event to determine the location of a mouse click. For more information on the mouse events, seeUsing mouse events.

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, theControl that contains theHotSpot raises the appropriate mouse event.

You can obtain the current mouse position with theLocation property of theMouseEventArgs when handling a mouse event or by using thePosition property of theCursor class. You can then use mouse location information to carry out 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 theListView,TreeView,MonthCalendar, andDataGridView controls.

Hit-testing is useful for determining when your application should do a specific action, when used with the appropriate mouse event, such asMouseHover.

Changing mouse input settings

You can detect and change the way a control handles mouse input by deriving from the control and using theGetStyle andSetStyle methods. TheSetStyle method takes a bitwise combination ofControlStyles values to determine whether the control has standard click, double-click behavior, or if the control handles its own mouse processing. Also, theSystemInformation 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.

PropertyDescription
DoubleClickSizeGets 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.
DoubleClickTimeGets the maximum number of milliseconds that can elapse between a first click and a second click for the mouse action to be considered a double-click.
MouseButtonsGets the number of buttons on the mouse.
MouseButtonsSwappedGets a value indicating whether the functions of the left and right mouse buttons have been swapped.
MouseHoverSizeGets 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.
MouseHoverTimeGets the time, in milliseconds, that the mouse pointer has to stay in the hover rectangle before a mouse hover message is generated.
MousePresentGets a value indicating whether a mouse is installed.
MouseSpeedGets a value indicating the current mouse speed, from 1 to 20.
MouseWheelPresentGets a value indicating whether a mouse with a mouse wheel is installed.
MouseWheelScrollDeltaGets the amount of the delta value of the increment of a single mouse wheel rotation.
MouseWheelScrollLinesGets the number of lines to scroll when the mouse wheel is rotated.

Methods that process user input messages

Forms and controls have access to theIMessageFilter interface and a set of overridable methods that process Windows messages at different points in the message queue. These methods all have aMessage parameter, which encapsulates the low-level details of Windows messages. You can implement or override these methods to examine the message and then either consume the message or pass it on to the next consumer in the message queue. The following table presents the methods that process all Windows messages in Windows Forms.

MethodNotes
PreFilterMessageThis method intercepts queued (also known as posted) Windows messages at the application level.
PreProcessMessageThis method intercepts Windows messages at the form and control level before they have been processed.
WndProcThis method processes Windows messages at the form and control level.
DefWndProcThis method performs the default processing of Windows messages at the form and control level. This provides the minimal functionality of a window.
OnNotifyMessageThis method intercepts messages at the form and control level, after they've been processed. TheEnableNotifyMessage style bit must be set for this method to be called.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?