How to create a CRgn composed of lines and arcs
To create a CRgn composed of lines and arcs, you can follow these steps:
- Define the points and radii for the arcs and lines that you want to include in the region.
- Create a CRgn object to store the region data.
- Use the CRgn::CreatePolygonRgn function to create a polygonal region that approximates the curves you want to include in the region.
- Use the CRgn::CombineRgn function to combine the polygonal region with the arcs and lines you want to add to the region.
- Use the CRgn::DeleteObject function to free the memory used by the CRgn object.
Here is an example of code that creates a CRgn composed of a rectangle with rounded corners:
// Define the points and radii for the rounded corners
CPoint topLeft(10, 10);
CPoint topRight(90, 10);
CPoint bottomRight(90, 90);
CPoint bottomLeft(10, 90);
int radius = 10;
// Create a CRgn object to store the region data
CRgn region;
// Create a polygonal region that approximates the rounded corners
POINT points[8];
points[0] = topLeft;
points[1] = CPoint(topRight.x - radius, topRight.y);
points[2] = CPoint(topRight.x, topRight.y + radius);
points[3] = bottomRight;
points[4] = CPoint(bottomRight.x, bottomRight.y - radius);
points[5] = CPoint(bottomLeft.x + radius, bottomLeft.y);
points[6] = bottomLeft;
points[7] = CPoint(topLeft.x, topLeft.y + radius);
region.CreatePolygonRgn(points, 8, ALTERNATE);
// Add the arcs for the rounded corners to the region
CRgn arc;
arc.CreateEllipticRgn(topLeft.x, topLeft.y, topLeft.x + radius * 2, topLeft.y + radius * 2);
region.CombineRgn(&arc, ®ion, RGN_OR);
arc.DeleteObject();
arc.CreateEllipticRgn(topRight.x - radius * 2, topRight.y, topRight.x, topRight.y + radius * 2);
region.CombineRgn(&arc, ®ion, RGN_OR);
arc.DeleteObject();
arc.CreateEllipticRgn(bottomRight.x - radius * 2, bottomRight.y - radius * 2, bottomRight.x, bottomRight.y);
region.CombineRgn(&arc, ®ion, RGN_OR);
arc.DeleteObject();
arc.CreateEllipticRgn(bottomLeft.x, bottomLeft.y - radius * 2, bottomLeft.x + radius * 2, bottomLeft.y);
region.CombineRgn(&arc, ®ion, RGN_OR);
arc.DeleteObject();
// Use the region to draw a rectangle with rounded corners
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CBrush brush(RGB(255, 255, 0));
dc.SelectObject(&pen);
dc.SelectObject(&brush);
dc.BeginPath();
dc.RoundRect(10, 10, 90, 90, 10, 10);
dc.EndPath();
dc.SelectClipRgn(®ion);
dc.FillPath();
dc.StrokePath();
dc.SelectClipRgn(NULL);
// Free the memory used by the CRgn object
region.DeleteObject();
``
原文地址: http://www.cveoy.top/t/topic/dOWt 著作权归作者所有。请勿转载和采集!