Aracılığıyla paylaş


Nasıl yapılır: Görselde Tıklama Testi Geometrisi

Bu örnek, bir veya daha fazla Geometry nesneden oluşan bir görsel nesne üzerinde isabet testi gerçekleştirmeyi gösterir.

Örnek

Aşağıdaki örnek, yöntemini kullanan GetDrawing bir görsel nesneden öğesinin nasıl alındığını DrawingGroup gösterir. Daha sonra içindeki her çizimin DrawingGroup işlenmiş içeriğinde hangi geometrinin isabetlendiğini belirlemek için bir isabet testi gerçekleştirilir.

Dekont

Çoğu durumda, bir noktanın görselin HitTest işlenmiş içeriğiyle kesişip kesişmediğini belirlemek için yöntemini kullanırsınız.

// 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

FillContains yöntemi, belirtilen Point veya Geometrykullanarak teste ulaşmanızı sağlayan aşırı yüklenmiş bir yöntemdir. Geometri konturlanmışsa, vuruş dolgu sınırlarının dışına yayılabilir. Bu durumda, öğesine ek olarak FillContainsçağrısı StrokeContains yapmak isteyebilirsiniz.

Bezier düzleştirme amacıyla kullanılan bir ToleranceType de sağlayabilirsiniz.

Dekont

Bu örnek, geometriye uygulanabilecek dönüştürmeleri veya kırpmaları hesaba katmıyor. Ayrıca bu örnek, doğrudan kendisiyle ilişkilendirilmiş çizimleri olmadığından stil denetimle çalışmaz.

Ayrıca bkz.