“捕获鼠标"是什么意思?在 WPF 中? [英] What does it mean to "Capture the mouse" in WPF?

查看:32
本文介绍了“捕获鼠标"是什么意思?在 WPF 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Windows.UIElement 上有一个CaptureMouse() 和一个配对的ReleaseMouseCapture() 方法.在这个 WPF DragDrop 示例中,他们在 MouseDown 上调用 CaptureMouse 并在 MouseUp 上释放它.MSDN 中的文档 与它来了 - CaptureMouse -> 捕获鼠标."

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."

在我尝试之前,我认为它以某种方式将鼠标锁定在 UIElement 边界内,但当我尝试时显然不是这种情况.从实验来看,似乎与鼠标在 UIElement 之外时响应事件有关,但不想成为 cargo cult 程序员 我不想只使用它,因为示例确实如此,我想要对其含义的权威描述.

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.

推荐答案

来自 Capture并在 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.

这篇关于“捕获鼠标"是什么意思?在 WPF 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