WPF在鼠标下获取元素 [英] WPF Get Element(s) under mouse
问题描述
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
Visual
s that intersect a location, [...] you must use [...] the[VisualTreeHelper.]HitTest
method that accepts aHitTestResultCallback
delegate. Before this version ofHitTest
returns, the delegate is invoked once for each relevantVisual
, 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屋!