Condividi tramite


Procedura: eseguire un hit test della geometria in un oggetto Visual

In questo esempio viene mostrato come eseguire un hit test in un oggetto Visual composto da uno o più oggetti Geometry.

Esempio

Nell'esempio seguente viene mostrato come recuperare l'oggetto DrawingGroup da un oggetto Visual che utilizza il metodo GetDrawing. Viene quindi eseguito un hit test sul contenuto di rendering di ciascun disegno nell'oggetto DrawingGroup per determinare la geometria raggiunta.

NotaNota

Nella maggior parte dei casi è preferibile utilizzare il metodo HitTest per determinare se un punto interseca un contenuto di un oggetto Visual di cui si è eseguito il rendering.

        ' 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.
            }
        }

    }
}

Il metodo FillContains è un metodo di overload che consente di eseguire l'hit test utilizzando un oggetto Point o Geometry specificato. Se viene tracciata una geometria, il tratto può estendersi oltre i limiti del riempimento. In tal caso, è necessario chiamare StrokeContains oltre a FillContains.

Inoltre, è possibile fornire un oggetto ToleranceType utilizzato per la linearizzazione di Bezier.

NotaNota

In questo esempio non vengono presi in considerazione tutte le trasformazioni o i ritagli che possono essere applicati alla geometria.Inoltre, questo esempio non è valido per un controllo a cui sono stati applicati degli stili, poiché non dispone di disegni associati direttamente.

Vedere anche

Attività

Procedura: eseguire un hit test utilizzando la geometria come parametro

Concetti

Hit testing a livello visivo