領域を使用したクリッピング

Graphics クラスのプロパティの 1 つは、クリッピング領域です。 特定の Graphics オブジェクトによって実行されるすべての描画は、その Graphics オブジェクトのクリッピング領域に制限されます。 クリッピング領域を設定するには、 SetClip メソッドを呼び出します。

次の例では、1 つの多角形で構成されたパスを構築します。 次に、コードは、そのパスに基づいてリージョンを構築します。 領域のアドレスが Graphics オブジェクトの SetClip メソッドに渡され、2 つの文字列が描画されます。

// 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(&region);
// 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);

次の図は、クリップされた文字列を示しています。

4 辺の図形内に 2 つの文の一部が表示されている図