使用VisualTreeHelper.GetDrawing(Visual v)枚举所有Visual内容的对象

原文: 使用VisualTreeHelper.GetDrawing(Visual v)枚举所有Visual内容的对象

C#代码:
public void RetrieveDrawing(Visual v)
{
    DrawingGroup dGroup = VisualTreeHelper.GetDrawing(v);
    EnumDrawingGroup(dGroup);
}

public void EnumDrawingGroup(DrawingGroup drawingGroup)
{
     DrawingCollection dc = drawingGroup.Children;

     foreach (Drawing drawing in dc)
     {
         // 如果是DrawingGroup,递归枚举
         if (drawing.GetType() == typeof(DrawingGroup))
         {
             EnumDrawingGroup((DrawingGroup)drawing);
         }
         else if (drawing.GetType() == typeof(GeometryDrawing))
         {
             // 处理代理
         }
         else if (drawing.GetType() == typeof(ImageDrawing))
         {
             // 处理代理
         }
         else if (drawing.GetType() == typeof(GlyphRunDrawing))
         {
             // 处理代理
         }
         else if (drawing.GetType() == typeof(VideoDrawing))
         {
             // 处理代理
         }
     }
}
 由于代码很简单,不赘述.

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/9850169.html