获取 WPF 元素的真实视觉边界框? [英] Getting the true visual bounding box of a WPF element?
问题描述
对于我的问题的简化版本,我想计算布局转换(甚至可能是渲染转换)形状的边界框,以便我始终可以在形状周围完美地放置一个矩形,无论它是什么旋转或缩放可能.如果你能解决这个问题,我会很高兴.
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.
我尝试过使用 LayoutInformation.GetLayoutSlot(),但这并没有以预期的方式工作.布局槽"实际上比实际边界大得多.我还尝试使用 VisualTreeHelper.GetDescendantBounds(),但由于我的测试形状的 VisualParent 受到保护,我无法访问此属性,因此决定在进一步研究之前先检查这里.
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.
我希望有人可以提供一种简单的方法来获取 WPF 中元素的真实视觉边界框,这是在所有变换之后计算的.如果我的问题没有说清楚,请告诉我.
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.
推荐答案
private Rect GetRectOfObject(FrameworkElement _element)
{
Rect rectangleBounds = _element.RenderTransform.TransformBounds(
new Rect(_element.RenderSize);
return rectangleBounds;
}
也许这会有所帮助.
这篇关于获取 WPF 元素的真实视觉边界框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!