Share via


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

업데이트: 2007년 11월

이 예제에서는 하나 이상의 Geometry 개체로 구성된 시각적 개체에 대해 적중 테스트를 수행하는 방법을 보여 줍니다.

예제

다음 예제에서는 GetDrawing 메서드를 사용하는 시각적 개체에서 DrawingGroup을 검색하는 방법을 보여 줍니다. 그런 다음 DrawingGroup에 있는 각 그리기의 렌더링된 콘텐츠에 대해 적중 테스트를 수행하여 적중된 기하 도형을 확인합니다.

참고

대개의 경우 HitTest 메서드를 사용하여 점이 시각적 요소의 렌더링된 콘텐츠와 교차하는지 여부를 확인합니다.

// Determine if a geometry within the visual was hit.
static public void HitTestGeometryInVisual(Visual visual, Point pt)
{
    // Retrieve the group of drawings for the visual.
    DrawingGroup drawingGroup = VisualTreeHelper.GetDrawing(visual);
    EnumDrawingGroup(drawingGroup, pt);
}

// Enumerate the drawings in the DrawingGroup.
static public void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
{
    DrawingCollection drawingCollection = drawingGroup.Children;

    // Enumerate the drawings in the DrawingCollection.
    foreach (Drawing drawing in drawingCollection)
    {
        // If the drawing is a DrawingGroup, call the function recursively.
        if (drawing.GetType() == typeof(DrawingGroup))
        {
            EnumDrawingGroup((DrawingGroup)drawing, pt);
        }
        else if (drawing.GetType() == typeof(GeometryDrawing))
        {
            // Determine whether the hit test point falls within the geometry.
            if (((GeometryDrawing)drawing).Geometry.FillContains(pt))
            {
                // Perform action based on hit test on geometry.
            }
        }

    }
}

FillContains 메서드는 지정된 Point 또는 Geometry를 사용하여 적중 테스트를 수행할 수 있게 해 주는 오버로드된 메서드입니다. 기하 도형이 스트로크되는 경우 스크로크는 채우기 경계 밖으로 확장될 수 있습니다. 경우에 따라 FillContains와 함께 StrokeContains를 호출할 수 있습니다.

또한 3차원 펴기의 목적으로 사용되는 ToleranceType을 제공할 수도 있습니다.

참고

이 샘플에서는 기하 도형에 적용될 수 있는 모든 변환이나 클리핑을 고려하지 않습니다. 또한 직접 연결된 그리기가 없기 때문에 이 샘플은 스타일이 적용된 컨트롤에서는 작동하지 않습니다.

참고 항목

작업

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

개념

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