Click here to Skip to main content
13,453,939 members (44,088 online)
Click here to Skip to main content
Add your own
alternative version


130 bookmarked
Posted 10 Mar 2002

Guide to WIN32 Regions

, 10 Mar 2002
Rate this:
Please Sign up or sign in to vote.
Guide to understanding how to create and use regions with the WIN32 SDK
<!-- Download Links --> <!-- Article image -->

Sample Image - preview.gif


Regions are a resource in windows that are very useful. They are device independent, which means that a device context (DC) is not need to create or use one. However, there are many functions in the WIN32 GDI that require a region.

Some of the useful places for a region are:

  • Update Region, Paint
  • Clipping, Paint
  • Hit Testing
  • Define window shape, Irregular Shaped Windows

Many of the functions that relate to regions are very simple, and need very little explanation, in fact the definition from MSDN is adequate enough to explain these functions. However, some of the functions are a little more difficult to understand and master, therefore extra details have been added to explain these functions. A few diagrams may accompany the explanation as well.

Region Creation

There are a number of ways to create a region. There are a number of functions that can be used create a region, from a simple shape such as a rectangle or an ellipse, to a complicated shape such as the outline of a string of text. Listed below is a table that describes each of the creation functions.

CreateEllipticRgnCreates an elliptical region. As with drawing ellipses, elliptic regions are very large, and may slow your program down if you use a large number of them.
CreateEllipticRgnIndirectCreates an elliptical region from a RECT structure. As with drawing ellipses, elliptic regions are very large, and may slow your program down if you use a large number of them.
CreatePolygonRgnCreates a polygonal region.
CreatePolyPolygonRgnCreates a region consisting of a series of polygons.
CreateRectRgnCreates a rectangular region.
CreateRectRgnIndirectCreates a rectangular region from a RECT structure.
CreateRoundRectRgnCreates a rectangular region with rounded corners.
ExtCreateRegionCreates a region from the specified region and transformation data.
PathToRegionCreates a region from a WIN32 Path created in a DC.


There are a few operations that can be performed on a region in order to query, modify test its data.


After a few basic regions are created, it is possible to perform boolean operations on the regions in order to create more complex regions with CombineRgn. This function has five operations that can be performed in order to combine two different regions. Shown below is an example of each of the boolean operations that can be performed in CombineRgn:

RGN_ANDCreates the intersection of the two combined regions.
CombineRgn(hrgnDest, hrgnSrc1, hrgnSrc2, RGN_AND);
RGN_COPYCreates a copy of the region identified by hrgnSrc1.
CombineRgn(hrgnDest, hrgnSrc1, NULL, RGN_COPY);
No Image
RGN_DIFFCombines the parts of hrgnSrc1 that are not part of hrgnSrc2.
CombineRgn(hrgnDest, hrgnSrc1, hrgnSrc2, RGN_DIFF);
RGN_ORCreates the union of two combined regions.
CombineRgn(hrgnDest, hrgnSrc1, hrgnSrc2, RGN_OR);
RGN_XORCreates the union of two combined regions except for any overlapping areas.
CombineRgn(hrgnDest, hrgnSrc1, hrgnSrc2, RGN_XOR);

One more thing to point out is that the destination region in CombineRgn can be one of the source regions. Therefore, if you continually modify a region by adding or subtracting a different region from it, you can perform one combine operation rather than worrying about copying the adjusted region. Here is an example:

HRGN hCacheRgn;
HRGN hUpdateRgn;


//C: Convoluted way.
//C: Initialize a temporary region.
HRGN hTemp = ::CreateRectRgn(0,0,0,0);
//C: Subtract teh update region from the Cache region. 
::CombineRgn(hTemp, hCacheRgn, hUpdateRgn, RGN_DIFF);
//C: Copy the Temp region back into the Cache region.
::CombineRgn(hCacheRgn, hTemp, NULL, RGN_COPY);
//C: Destroy the Temp region.

//C: Simple way
::CombineRgn(hCacheRgn, hCacheRgn, hUpdateRgn, RGN_DIFF);


The EqualRgn function checks the two specified regions to determine whether they are identical. The function considers two regions identical if they are equal in size and shape.

