Regions can also play a role in clipping. (I discussed clipping in Chapter 2 when discussing the various SYSMETS programs.) The InvalidateRect function invalidates a rectangular area of the display and generates a WM_PAINT message. Often we use the InvalidateRect function to erase the client area and generate a WM_PAINT message:
InvalidateRect (hwnd, NULL, TRUE) ;
You can obtain the coordinates of the invalid rectangle by calling GetUpdateRect, and you can validate a rectangle of the client area using the ValidateRect function. When you receive a WM_PAINT message, the coordinates of the invalid rectangle are available from the PAINTSTRUCT structure that is filled in by the BeginPaint function. This invalid rectangle also defines a ”clipping region.“ You cannot paint outside the clipping region.
Windows has two functions similar to InvalidateRect and ValidateRect that work with regions rather than rectangles:
InvalidateRgn (hwnd, hRgn, bErase) ;
and:
ValidateRgn (hwnd, hRgn) ;
However, when you receive a WM_PAINT message as a result of an invalid region, the clipping region will still be a rectangle that encompasses the invalid region.
If you want a nonrectangular clipping region, you can select a region into the device context using either:
SelectObject (hdc, hRgn) ;
or:
SelectClipRgn (hdc, hRgn) ;
SelectObject returns a handle to the previous clipping region selected in the device context, whereas SelectClipRgn returns an nRgnType value like CombineRgn. Windows also includes several functions to manipulate this clipping region, such as ExcludeClipRect to exclude a rectangle from the clipping region, IntersectClipRect to create a new clipping region that is the intersection of the previous clipping region and a rectangle, and OffsetClipRgn to move a clipping region to another part of the client area.