Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to "Capture the mouse" in WPF?

Tags:

events

wpf

mouse

On System.Windows.UIElement there is a CaptureMouse() and a paired ReleaseMouseCapture() method. In this WPF DragDrop sample they call CaptureMouse on MouseDown and release it on MouseUp. The documentation in MSDN is about as useless as it comes - "CaptureMouse -> Captures the mouse."

In my head before trying it I assumed that it somehow locked the mouse inside the UIElement bounds, but that's clearly not the case when I try it. From experimenting, it seems to have something to do with responding to events when the mouse is outside of the UIElement, but not wanting to be a cargo cult programmer I don't want to just use it because the example does, I'd like an authoritative description of what it means.

like image 302
Eclipse Avatar asked Jun 02 '09 23:06

Eclipse


2 Answers

From Capture and Uncapture the Mouse on MSDN:

When an object captures the mouse, all mouse related events are treated as if the object with mouse capture perform the event, even if the mouse pointer is over another object.

Only the capturing control receives the mouse events until released.

Capturing the mouse is useful for dragging because all the dragging code can exist in the one control, rather than being spread over multiple controls.

like image 74
Cameron MacFarland Avatar answered Oct 26 '22 23:10

Cameron MacFarland


When it has captured the mouse, a control will receive mouse events even if the mouse pointer is no longer within its bounding area.

Typically, it's used for:

  • Drag and drop
  • Buttons (to handle Mouse Up when you put the mouse down on the button and move the mouse before you release the button)
like image 26
Alun Harford Avatar answered Oct 26 '22 23:10

Alun Harford