What does mouse capture mean in WPF?

In System.Windows.UIElement there is a CaptureMouse() and a paired ReleaseMouseCapture() method. In this WPF DragDrop example, they call CaptureMouse on MouseDown and release it on MouseUp. The MSDN documentation is about as useless as it is - "CaptureMouse β†’ Mouse Capture".

In my head, before I tried, I suggested that she somehow locked the mouse inside the borders of the UIElement, but this is clearly not the case when I try. From the experiments, it seems that something is connected with the response to events when the mouse is outside the UIElement, but not wanting to be a cult cult programmer. I do not want to just use it, because in this example I would like to get a reliable description of what this means .

+66
events wpf mouse
Jun 02 '09 at 23:04
source share
3 answers

From Capturing and removing a mouse on MSDN:

When an object captures a mouse, all mouse-related events are processed as if an object with a mouse capture were executing an event, even if the mouse pointer is over another object.

Mouse capture is useful for drag and drop, because only the capture control accepts mouse events until it is released. All drag-and-drop code can exist in one control, and not extend to several controls.

+73
Jun 02 '09 at 23:18
source share

When it grabs the mouse, the control will receive mouse events, even if the mouse pointer is no longer within its bounds.

It is usually used for:

  • Drag and drop
  • Buttons (to control the Mouse Up when you click on the button and move the mouse before releasing the button)
+11
Jun 02 '09 at 23:19
source share

The Silverlight 2 documentation has a more detailed description for it, I don’t know why it is not part of the 3.5 Documentation page:

When an object is captured by a mouse, this object receives mouse input, regardless of whether the mouse pointer is within its boundary region. The mouse is usually removed only during simulated drag and drop operations.
...

It works the same with WPF, and therefore the reason it is used with DragDrop is because it knows that it has to report on drag and drop of the control when the mouse can be outside this control. If you comment out MyCanvas.Capture () and Capture (Null) (which clears it), then you can no longer refuse.

+4
Jun 02 '09 at 23:18
source share



All Articles