Is there a way to get a test for disabled control?

I am trying to detect a control under a mouse cursor, regardless of whether the control is enabled or not.

VisualTreeHelper.FindElementsInHostCoordinatesignores controls that have their property IsEnabledset to false. Is there a way to change this behavior or any other way to find controls on a particular screen?

Thank.

+3
source share
1 answer

You can implement your own recursive method to search for a subtree and convert each element into a visual root file of the application to get its “absolute” borders, and then check if the “absolute” mouse point is in this region.

, , . FindElementsInHostCoordinates , MouseMove. " " FrameworkElements, ActualWidth ActualHeight .

private IEnumerable<UIElement> FindAllElementsInHostCoordinates(Point intersectingPoint, UIElement subTree)
{
    var results = new List<UIElement>();

    int count = VisualTreeHelper.GetChildrenCount(subTree);

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(subTree, i) as FrameworkElement;

        if (child != null)
        {
            GeneralTransform gt = child.TransformToVisual(Application.Current.RootVisual as UIElement);
            Point offset = gt.Transform(new Point(0, 0));
            Rect elementBounds = new Rect(offset.X, offset.Y, child.ActualWidth, child.ActualHeight);

            if (IsInBounds(intersectingPoint, elementBounds))
            {
                results.Add(child as UIElement);
            }
        }

        results.AddRange(FindAllElementsInHostCoordinates(intersectingPoint, child));
    }

    return results;
}

private bool IsInBounds(Point point, Rect bounds)
{
    if (point.X > bounds.Left && point.X < bounds.Right &&
        point.Y < bounds.Bottom && point.Y > bounds.Top)
    {
        return true;
    }

    return false;
}

, , MouseMove, Application.Current.RootVisual:

IEnumerable<UIElement> elements = FindAllElementsInHostCoordinates(e.GetPosition(Application.Current.RootVisual), this);
+1

Source: https://habr.com/ru/post/1764022/


All Articles