Jak przeprowadzić test trafienia geometrii w Visual

W tym przykładzie pokazano, jak wykonać test trafienia dla obiektu wizualnego składającego się z co najmniej jednego Geometry obiektu.

Przykład

W poniższym przykładzie pokazano, jak pobrać element DrawingGroup z obiektu wizualizacji, który używa GetDrawing metody . Następnie test trafienia jest wykonywany na renderowanej zawartości każdego rysunku w obiekcie DrawingGroup , aby określić, która geometria została osiągnięta.

Uwaga

W większości przypadków należy użyć HitTest metody , aby określić, czy punkt przecina dowolną z renderowanej zawartości wizualizacji.

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

Metoda FillContains jest przeciążona metodą, która umożliwia trafienie testu przy użyciu określonej Point metody lub Geometry. Jeśli geometria jest pociągnięta, pociągnięcie może rozciągać się poza granice wypełnienia. W tym przypadku możesz wywołać metodę StrokeContains oprócz FillContainsfunkcji .

Można również podać element ToleranceType używany do celów spłaszczania Beziera.

Uwaga

Ten przykład nie uwzględnia żadnych przekształceń ani wycinków, które mogą być stosowane do geometrii. Ponadto ten przykład nie będzie działać ze stylizowanym formantem, ponieważ nie ma żadnych rysunków bezpośrednio skojarzonych z nim.

Zobacz też