使用區域點擊測試

點擊測試的目的是要判斷游標是否位於指定的物件上,例如圖示或按鈕。 下列範例會藉由形成兩個矩形區域的聯集來建立加號形區域。 假設變數 會保留最近按一下的位置。 程式碼會檢查 是否位於加號區域。 如果 位於區域中 (點擊) ,則區域會填滿不透明的紅色筆刷。 否則,區域會填入半透明紅色筆刷。

Point point(60, 10);
// Assume that the variable "point" contains the location
// of the most recent click.
// To simulate a hit, assign (60, 10) to point.
// To simulate a miss, assign (0, 0) to point.
SolidBrush solidBrush(Color());
Region region1(Rect(50, 0, 50, 150));
Region region2(Rect(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, &graphics))
{
   // The point is in the region. Use an opaque brush.
   solidBrush.SetColor(Color(255, 255, 0, 0));
}
else
{
   // The point is not in the region. Use a semitransparent brush.
   solidBrush.SetColor(Color(64, 255, 0, 0));
}
graphics.FillRegion(&solidBrush, &region1);