Прочетете на английски Редактиране

Споделяне чрез


VisualTreeHelper.GetDrawing(Visual) Method

Definition

Returns the drawing content of the specified Visual.

C#
public static System.Windows.Media.DrawingGroup GetDrawing(System.Windows.Media.Visual reference);

Parameters

reference
Visual

The Visual whose drawing content is returned.

Returns

The drawing content of the Visual returned as a DrawingGroup type.

Examples

The following example shows how to retrieve the DrawingGroup from a visual object by using the GetDrawing method. A hit test is then performed on the geometry of each drawing in the DrawingGroup to determine which geometry was hit.

C#
// Determine if a geometry within the visual was hit.
public static void HitTestGeometryInVisual(Visual visual, Point pt)
{
    // Retrieve the group of drawings for the visual.
    DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(visual);
    EnumDrawingGroup(drawingGroup, pt);
}

// Enumerate the drawings in the DrawingGroup.
public static void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
{
    DrawingCollection drawingCollection = drawingGroup.Children;

    // Enumerate the drawings in the DrawingCollection.
    foreach (Drawing drawing in drawingCollection)
    {
        // If the drawing is a DrawingGroup, call the function recursively.
        if (drawing.GetType() == typeof(DrawingGroup))
        {
            EnumDrawingGroup((DrawingGroup)drawing, pt);
        }
        else if (drawing.GetType() == typeof(GeometryDrawing))
        {
            // Determine whether the hit test point falls within the geometry.
            if (((GeometryDrawing)drawing).Geometry.FillContains(pt))
            {
                // Perform action based on hit test on geometry.
            }
        }
    }
}

Applies to

Продукт Версии
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10