如何:将几何图形用作参数的命中测试

此示例演示如何在将 Geometry 用作命中测试参数的情况下,针对视觉对象执行命中测试。

示例

以下示例显示如何使用 HitTest 方法的 GeometryHitTestParameters 设置命中测试。 传递给 OnMouseDown 方法的 Point 值用于创建一个 Geometry 对象,以扩展命中测试的范围。

// 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();
    }
}
' 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

GeometryHitTestResultIntersectionDetail 属性提供有关使用 Geometry 作为命中测试参数的命中测试结果的信息。 下图演示了命中测试几何图形(蓝色圆圈)与目标视觉对象(红色正方形)的呈现内容之间的关系。

Diagram that shows IntersectionDetail used in hit testing.

以下示例演示如何在将 Geometry 用作命中测试参数时,实现命中测试回叫。 将 result 参数强制转换为 GeometryHitTestResult 以检索 IntersectionDetail 属性的值。 使用属性值,可以确定 Geometry 命中测试参数是完全还是部分包含在命中测试目标的呈现内容中。 在本示例中,示例代码仅将命中测试结果添加到完全包含在目标边界中的视觉对象的列表中。

// 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;
    }
}
' 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

注意

当交集详细信息为 Empty 时,不应调用 HitTestResult 回叫。

另请参阅