WPF在鼠标下获取元素 [英] WPF Get Element(s) under mouse

查看:202
本文介绍了WPF在鼠标下获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF是否可以通过MouseMove事件在鼠标下获取元素数组?

Is there a way with WPF to get an array of elements under the mouse on a MouseMove event?

推荐答案

摘自" WPF推出",第383:

视觉命中测试可以通知您 关于与所有 Visual相交的 位置,您必须使用[...] 接受一个[VisualTreeHelper.]HitTest方法 HitTestResultCallback代表.前 此版本的HitTest返回, 每个代表被调用一次 相关Visual,从 最顶端,最底端结束.

Visual hit testing can inform you about all Visuals that intersect a location, [...] you must use [...] the [VisualTreeHelper.]HitTest method that accepts a HitTestResultCallback delegate. Before this version of HitTest returns, the delegate is invoked once for each relevant Visual, starting from the topmost and ending at the bottommost.

这样的回调的签名是

HitTestResultBehavior Callback(HitTestResult result)

,它必须返回HitTestResultBehaviour.Continue才能接收进一步的匹配,如下所示(来自MSDN的链接页面):

and it has to return HitTestResultBehaviour.Continue to receive further hits, as shown below (from the linked page on MSDN):

// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
    // Add the hit test result to the list that will be processed after the enumeration.
    hitResultsList.Add(result.VisualHit);

    // Set the behavior to return visuals at all z-order levels.
    return HitTestResultBehavior.Continue;
}

有关更多信息,请查阅 MSDN文档表示VisualTreeHelper.HitTest .

For further information, please consult the MSDN documentation for VisualTreeHelper.HitTest.

这篇关于WPF在鼠标下获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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