다음을 통해 공유


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

업데이트: 2007년 11월

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

예제

다음 예제에서는 HitTest 메서드에 대한 GeometryHitTestParameters를 사용하여 적중 테스트를 설정하는 방법을 보여 줍니다. 적중 테스트의 범위를 확장하기 위해 Geometry 개체를 만드는 데에는 OnMouseDown 메서드에 전달되는 Point 값이 사용됩니다.

// 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를 적중 테스트 매개 변수로 사용할 때 적중 테스트 콜백을 구현하는 방법을 보여 줍니다. result 매개 변수는 IntersectionDetail 속성의 값을 검색하기 위해 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;
    }
}

참고

교차 세부 사항이 Empty인 경우에는 HitTestResult 콜백을 호출하지 않아야 합니다.

참고 항목

작업

방법: 시각적 요소의 기하 도형 적중 테스트

개념

시각적 계층에서 적중 테스트