방법: 영역에 적중 테스트 사용

적중 횟수 테스트의 목적은 커서가 아이콘 또는 단추와 같은 지정된 개체 위에 있는지 여부를 확인하는 것입니다.

예제

다음 예제에서는 두 사각형 영역의 공용 구조체를 형성하여 더하기 모양의 영역을 만듭니다. point 변수는 가장 최근의 클릭 위치를 보유한다고 가정합니다. 코드는 point가 더하기 모양의 영역에 있는지 여부를 확인합니다. 점이 영역(적중)에 있는 경우 영역은 불투명한 빨간색 브러시로 채워집니다. 그렇지 않으면 영역이 반투명 빨간색 브러시로 채워집니다.

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);
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)

코드 컴파일

앞의 예제는 Windows Forms에서 사용하도록 설계되었으며 PaintEventHandler의 매개 변수인 PaintEventArgse가 필요합니다.

참고 항목