次の方法で共有


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

この例では、Geometry をヒット テスト パラメーターとして使用して、ビジュアル オブジェクトに対してヒット テストを実行する方法を示します。

使用例

HitTest メソッドの GeometryHitTestParameters を使用してヒット テストを設定する方法を次の例に示します。 OnMouseDown メソッドに渡される Point 値は、Geometry オブジェクトを作成してヒット テストの範囲を拡張する場合に使用されます。

        ' Respond to the mouse button down event by setting up a hit test results callback.
        Private Overloads Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
            ' Retrieve the coordinate of the mouse position.
            Dim pt As Point = e.GetPosition(CType(sender, UIElement))

            ' Expand the hit test area by creating a geometry centered on the hit test point.
            Dim expandedHitTestArea As New EllipseGeometry(pt, 10.0, 10.0)

            ' Clear the contents of the list used for hit test results.
            hitResultsList.Clear()

            ' Set up a callback to receive the hit test result enumeration.
            VisualTreeHelper.HitTest(myControl, Nothing, New HitTestResultCallback(AddressOf MyHitTestResultCallback), New GeometryHitTestParameters(expandedHitTestArea))

            ' Perform actions on the hit test results list.
            If hitResultsList.Count > 0 Then
                ProcessHitTestResultsList()
            End If
        End Sub
// Respond to the mouse button down event by setting up a hit test results callback.
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // Expand the hit test area by creating a geometry centered on the hit test point.
    EllipseGeometry expandedHitTestArea = new EllipseGeometry(pt, 10.0, 10.0);

    // Clear the contents of the list used for hit test results.
    hitResultsList.Clear();

    // Set up a callback to receive the hit test result enumeration.
    VisualTreeHelper.HitTest(myControl, null,
        new HitTestResultCallback(MyHitTestResultCallback),
        new GeometryHitTestParameters(expandedHitTestArea));

    // Perform actions on the hit test results list.
    if (hitResultsList.Count > 0)
    {
        ProcessHitTestResultsList();
    }
}

GeometryHitTestResultIntersectionDetail プロパティは、Geometry をヒット テスト パラメーターとして使用したヒット テストの実行結果に関する情報を提供します。 ヒット テストのジオメトリ (青い円) と対象のビジュアル オブジェクト (赤い正方形) の描画されるコンテンツとの関係を次の図に示します。

ヒット テストのジオメトリと対象のビジュアル オブジェクトの交差部分

ヒット テストで使用される IntersectionDetail のダイアグラム

Geometry をヒット テスト パラメーターとして使用する場合に、ヒット テストのコールバックを実装する方法を次の例に示します。 IntersectionDetail プロパティの値を取得するために、result パラメーターを GeometryHitTestResult にキャストしています。 このプロパティ値を使用して、Geometry ヒット テスト パラメーターの全体または一部がヒット テスト対象の描画されるコンテンツ内に含まれるかどうかを判別することができます。 ここに示すサンプル コードでは、完全に対象の境界内に含まれるビジュアルについてのみ、ヒット テストの結果をリストに追加しています。

        ' Return the result of the hit test to the callback.
        Public Function MyHitTestResultCallback(ByVal result As HitTestResult) As HitTestResultBehavior
            ' Retrieve the results of the hit test.
            Dim intersectionDetail As IntersectionDetail = (CType(result, GeometryHitTestResult)).IntersectionDetail

            Select Case intersectionDetail
                Case IntersectionDetail.FullyContains

                    ' Add the hit test result to the list that will be processed after the enumeration.
                    hitResultsList.Add(result.VisualHit)

                    Return HitTestResultBehavior.Continue

                Case IntersectionDetail.Intersects

                    ' Set the behavior to return visuals at all z-order levels.
                    Return HitTestResultBehavior.Continue

                Case IntersectionDetail.FullyInside

                    ' Set the behavior to return visuals at all z-order levels.
                    Return HitTestResultBehavior.Continue

                Case Else
                    Return HitTestResultBehavior.Stop
            End Select
        End Function
// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResultCallback(HitTestResult result)
{
    // Retrieve the results of the hit test.
    IntersectionDetail intersectionDetail = ((GeometryHitTestResult)result).IntersectionDetail;

    switch (intersectionDetail)
    {
        case IntersectionDetail.FullyContains:

            // Add the hit test result to the list that will be processed after the enumeration.
            hitResultsList.Add(result.VisualHit);

            return HitTestResultBehavior.Continue;

        case IntersectionDetail.Intersects:

            // Set the behavior to return visuals at all z-order levels.
            return HitTestResultBehavior.Continue;

        case IntersectionDetail.FullyInside:

            // Set the behavior to return visuals at all z-order levels.
            return HitTestResultBehavior.Continue;

        default:
            return HitTestResultBehavior.Stop;
    }
}
メモメモ

IntersectionDetail が Empty の場合は、HitTestResult コールバックを呼び出さないようにしてください。

参照

処理手順

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

概念

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