Microsoft Developer Network (MSDN)
This section provides information on the functions used to create, manipulate, and query GDI regions. Regions are a powerful graphics primitive that define an arbitrary area on the screen. They can be used for clipping, hit testing, and complex drawing operations.
A GDI region is an object that represents a specific area. This area can be composed of rectangles, paths, or a combination of both. Regions are defined by a set of boundaries and can be used to:
The primary structure associated with regions is the RGNDATA structure, which contains information about the region's bounding rectangle and the data defining its shape.
The following table lists some of the most commonly used GDI region functions:
| Function | Description |
|---|---|
CreateRectRgn |
Creates a GDI region that consists of a rectangle. |
CreatePolygonRgn |
Creates a GDI region that consists of a polygon. |
CreatePathRgn |
Creates a GDI region from the current path. |
CombineRgn |
Combines two regions into a third region. |
GetRegionData |
Retrieves the region data. |
PtInRegion |
Determines whether a point is within a region. |
RectInRegion |
Determines whether a rectangle intersects with a region. |
DeleteObject |
Deletes a GDI region object. |
CreateRectRgnCreates a GDI region that consists of a rectangle.
(int x1, int y1, int x2, int y2): Specifies the coordinates of the upper-left and lower-right corners of the rectangle, in logical units.HRGN) to the newly created region.NULL if an error occurs.x1, y1) define the upper-left corner, and (x2, y2) define the lower-right corner of the rectangle.DeleteObject.CreatePolygonRgnCreates a GDI region that consists of a polygon defined by an array of points.
(LPPOINT lpPoints, int nCount, int fnFillMode):
lpPoints: Pointer to an array of POINT structures that specify the vertices of the polygon.nCount: The number of points in the array.fnFillMode: The fill mode for the polygon. Can be ALTERNATE or WINDING.HRGN) to the newly created region.NULL if an error occurs.fnFillMode parameter determines how the interior of the polygon is determined.CreatePathRgnCreates a GDI region from the current path constructed using the path-drawing functions.
(int nMode): Specifies how the path data is to be converted into a region. Can be RGN_FORMAT (not recommended) or RGN_COPY. Use RGN_COPY to create a copy of the path.HRGN) to the newly created region.NULL if an error occurs.BeginPath, MoveToEx, LineTo, PolylineTo, ArcTo, etc., and before calling EndPath.CombineRgnCombines two regions into a third region. The operation can be an intersection, union, difference, or symmetrical difference.
(HRGN hRgnDest, HRGN hRgnSrc1, HRGN hRgnSrc2, int fnCombine):
hRgnDest: Handle to the destination region. This region will be modified to contain the result of the combination.hRgnSrc1: Handle to the first source region.hRgnSrc2: Handle to the second source region.fnCombine: Specifies the operation to be performed. Common values include:
RGN_AND (Intersection)RGN_OR (Union)RGN_XOR (Symmetrical Difference)RGN_DIFF (Difference: Region 1 - Region 2)RGN_MIN (Minimum rectangle)RGN_MAX (Maximum rectangle)NULLREGION, SIMPLEREGION, or COMPLEXREGION.0 if an error occurs.GetRegionDataRetrieves the dimensions and structure of a region. This function returns the region's data in a buffer.
(HRGN hRgn, DWORD dwCount, LPRGNDATA lpRgnData):
hRgn: Handle to the region.dwCount: The size, in bytes, of the buffer pointed to by lpRgnData.lpRgnData: Pointer to a RGNDATA structure that receives the region data. If this parameter is NULL, the function returns the required buffer size.lpRgnData is not NULL, the return value is the number of bytes copied to the buffer, or 0 if an error occurs.lpRgnData is NULL, the return value is the number of bytes required for the buffer, or 0 if an error occurs.PtInRegionDetermines whether a specified point lies within a region.
(HRGN hRgn, int x, int y):
hRgn: Handle to the region.x: The x-coordinate of the point.y: The y-coordinate of the point.RectInRegionDetermines whether any part of a specified rectangle lies within a region.
(HRGN hRgn, const RECT *lprc):
hRgn: Handle to the region.lprc: Pointer to a RECT structure that specifies the rectangle's coordinates.DeleteObjectDeletes a specified logical pen, brush, font, bitmap, region, or color palette. After the object is deleted, the handle to it is no longer valid.
(HGDIOBJ hObject): Handle to the GDI object to be deleted.HRGN handle.