gpt4 book ai didi

.net - 获取 DrawingVisual 属性

转载 作者:行者123 更新时间:2023-12-04 05:50:45 30 4
gpt4 key购买 nike

我有一个 DrawingVisual例如,我使用此方法创建的对象:

Private Function CreateDrawingVisualRectangle() As DrawingVisual

Dim drawingVisual As New DrawingVisual()
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
Dim rect As New Rect(New Point(160, 100), New Size(320, 80))
drawingContext.DrawRectangle(Brushes.LightBlue, New Pen(Brushes.Black, 0.5), rect)

Return drawingVisual

End Function

有一种方法可以获取 DrawingContext 绘制的形状类型 method和它的属性?

IE。:
Dim MyVisual as DrawingVisual = CreateDrawingVisualRectangle()

Dim MyVisualType as MyType = MyVisual.GetDrawingType()
Dim MyBrush as Brush = MyVisual.GetDrawingBrush()
Dim MyPen as Pen = MyVisual.GetDrawingPen()
Dim MyRect as Rect = MyVisual.GetDrawingRect()
...

显然,我在上一个示例中使用的方法是现实中不存在的指示性方法,但我会使用它来获取 MyVisual 属性。

谢谢。

最佳答案

您可以递归地遍历 Drawing DrawingGroup 中的对象由 Drawing 提供DrawingVisual 的属性,如果子绘图是 GeometryDrawing,请检查其 Pen , BrushGeometry特性:

void InspectDrawings(DrawingVisual drawingVisual)
{
InspectDrawings(drawingVisual.Drawing);
}

void InspectDrawings(DrawingGroup drawingGroup)
{
foreach (Drawing drawing in drawingGroup.Children)
{
if (drawing is GeometryDrawing)
{
GeometryDrawing geometryDrawing = (GeometryDrawing)drawing;
// inspect properties here
}
else if (drawing is DrawingGroup)
{
// recurse into sub-group
InspectDrawings((DrawingGroup)drawing);
}
}
}

您现在可以检索几何类型并检查更多特殊属性。例如,如果它是一个 RectangleGeometry,检查它的 Rect属性(property)。

关于.net - 获取 DrawingVisual 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10080817/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com