VisualTreeHelper.HitTest 方法

定義

傳回點擊測試最上層的 Visual 物件。

多載

HitTest(Visual, Point)

藉由指定 Point,傳回點擊測試最上層的 Visual 物件。

HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D)

利用呼叫端定義的 HitTestFilterCallbackHitTestResultCallback 方法,在指定的 Visual3D 上起始點擊測試。

HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters)

利用呼叫端定義的 HitTestFilterCallbackHitTestResultCallback 方法,在指定的 Visual 上起始點擊測試。

備註

採用 HitTestResultCallbackHitTestFilterCallback 作為參數的多載方法會在點擊測試期間提供額外的功能:

  • 可讓您參與在點擊測試期間控制視覺化樹狀結構周遊。

  • 可讓您擷取點或幾何下的所有視覺效果,而不只是最上層視覺效果。

HitTest(Visual, Point)

藉由指定 Point,傳回點擊測試最上層的 Visual 物件。

public:
 static System::Windows::Media::HitTestResult ^ HitTest(System::Windows::Media::Visual ^ reference, System::Windows::Point point);
public static System.Windows.Media.HitTestResult HitTest (System.Windows.Media.Visual reference, System.Windows.Point point);
static member HitTest : System.Windows.Media.Visual * System.Windows.Point -> System.Windows.Media.HitTestResult
Public Shared Function HitTest (reference As Visual, point As Point) As HitTestResult

參數

reference
Visual

要進行點擊測試的 Visual

point
Point

要進行點擊測試的點值。

傳回

HitTestResult

Visual 的點擊測試結果,作為 HitTestResult 類型傳回。

範例

下列範例示範如何使用 HitTest 方法來點擊測試 中的 Canvas 物件。

// Respond to the left mouse button down event by initiating the hit test.
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // Perform the hit test against a given portion of the visual object tree.
    HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);

    if (result != null)
    {
        // Perform action on hit visual object.
    }
}
' Respond to the left mouse button down event by initiating the hit test.
Private Overloads Sub OnMouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
    ' Retrieve the coordinate of the mouse position.
    Dim pt As Point = e.GetPosition(CType(sender, UIElement))

    ' Perform the hit test against a given portion of the visual object tree.
    Dim result As HitTestResult = VisualTreeHelper.HitTest(myCanvas, pt)

    If result IsNot Nothing Then
        ' Perform action on hit visual object.
    End If
End Sub

備註

VisualHit 回值的 屬性代表已叫用 Visual 的物件。

注意

從 2D 視覺化樹狀結構開始的點擊測試可能會傳回 RayHitTestResult ,這是 3D 點擊測試結果物件。 從 2D 視覺化樹狀結構開始的點擊測試會順暢地延伸至任何已定義的 3D 視覺化樹狀結構。

另請參閱

適用於

HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D)

利用呼叫端定義的 HitTestFilterCallbackHitTestResultCallback 方法,在指定的 Visual3D 上起始點擊測試。

public:
 static void HitTest(System::Windows::Media::Media3D::Visual3D ^ reference, System::Windows::Media::HitTestFilterCallback ^ filterCallback, System::Windows::Media::HitTestResultCallback ^ resultCallback, System::Windows::Media::Media3D::HitTestParameters3D ^ hitTestParameters);
public static void HitTest (System.Windows.Media.Media3D.Visual3D reference, System.Windows.Media.HitTestFilterCallback filterCallback, System.Windows.Media.HitTestResultCallback resultCallback, System.Windows.Media.Media3D.HitTestParameters3D hitTestParameters);
static member HitTest : System.Windows.Media.Media3D.Visual3D * System.Windows.Media.HitTestFilterCallback * System.Windows.Media.HitTestResultCallback * System.Windows.Media.Media3D.HitTestParameters3D -> unit
Public Shared Sub HitTest (reference As Visual3D, filterCallback As HitTestFilterCallback, resultCallback As HitTestResultCallback, hitTestParameters As HitTestParameters3D)

參數

reference
Visual3D

要進行點擊測試的 Visual3D

filterCallback
HitTestFilterCallback

代表點擊測試篩選回呼值的方法。

