HOW TO:使用區域的使用點擊測試
更新:2007 年 11 月
點擊測試的目的是要決定游標是否已移到指定的物件上方,例如圖示或按鈕。
範例
下列範例會藉由形成兩個矩形區域的聯集,來建立加號形狀的區域。假設變數 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 Form 搭配使用而設計的,而且它需要 PaintEventArgs e (即 PaintEventHandler 的參數)。