Редактиране

Using mouse events (Windows Forms .NET)

Most Windows Forms programs process mouse input by handling the mouse events. This article provides an overview of the mouse events, including details on when to use each event and the data that is supplied for each event. For more information about events in general, see Events overview (Windows Forms .NET).

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're 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.

Mouse information

A MouseEventArgs is sent to the handlers of mouse events related to clicking a mouse button and tracking mouse movements. MouseEventArgs provides information about the current state of the mouse, including the location of the mouse pointer in client coordinates, which mouse buttons are pressed, and whether the mouse wheel has scrolled. Several mouse events, such as those that are raised when the mouse pointer has entered or left the bounds of a control, send an EventArgs to the event handler with no further information.

If you want to know the current state of the mouse buttons or the location of the mouse pointer, and you want to avoid handling a mouse event, you can also use the MouseButtons and MousePosition properties of the Control class. MouseButtons returns information about which mouse buttons are currently pressed. The MousePosition returns the screen coordinates of the mouse pointer and is equivalent to the value returned by Position.

Converting Between Screen and Client Coordinates

Because some mouse location information is in client coordinates and some is in screen coordinates, you may need to convert a point from one coordinate system to the other. You can do this easily by using the PointToClient and PointToScreen methods available on the Control class.

Standard Click event behavior

If you want to handle mouse click events in the proper order, you need to know the order in which click events are raised in Windows Forms controls. All Windows Forms controls raise click events in the same order when any supported mouse button is pressed and released, except where noted in the following list for individual controls. The following list shows the order of events raised for a single mouse-button click:

  1. MouseDown event.
  2. Click event.
  3. MouseClick event.
  4. MouseUp event.

The following is the order of events raised for a double mouse-button click:

  1. MouseDown event.

  2. Click event.

  3. MouseClick event.

  4. MouseUp event.

  5. MouseDown event.

  6. DoubleClick event.

    This can vary, depending on whether the control in question has the StandardDoubleClick style bit set to true. For more information about how to set a ControlStyles bit, see the SetStyle method.

  7. MouseDoubleClick event.

  8. MouseUp event.

Individual controls

The following controls don't conform to the standard mouse click event behavior:

Painting behavior of toggle controls

Toggle controls, such as the controls deriving from the ButtonBase class, have the following distinctive painting behavior in combination with mouse click events:

  1. The user presses the mouse button.

  2. The control paints in the pressed state.

  3. The MouseDown event is raised.

  4. The user releases the mouse button.

  5. The control paints in the raised state.

  6. The Click event is raised.

  7. The MouseClick event is raised.

  8. The MouseUp event is raised.

    Note

    If the user moves the pointer out of the toggle control while the mouse button is down (such as moving the mouse off the Button control while it is pressed), the toggle control will paint in the raised state and only the MouseUp event occurs. The Click or MouseClick events will not occur in this situation.

See also