WPF Get Element(s) under mouse

风流意气都作罢 提交于 2019-11-26 03:59:31

问题


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


回答1:


From "WPF Unleashed", page 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.

The signature of such a callback is

HitTestResultBehavior Callback(HitTestResult result)

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;
}

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




回答2:


You can also try using the Mouse.DirectlyOver property to get the top-most element that is under the mouse.




回答3:


Can you use the VisualTreeHelper.HitTest ?

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



来源:https://stackoverflow.com/questions/45813/wpf-get-elements-under-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!