在System.Windows.UIElement
上有一个CaptureMouse()
和一个配对的ReleaseMouseCapture()
方法。在此WPF DragDrop示例中,他们在 MouseDown 上调用 CaptureMouse,并在 MouseUp 上释放它。 documentation in MSDN几乎毫无用处 - “CaptureMouse -> 捕获鼠标。”
在尝试之前,我的脑海中假设它以某种方式将鼠标锁定在 UIElement 范围内,但当我尝试它时,情况显然并非如此。通过实验,这似乎与鼠标位于 UIElement 之外时响应事件有关,但又不想成为 cargo cult programmer我不想仅仅使用它,因为示例使用了它,我想要一个关于它含义的权威描述。
最佳答案
来自Capture and Uncapture the Mouse在 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.
只有捕获控件会接收鼠标事件,直到释放为止。
捕获鼠标对于拖动非常有用,因为所有拖动代码都可以存在于一个控件中,而不是分布在多个控件中。
关于wpf - WPF中的 "Capture the mouse"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/942357/