방법: 기하 도형을 매개 변수로 사용하여 적중 테스트

이 예제에서는 Geometry를 적중 테스트 매개 변수로 사용하여 시각적 개체에 대해 적중 테스트를 수행하는 방법을 보여 줍니다.

예제

다음 예제에서는 사용 하 여 적중된 테스트를 설정 하는 방법을 보여 줍니다 GeometryHitTestParameters 에 대 한는 HitTest 메서드. Point 에 전달 되는 값을 OnMouseDown 메서드는 만드는 데는 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

IntersectionDetail 속성을 GeometryHitTestResult 사용 하는 적중된 테스트의 결과 대 한 정보를 제공는 Geometry 를 적중 테스트 매개 변수로 합니다. 다음 그림에서는 적중 테스트 기하 도형(파란색 원)과 대상 시각적 개체의 렌더링된 콘텐츠(빨간색 사각형) 간 관계를 보여 줍니다.

적중 테스트에 사용되는 IntersectionDetail을 표시하는 다이어그램.

다음 예제에서는 Geometry가 적중 테스트 매개 변수로 사용될 때 적중 테스트 콜백을 구현하는 방법을 보여 줍니다. IntersectionDetail 속성의 값을 가져오기 위해 result 매개 변수가 GeometryHitTestResult로 캐스팅됩니다. 이 속성 값을 사용하여 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 콜백을 호출하면 안 됩니다.

참고 항목