다음을 통해 공유


방법: 영역을 사용하여 적중 테스트

커서가 아이콘이나 단추 같은 특정 개체 위에 있는지 여부를 알기 위해 적중 테스트를 수행합니다.

예제

아래 예제에서는 두 개의 사각형 영역을 결합하여 더하기 기호 모양의 영역을 만듭니다. point 변수에는 가장 최근에 클릭한 위치가 포함된다고 가정합니다. 코드를 통해 point 변수의 값이 더하기 기호 모양의 영역에 있는지 여부를 확인하고 point 변수의 값이 영역에 있으면, 즉 적중 테스트 결과가 참이면 빨간색 불투명 브러시를 사용하여 영역을 채웁니다. 그렇지 않으면 빨간색 반투명 브러시를 사용하여 영역을 채웁니다.

        Dim point As New Point(60, 10)

        ' Assume that the variable "point" contains the location of the
        ' most recent mouse click.
        ' To simulate a hit, assign (60, 10) to point.
        ' To simulate a miss, assign (0, 0) to point.

        Dim solidBrush As New SolidBrush(Color.Black)
        Dim region1 As New [Region](New Rectangle(50, 0, 50, 150))
        Dim region2 As New [Region](New Rectangle(0, 50, 150, 50))

        ' Create a plus-shaped region by forming the union of region1 and region2.
        ' The union replaces region1.
        region1.Union(region2)

        If region1.IsVisible(point, e.Graphics) Then
            ' The point is in the region. Use an opaque brush.
            solidBrush.Color = Color.FromArgb(255, 255, 0, 0)
        Else
            ' The point is not in the region. Use a semitransparent brush.
            solidBrush.Color = Color.FromArgb(64, 255, 0, 0)
        End If

        e.Graphics.FillRegion(solidBrush, region1)

Point point = new Point(60, 10);

// Assume that the variable "point" contains the location of the
// most recent mouse click.
// To simulate a hit, assign (60, 10) to point.
// To simulate a miss, assign (0, 0) to point.

SolidBrush solidBrush = new SolidBrush(Color.Black);
Region region1 = new Region(new Rectangle(50, 0, 50, 150));
Region region2 = new Region(new Rectangle(0, 50, 150, 50));

// Create a plus-shaped region by forming the union of region1 and 
// region2.
// The union replaces region1.
region1.Union(region2);

if (region1.IsVisible(point, e.Graphics))
{
    // The point is in the region. Use an opaque brush.
    solidBrush.Color = Color.FromArgb(255, 255, 0, 0);
}
else
{
    // The point is not in the region. Use a semitransparent brush.
    solidBrush.Color = Color.FromArgb(64, 255, 0, 0);
}

e.Graphics.FillRegion(solidBrush, region1);

코드 컴파일

앞의 예제는 Windows Forms에서 사용해야 하며 PaintEventHandler의 매개 변수인 PaintEventArgs e를 필요로 합니다.

참고 항목

작업

방법: 영역을 사용하여 클리핑

참조

Region

개념

GDI+의 영역