在 WPF 中“捕获鼠标”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/942357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean to "Capture the mouse" in WPF?
提问by Eclipse
On System.Windows.UIElement
there is a CaptureMouse()
and a paired ReleaseMouseCapture()
method. In this WPF DragDropsample they call CaptureMouse on MouseDown and release it on MouseUp. The documentation in MSDNis about as useless as it comes - "CaptureMouse -> Captures the mouse."
上System.Windows.UIElement
有一个CaptureMouse()
和一个配对的ReleaseMouseCapture()
方法。在这个WPF DragDrop示例中,他们在 MouseDown 上调用 CaptureMouse 并在 MouseUp 上释放它。MSDN 中的文档几乎没有用处 - “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 programmerI don't want to just use it because the example does, I'd like an authoritative description of what it means.
在尝试之前,在我的脑海中,我假设它以某种方式将鼠标锁定在 UIElement 边界内,但是当我尝试时显然不是这种情况。从实验来看,当鼠标在 UIElement 之外时,它似乎与响应事件有关,但不想成为一个货物崇拜程序员我不想只使用它,因为示例确实如此,我想对其含义的权威描述。
回答by Cameron MacFarland
From Capture and Uncapture the Mouseon MSDN:
来自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.
捕获鼠标对于拖动很有用,因为所有拖动代码都可以存在于一个控件中,而不是分布在多个控件上。
回答by Alun Harford
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)
- 拖放
- 按钮(当您将鼠标放在按钮上并在释放按钮之前移动鼠标时处理鼠标向上)
回答by rmoore
The Silverlight 2 documentationfor it has a more verbose description, I don't know why it isn't a part of the 3.5 documentation page too:
在Silverlight 2中的文档为它有一个更详细的描述,我不知道为什么它不是3.5文档页面的一部分,太:
When an object has captured the mouse, that object receives mouse input whether or not the mouse pointer is within its bounding area. The mouse is typically only captured during simulated drag operations.
...
当一个对象捕获了鼠标时,无论鼠标指针是否在其边界区域内,该对象都会接收鼠标输入。通常仅在模拟拖动操作期间捕获鼠标。
...
It works the same with WPF, and so the reason it is used with DragDrop, is that is how the it knows to report back to the control being dragged from when the mouse may be outside of that control. If you comment out the MyCanvas.Capture() and the Capture(Null) (which clears it) then you can no longer drop.
它与 WPF 的工作方式相同,因此它与 DragDrop 一起使用的原因是它知道如何在鼠标可能超出该控件时向被拖动的控件报告。如果您注释掉 MyCanvas.Capture() 和 Capture(Null)(清除它),那么您将无法再删除。