Découpage avec une région

L’une des propriétés de la classe Graphics est la zone de découpage. Tout dessin effectué par un objet Graphics donné est limité à la zone de découpage de cet objet Graphics . Vous pouvez définir la zone de découpage en appelant la méthode SetClip .

L’exemple suivant construit un chemin qui se compose d’un polygone unique. Ensuite, le code construit une région basée sur ce chemin. L’adresse de la région est passée à la méthode SetClip d’un objet Graphics , puis deux chaînes sont dessinées.

// 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);

L’illustration suivante montre les chaînes coupées.

illustration montrant des parties de deux phrases apparaissant dans une forme à quatre côtés