WPF在鼠标下获取元素

WPF在鼠标下获取元素

WPF Get Element(s) under mouse

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


您也可以尝试使用Mouse.DirectlyOver属性获取鼠标下方的最顶部元素。


从" WPF释放",页383:

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.

这样的回调的签名是

1
HitTestResultBehavior Callback(HitTestResult result)

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

1
2
3
4
5
6
7
8
9
// 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;
}

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


可以使用VisualTreeHelper.HitTest吗?

http://lukieb.blogspot.com/2008/07/visualtreehelperhittest.html


推荐阅读