2

例如,我使用此方法创建了一个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方法绘制的形状类型及其属性?

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 属性。

谢谢。

4

1 回答 1

3

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

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属性。

于 2012-04-10T08:33:57.430 回答