使用區域裁剪
Graphics類別的其中一個屬性是裁剪區域。 指定 Graphics 物件完成的所有繪圖都會限制為該 Graphics 物件的裁剪區域。 您可以呼叫 SetClip 方法來設定裁剪區域。
下列範例會建構由單一多邊形組成的路徑。 然後,程式碼會根據該路徑建構區域。 區域的位址會傳遞至Graphics物件的SetClip方法,然後繪製兩個字串。
// Create a path that consists of a single polygon.
Point polyPoints[] = {Point(10, 10), Point(150, 10),
Point(100, 75), Point(100, 150)};
GraphicsPath path;
path.AddPolygon(polyPoints, 4);
// Construct a region based on the path.
Region region(&path);
// Draw the outline of the region.
Pen pen(Color(255, 0, 0, 0));
graphics.DrawPath(&pen, &path);
// Set the clipping region of the Graphics object.
graphics.SetClip(®ion);
// Draw some clipped strings.
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 36, FontStyleBold, UnitPixel);
SolidBrush solidBrush(Color(255, 255, 0, 0));
graphics.DrawString(L"A Clipping Region", 20, &font,
PointF(15, 25), &solidBrush);
graphics.DrawString(L"A Clipping Region", 20, &font,
PointF(15, 68), &solidBrush);
下圖顯示裁剪的字串。