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

查看:239
本文介绍了“捕获鼠标”是什么意思在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之外的事件响应有关,但不希望成为货物崇拜程序员我不想只是使用它,因为例子,我想要一个权威的描述它的意思。

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.

推荐答案

捕获并取消捕获鼠标在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.

捕获鼠标对于拖动很有用,因为只有捕获控件接收鼠标事件直到发布。所有拖动代码都可以存在于一个控件中,而不是分布在多个控件上。

Capturing the mouse is useful for dragging because only the capturing control receives the mouse events until released. All the dragging code can exist in the one control, rather than being spread over multiple controls.

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

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