BOOL EqualRgn(
  HRGN hSrcRgn1,  // handle to first region
  HRGN hSrcRgn2   // handle to second region


The PtInRegion function determines whether the specified point is inside the specified region. This function is very useful for creating a hit-test region on your control. Such as a bitmap that has a hot-region that can be clicked by the user.

BOOL PtInRegion(
  HRGN hrgn,  // handle to region
  int X,      // x-coordinate of point
  int Y       // y-coordinate of point


The RectInRegion function is very similar to PtInRegion, except that it will determines whether any part of the specified rectangle is within the boundaries of a region.

BOOL RectInRegion(
  HRGN hrgn,         // handle to region
  CONST RECT *lprc   // pointer to rectangle


The GetRgnBox function retrieves the bounding rectangle of the specified region.

int GetRgnBox(
  HRGN hrgn,   // handle to a region
  LPRECT lprc  // bounding rectangle


The OffsetRgn function moves a region by the specified offsets.

int OffsetRgn(
  HRGN hrgn,     // handle to region
  int nXOffset,  // offset along x-axis
  int nYOffset   // offset along y-axis


The SetRectRgn function converts a region into a rectangular region with the specified coordinates.

BOOL SetRectRgn(
  HRGN hrgn,       // handle to region
  int nLeftRect,   // x-coordinate of upper-left corner of rectangle
  int nTopRect,    // y-coordinate of upper-left corner of rectangle
  int nRightRect,  // x-coordinate of lower-right corner of rectangle
  int nBottomRect  // y-coordinate of lower-right corner of rectangle

Paint Functions

WIN32 provides a number of functions that will allow a region handle to be used for painting affects. These effects include filling with a brush, outlining, and inverting the current contents


The FillRgn function fills a region by using the specified brush.

BOOL FillRgn(
  HDC hdc,    // handle to device context
  HRGN hrgn,  // handle to region to be filled
  HBRUSH hbr  // handle to brush used to fill the region


The FrameRgn function draws a border around the specified region by using the specified brush.

BOOL FrameRgn(
  HDC hdc,     // handle to device context
  HRGN hrgn,   // handle to region to be framed
  HBRUSH hbr,  // handle to brush used to draw border
  int nWidth,  // width of region frame
  int nHeight  // height of region frame


The InvertRgn function inverts the colors in the specified region.

BOOL InvertRgn(
  HDC hdc,    // handle to device context
  HRGN hrgn   // handle to region to be inverted


The PaintRgn function is similar to the FillRgn function, except that this function paints the specified region by using the brush currently selected into the device context.

BOOL PaintRgn(
  HDC hdc,    // handle to device context
  HRGN hrgn   // handle to region to be painted


The GetRegionData function fills the specified buffer with data desc

DWORD GetRegionData(
  HRGN hRgn,            // handle to region
  DWORD dwCount,        // size of region data bufferribing a region. 
This data includes the dimensions of the rectangles that make up the region.
  LPRGNDATA lpRgnData   // region data buffer

Region Data

A WIN32 region is really a set of rectangles managed in one object. The RGN_DATA structure manages this set of rectangles as well as some of the other data that is required to maintain the region. The RGN_DATA structure is accessible through the GetRegionData function. A region object can be created with a RGN_DATA structure with the ExtCreateRgn function. Here is a diagram to show the internal representation of a HRGN object. Each rectangle in the region is painted with an alternating colored brush.

Internal Representation of a region.


The ExtCreateRegion function creates a region from the specified region and transformation data.

HRGN ExtCreateRegion(
  CONST XFORM *lpXform,     // transformation data
  DWORD nCount,             // size of region data
  CONST RGNDATA *lpRgnData  // region data buffer

Something to be aware of, is that Windows 2000 will accept a region that has overlapping rectangles. If you try to create a region with data that contains overlapping rectangles on Windows NT4, this function will fail. Therefore if you create one of these regions on a Windows 2000 machine, and store the RGN_DATA of a region in a file or send the data across the network to a Windows NT4 machine, when you try to call ExtCreateRgn on the structure, it will fail.

The Windows 9x kernel dpes not support world transforms that involve either shearing or rotations. ExtCreateRegion fails if the transformation (XFORM) matrix has a scaling or translation of the region.


The program that has been created to demonstrate some of the possibilities for regions is an interactive window that allows the user to create two regions. The user can add new portions to either one of the source regions with a rectangle, ellipse or round rectangle tool. The polygon has been left out simply because of extra work related to the UI that would be required to incorporate this tool, however, a polygon is completely possible.

Here are the directions for creating a source region:

  1. Select the desired draw mode shape from the menu or the tool bar. There are three choices, Rectangle , Ellipse , or Round Rectangle .
  2. Select and area on the main window, and start dragging the region. A rubber-banding effect will show where the region will appear.
  3. Use the Left mouse button to draw a shape for the Source 1 region.
    Use the Right mouse button to draw a shape for the Source 2 region.
  4. At any time, you can hit the Escape key to cancel the drawing operation.
  5. Let go of the mouse button to update the region with the new shape that you have created.
  6. The Source 1 region will be drawn in RED, the Source 2 region will be drawn in Blue.

On the right-hand side of the window where the regions are drawn, there are four sub-views, where the different combine modes: RGN_AND, RGN_DIFF, RGN_OR, RGN_XOR are all demonstrated. As each source region is modified, these sub-views are updated. It is possible to replace one of the current source regions with one of the sub-view results simply by dragging the region to the window where the source regions are displayed. The Source 1 and Source 2 regions are displayed in Red and Blue respectively, just as in the main view, and the new combine region will be displayed in purple.

Here are the directions for moving the combine region to the source region.

  1. Simply decide which of the four sub-views that you would like to use.
  2. Click in the sub-view and drag the mouse to the main window. An icon will appear indicating that the item is being dragged.
  3. Use the Left mouse button to replace the combine region for the Source 1 region.
    Use the Right mouse button to replace the combine region for the Source 2 region.
  4. At any time, you can hit the Escape key to cancel the replace operation.
  5. Let go of the mouse button to replace the region with the combine region that you dragged..

You can delete the Source1, Source2 or both of the regions by selecting the appropriate menu item.

You can choose to view either the Source1 region, the Source2 region, or all of the regions by selecting the appropriate view from the menu. View All is the default selection.

One last operation in the RgnGuide program is to view the rectangles that compose the region in breakdown mode. You can do this by simply checking either the BreakDown Src 1 or BreakDown Src 2 menu item. The view will then show the selected region broken down into each of the rectangles that creates that region. Each rectangle will be painted with an alternating color. A plain reactangle will be pretty boring with one solid colored rectangle. An ellipse on the otherhand will contain a new rectangle on just about every other scanline.


Regions are a very powerful OS resource that you can use in your program. The demo program simply illustrates how to manage your resources, there are many other uses than the ones that have been described. WIN32 provides a rich set of functions that allows you to manage and manipulate your regions.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul M Watt
United States United States
I am a software architect and I have been developing software for nearly two decades. Over the years I have learned to value maintainable solutions first. This has allowed me to adapt my projects to meet the challenges that inevitably appear during development. I use the most beneficial short-term achievements to drive the software I develop towards a long-term vision.

C++ is my strongest language. However, I have also used x86 ASM, ARM ASM, C, C#, JAVA, Python, and JavaScript to solve programming problems. I have worked in a variety of industries throughout my career, which include:
• Manufacturing
• Consumer Products
• Virtualization
• Computer Infrastructure Management
• DoD Contracting

My experience spans these hardware types and operating systems:
• Desktop
o Windows (Full-stack: GUI, Application, Service, Kernel Driver)
o Linux (Application, Daemon)
• Mobile Devices
o Windows CE / Windows Phone
o Linux
• Embedded Devices
o VxWorks (RTOS)
o Greenhills Linux
o Embedded Windows XP

I am a Mentor and frequent contributor to with tutorial articles that teach others about the inner workings of the Windows APIs.

I am the creator of an open source project on GitHub called Alchemy[^], which is an open-source compile-time data serialization library.

I maintain my own repository and blog at[^], because code maintenance does not have to be a living hell.

You may also be interested in...

Comments and Discussions

Questionmissing atlres.h Pin
cassiob13-Sep-17 20:30
membercassiob13-Sep-17 20:30 
QuestionRegion from FrameRgn Pin
mcrawley3-Apr-12 7:05
membermcrawley3-Apr-12 7:05 
I am trying to take the region of a parent window, copy it, frame the copy, then clip out the frame region so the frame region can become a child window's clipping region. The child window will sit on top of the parent window and above other child windows and provide resize/move functionality for the parent window. It has to be on top of the other windows and therefore needs to be transparent but accessible to mouse input.

I've tried several approaches, but I can't seem to figure it out. I've tried scaling the region. I've tried filling the copied region with a color, SetBkMode to transparent before painting the frame a different color, then XOR the difference would work, but nothing. I'm thinking I'm going to have to line-by-line set the bits I need transparent using GetRegionData before trying to crop again, but that would be slow to execute and tedious to code.

If you or someone could point me in the right direction, I would appreciate it.
AnswerRe: Region from FrameRgn Pin
Paul Watt6-Apr-12 19:46
memberPaul Watt6-Apr-12 19:46 
QuestionRegions Pin
crazymike193820-Dec-11 13:18
membercrazymike193820-Dec-11 13:18 
AnswerRe: Regions Pin
Paul Watt21-Dec-11 14:39
memberPaul Watt21-Dec-11 14:39 
GeneralMy vote of 5 Pin
crazymike193823-Oct-11 10:13
membercrazymike193823-Oct-11 10:13 
GeneralRe: My vote of 5 Pin
Paul Watt23-Oct-11 13:54
memberPaul Watt23-Oct-11 13:54 
GeneralMy vote of 5 Pin
rkjpsn7-Dec-10 11:52
memberrkjpsn7-Dec-10 11:52 
GeneralGood job! Pin
JoeJiao12-Jul-10 6:36
memberJoeJiao12-Jul-10 6:36 
Questionhow to fill a region of independent block. Pin
faju21-Apr-09 3:55
memberfaju21-Apr-09 3:55 
AnswerRe: how to fill a region of independent block. Pin
Paul Watt21-Apr-09 5:28
memberPaul Watt21-Apr-09 5:28 
QuestionCombining Text In Regions Pin
SparkyNZ8-Jan-07 10:17
memberSparkyNZ8-Jan-07 10:17 
GeneralProblem with RectInRegion Pin
ertl1-Oct-06 4:35
memberertl1-Oct-06 4:35 
GeneralRe: Problem with RectInRegion Pin
ertl2-Oct-06 6:18
memberertl2-Oct-06 6:18 
GeneralFillRgn or PaintRgn not working with DrawItem Pin
Nareen Neelamegam11-Sep-05 21:59
memberNareen Neelamegam11-Sep-05 21:59 
GeneralRe: FillRgn or PaintRgn not working with DrawItem Pin
relient12-Sep-05 5:38
memberrelient12-Sep-05 5:38 
GeneralRe: FillRgn or PaintRgn not working with DrawItem Pin
Paul Watt12-Sep-05 6:36
memberPaul Watt12-Sep-05 6:36 
GeneralRe: FillRgn or PaintRgn not working with DrawItem Pin
Nareen Neelamegam12-Sep-05 20:13
memberNareen Neelamegam12-Sep-05 20:13 
GeneralCombineRgn problem Pin
relient11-Sep-05 19:37
memberrelient11-Sep-05 19:37 
GeneralRe: CombineRgn problem Pin
Paul Watt12-Sep-05 6:14
memberPaul Watt12-Sep-05 6:14 
GeneralRe: CombineRgn problem Pin
relient12-Sep-05 7:43
memberrelient12-Sep-05 7:43 
GeneralRe: CombineRgn problem Pin
bikas20-Jul-10 1:16
memberbikas20-Jul-10 1:16 
GeneralRe: CombineRgn problem Pin
relient24-Sep-05 7:25
memberrelient24-Sep-05 7:25 
GeneralRe: CombineRgn problem Pin
relient26-Sep-05 6:27
memberrelient26-Sep-05 6:27 
GeneralExcluding region of arbitrary size - not rectangular Pin
relient9-Sep-05 10:18
memberrelient9-Sep-05 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180321.1 | Last Updated 11 Mar 2002
Article Copyright 2002 by Paul M Watt
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid