获取 WPF 元素的真实视觉边界框?

发布于 2024-10-18 21:04:51 字数 449 浏览 1 评论 0原文

对于我的问题的简化版本,我想计算布局转换(甚至可能渲染转换)形状的边界框,以便我始终可以在形状周围完美地拟合矩形,无论其旋转或缩放如何或许。如果你能解决这个问题,我会很高兴。

更复杂的问题是计算任何框架元素的视觉边界框。我所说的“视觉边界框”是指框架元素内最顶部的可见像素确定顶部边界,最右侧的可见像素确定右侧边界,等等。如果你能解决这个问题,我会更高兴。

我尝试使用 LayoutInformation.GetLayoutSlot(),但这并没有按预期方式工作。 “布局槽”实际上比实际边界大得多。我还尝试使用 VisualTreeHelper.GetDescendantBounds(),但由于我的测试形状的 VisualParent 受到保护,我无法访问此属性,并决定在进一步研究之前先检查一下。

我希望有人可以提供一种简单的方法来获取 WPF 中元素的真实视觉边界框,这是在所有转换之后计算的。如果我的问题没有说清楚,请告诉我。

For a simplified version of my problem, I would like to calculate the bounding box of a layout-transformed (possibly even render-transformed) shape, so that I can always fit a rectangle perfectly around the shape, no matter what its rotation or scale may be. If you can solve this, I will be happy.

The more complex problem is that of calculating the visual bounding box of any framework element. By 'visual bounding box' I mean the top-most visible pixel within the framework element determines the top-bound, the right-most visible pixel determines the right-bound, etc. If you can solve this, I will be even more happy.

I have tried playing with LayoutInformation.GetLayoutSlot(), but this did not work in the expected manner. The 'layout slot' was actually MUCH larger than the actual bounds. I also tried using VisualTreeHelper.GetDescendantBounds(), but because of the VisualParent of my test shape being protected I could not manage to access this property, and decided to check here before I go any further into it.

I hope that somebody can provide an easy way of getting the true visual bounding box of an element in WPF, that is calculated AFTER all transforms. If I have not made something clear in my question, please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吖咩 2024-10-25 21:04:51
private Rect GetRectOfObject(FrameworkElement _element)
{
    Rect rectangleBounds = _element.RenderTransform.TransformBounds(
        new Rect(_element.RenderSize);

    return rectangleBounds;
}

也许这会有所帮助。

private Rect GetRectOfObject(FrameworkElement _element)
{
    Rect rectangleBounds = _element.RenderTransform.TransformBounds(
        new Rect(_element.RenderSize);

    return rectangleBounds;
}

Maybe this will help out.

沉鱼一梦 2024-10-25 21:04:51

使用 VisualTreeHelper.GetDescendantBounds() 可以获得良好的结果,并且可以使用 VisualTreeHelper.GetParent() 来访问受其他保护的 VisualParent 属性。然而,您可能想要做的是对形状本身而不是其父对象调用 GetDescendantBounds,因为无论其名称如何,该方法都会返回父对象及其所有后代的边界。

You will get good results with VisualTreeHelper.GetDescendantBounds() and you can use VisualTreeHelper.GetParent() to gain access to the otherwise protected VisualParent property. However what you probably want to do is call GetDescendantBounds on the shape itself, not its parent, because in spite of its name, the method returns the bounds of the parent and all of its decendants.

空袭的梦i 2024-10-25 21:04:51

这个问题并不容易,因为控件可能会超出其范围。

但如果您认为这种情况不会发生,您可以通过使用 parent.TranslatePoint(point_in_child_coord_system, child) 转换 (0,0), ( child.ActualWidth,0)(child.ActualWidth, child.ActualHeight)(0,child.ActualHeight) 到父坐标系。然后对所有点的 x 和 y 坐标进行排序,并使用最小值和最大值来找到边界框。

注意:排序是必要的,因为可能会发生子轮换。

The problem is not easy, as a control may draw outside its bounds.

But if you assume this doesn't happen you can solve the problem by using parent.TranslatePoint(point_in_child_coord_system, child) to transform (0,0), (child.ActualWidth,0), (child.ActualWidth, child.ActualHeight) and (0,child.ActualHeight) to the parent coord system. Then sort the x and y coordinates of all points and use minimum and maximum values to find the bounding box.

Note: sorting is necessary because of possible child rotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文