2012-04-09 42 views
2

我有我有,例如使用此方法創建一個DrawingVisual對象:獲取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及其性質得出的類型?

即:

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

謝謝。

回答

3

您可以通過在由DrawingVisual的Drawing財產,如果一個孩子繪畫是GeometryDrawing,檢查其PenBrushGeometry屬性中提供的DrawingGroupDrawing對象recursivly迭代:

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

+0

你能把這個翻譯成VB嗎? – Clemens 2012-04-11 07:21:10

+0

是的,我完全沒有問題。到現在爲止,我使用了一個繼承自DrawingVisual的類,並添加了我需要的屬性。今天我會嘗試你的答案,我會給你我的迴應。 – gliderkite 2012-04-12 10:20:58