使用区域进行剪裁

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

下图显示了剪裁的字符串。

显示显示在四面形状中的两个句子部分的插图