次の方法で共有


方法 : ビジュアル内のジオメトリのヒット テストを実行する

この例では、1 つ以上の Geometry オブジェクトで構成されるビジュアル オブジェクトに対してヒット テストを実行する方法を示します。

使用例

GetDrawing メソッドを使用して、ビジュアル オブジェクトから DrawingGroup を取得する方法を次の例に示します。 次に、DrawingGroup の各描画のレンダリングされたコンテンツに対してヒット テストを実行し、ヒットしたジオメトリを確認します。

メモメモ

通常、ポイントがビジュアルのレンダリングされたコンテンツと交差するかどうかを判定するには、HitTest メソッドを使用します。

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

    }
}

FillContains メソッドはオーバーロードされたメソッドであり、これを使用すると、指定した Point または Geometry を使用してヒット テストを行うことができます。 ジオメトリに線が付いている場合は、塗りつぶしの境界の外側までストロークを延長できます。 この場合は、FillContains に加えて StrokeContains を呼び出すことができます。

ベジエ平坦化に使用する ToleranceType を提供することもできます。

メモメモ

このサンプルでは、ジオメトリに適用される可能性のある変換またはクリッピングは考慮していません。また、スタイルが設定されたコントロールには直接関連付けられた描画がないので、このサンプルはスタイルが設定されたコントロールに対しては機能しません。

参照

処理手順

方法 : パラメーターとしてジオメトリを使用してヒット テストを実行する

概念

ビジュアル層でのヒット テスト