語言

VisualTreeHelper.HitTest 方法

定義

回傳命中測試中最 Visual 頂端的物件。

多載

備註

以參數為HitTestResultCallbackHitTestFilterCallback參數的超載方法,在命中測試中提供額外功能:

  • 讓你能參與控制視覺樹的遍歷,在命中測試時。

  • 允許你取得該點或幾何體下方的所有視覺資料,而不僅限於最頂端。

HitTest(Visual, Point)

透過指定 Visual一個 ,回傳命中測試的最高Point對象。

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

就是用來測試的點數值。

傳回

命中測試結果 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被擊中的物件。

Note

從二維視覺樹開始的命中測試有可能回傳 RayHitTestResult,該物件為三維命中測試結果物件。 從 2D 視覺樹開始的命中測試,無縫延伸到任何定義的 3D 視覺樹。

另請參閱

適用於

HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D)

在指定的 Visual3D、 HitTestFilterCallbackHitTestResultCallback 及 方法上啟動命中測試。

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。

Note

從二維視覺樹開始的命中測試有可能回傳 RayHitTestResult,該物件為三維命中測試結果物件。 從 2D 視覺樹開始的命中測試,無縫延伸到任何定義的 3D 視覺樹。

適用於

HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters)

在指定的 Visual、 HitTestFilterCallbackHitTestResultCallback 及 方法上啟動命中測試。

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。

Note

從二維視覺樹開始的命中測試有可能回傳 RayHitTestResult,該物件為三維命中測試結果物件。 從 2D 視覺樹開始的命中測試,無縫延伸到任何定義的 3D 視覺樹。

另請參閱

適用於