resultCallback
HitTestResultCallback

代表點擊測試結果回呼值的方法。

hitTestParameters
HitTestParameters3D

要進行點擊測試的 3D 參數值。

備註

參數 filterCallback 可以是 null ,在此情況下會忽略它。 如果 filterCallback 不是 null ,則會在 之前 resultCallback 叫用它。 resultCallback 不可以是 null

注意

從 2D 視覺化樹狀結構開始的點擊測試可能會傳回 RayHitTestResult ,這是 3D 點擊測試結果物件。 從 2D 視覺化樹狀結構開始的點擊測試會順暢地延伸至任何已定義的 3D 視覺化樹狀結構。

適用於

HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters)

利用呼叫端定義的 HitTestFilterCallbackHitTestResultCallback 方法,在指定的 Visual 上起始點擊測試。

public:
 static void HitTest(System::Windows::Media::Visual ^ reference, System::Windows::Media::HitTestFilterCallback ^ filterCallback, System::Windows::Media::HitTestResultCallback ^ resultCallback, System::Windows::Media::HitTestParameters ^ hitTestParameters);
public static void HitTest (System.Windows.Media.Visual reference, System.Windows.Media.HitTestFilterCallback filterCallback, System.Windows.Media.HitTestResultCallback resultCallback, System.Windows.Media.HitTestParameters hitTestParameters);
static member HitTest : System.Windows.Media.Visual * System.Windows.Media.HitTestFilterCallback * System.Windows.Media.HitTestResultCallback * System.Windows.Media.HitTestParameters -> unit
Public Shared Sub HitTest (reference As Visual, filterCallback As HitTestFilterCallback, resultCallback As HitTestResultCallback, hitTestParameters As HitTestParameters)

參數

reference
Visual

要進行點擊測試的 Visual

filterCallback
HitTestFilterCallback

代表點擊測試篩選回呼值的方法。

resultCallback
HitTestResultCallback

代表點擊測試結果回呼值的方法。

hitTestParameters
HitTestParameters

要進行點擊測試的參數值。

範例

下列範例示範如何使用 HitTest 方法來點擊測試 中的 Canvas 物件。

// Respond to the right mouse button down event by setting up a hit test results callback.
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // 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(myCanvas, null,
        new HitTestResultCallback(MyHitTestResult),
        new PointHitTestParameters(pt));

    // Perform actions on the hit test results list.
    if (hitResultsList.Count > 0)
    {
        Console.WriteLine("Number of Visuals Hit: " + hitResultsList.Count);
    }
}
' Respond to the right mouse button down event by setting up a hit test results callback.
Private Overloads Sub OnMouseRightButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
    ' Retrieve the coordinate of the mouse position.
    Dim pt As Point = e.GetPosition(CType(sender, UIElement))

    ' 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(myCanvas, Nothing, New HitTestResultCallback(AddressOf MyHitTestResult), New PointHitTestParameters(pt))

    ' Perform actions on the hit test results list.
    If hitResultsList.Count > 0 Then
        Console.WriteLine("Number of Visuals Hit: " & hitResultsList.Count)
    End If
End Sub
// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
    // Add the hit test result to the list that will be processed after the enumeration.
    hitResultsList.Add(result.VisualHit);

    // Set the behavior to return visuals at all z-order levels.
    return HitTestResultBehavior.Continue;
}
' Return the result of the hit test to the callback.
Public Function MyHitTestResult(ByVal result As HitTestResult) As HitTestResultBehavior
    ' Add the hit test result to the list that will be processed after the enumeration.
    hitResultsList.Add(result.VisualHit)

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

備註

參數 filterCallback 可以是 null ,在此情況下會予以忽略。 如果 filterCallback 不是 null ,則會在 之前 resultCallback 叫用它。 resultCallback 不可以是 null

注意

從 2D 視覺化樹狀結構開始的點擊測試可能會傳回 RayHitTestResult ,這是 3D 點擊測試結果物件。 從 2D 視覺化樹狀結構開始的點擊測試會順暢地延伸至任何已定義的 3D 視覺化樹狀結構。

另請參閱

適用於