Compartir a través de


Cómo: Geometría de una prueba de seguimiento en un objeto visual

En este ejemplo se muestra cómo realizar una prueba de posicionamiento en un objeto visual compuesto de uno o más objetos Geometry.

Ejemplo

En el ejemplo siguiente se muestra cómo recuperar DrawingGroup de un objeto visual que utiliza el método GetDrawing. A continuación, se realiza una prueba de posicionamiento en el contenido representado de cada dibujo de DrawingGroup para determinar a qué geometría se obtuvo acceso.

NotaNota

En la mayoría de los casos, se utiliza el método HitTest para determinar si un punto interseca con cualquier parte del contenido representado de un objeto visual.

        ' Determine if a geometry within the visual was hit.
        Public Shared Sub HitTestGeometryInVisual(ByVal visual As Visual, ByVal pt As Point)
            ' Retrieve the group of drawings for the visual.
            Dim drawingGroup As DrawingGroup = VisualTreeHelper.GetDrawing(visual)
            EnumDrawingGroup(drawingGroup, pt)
        End Sub

        ' Enumerate the drawings in the DrawingGroup.
        Public Shared Sub EnumDrawingGroup(ByVal drawingGroup As DrawingGroup, ByVal pt As Point)
            Dim drawingCollection As DrawingCollection = drawingGroup.Children

            ' Enumerate the drawings in the DrawingCollection.
            For Each drawing As Drawing In drawingCollection
                ' If the drawing is a DrawingGroup, call the function recursively.
                If drawing.GetType() Is GetType(DrawingGroup) Then
                    EnumDrawingGroup(CType(drawing, DrawingGroup), pt)
                ElseIf drawing.GetType() Is GetType(GeometryDrawing) Then
                    ' Determine whether the hit test point falls within the geometry.
                    If (CType(drawing, GeometryDrawing)).Geometry.FillContains(pt) Then
                        ' Perform action based on hit test on geometry.
                    End If
                End If

            Next drawing
        End Sub
// Determine if a geometry within the visual was hit.
static public 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.
static public 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.
            }
        }

    }
}

El método FillContains es un método sobrecargado que permite realizar las pruebas de posicionamiento mediante un objeto Point o Geometry especificado. Si se traza una geometría, el trazo puede extenderse fuera del límite del relleno. En cuyo caso, puede que sea conveniente llamar a StrokeContains además de a FillContains.

También puede proporcionar un tipo ToleranceType que se utiliza para el aplanamiento de curvas Bézier.

NotaNota

En este ejemplo no se tiene en cuenta ninguna transformación ni recorte que se puede aplicar a la geometría.Además, este ejemplo no funcionará con un control con estilo, puesto que no tiene ningún dibujo asociado directamente a él.

Vea también

Tareas

Cómo: Realizar una prueba de posicionamiento usando Geometry como parámetro

Conceptos

Realizar pruebas de posicionamiento en la capa visual