命中测试的目的是确定光标位于给定对象上,例如图标还是按钮。
示例:
以下示例通过形成两个矩形区域的并集来创建加形区域。 假设变量 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 窗体,它需要 PaintEventArgse
,这是 PaintEventHandler 的参数。