"Use of Win32" demonstrations
Clipping control by device context

   What is clipping?
   Why alter the clipping region?
   Manipulating the DC directly
   How these tests work
   The tests themselves

What is clipping?
To understand this, it is necessary to understand how Windows deals with windows which overlap each other. Windows does not draw to the screen a series of windows one on top of the other so that the last one to be painted appears on top. If it did do this there would be a lot of "flashing" as windows appeared then disappeared as they were drawn over. Instead, the system keeps a record of which part of the screen needs updating (the "update region" or "invalid region") and it also knows which part of a window is visible at any one time (the "visible region"). The system only permits drawing in these two regions (together they are known as the "clipping region"). There may be various reasons for the need for updating: for example, another window may have been removed to expose a part of the window to be painted, or the user may have clicked the taskbar to show the window concerned.

Why alter the clipping region ?
The ability to change the clipping region presents many opportunities to customise your application to look quite different on the screen. It gives you the opportunity to create buttons and windows of various shapes and styles which are not available in the Windows API. A simple example was given in temporary window drawn directly, where a copyright notice was drawn to the screen during WM_PAINT and then excluded from the clipping region to make sure it was not written over. There are other examples in clipping control by window.

Manipulating the DC directly
How you change the clipping region depends on whether or not you have called BeginPaint or GetDC or GetDCEx. If you have already called one of these, the clipping region for the window's device context (DC) has already been set to the (now) visible region of the window (ie. the previous visible region, less the region now covered, plus the region now uncovered). Thus you can only change the clipping area by using those functions which directly affect the DC, as in all these examples. However, you can affect the clipping region in other ways before getting the DC. For example you can invalidate or invalidate the whole window or parts of the window using InvalidateRect, InvalidateRgn or ValidateRect, ValidateRgn or RedrawWindow. You can define which parts of the window are to be recognised and drawn to by the system using SetWindowRgn (see clipping control by window). Window styles will also affect how the clipping region is found, for example WS_CLIPCHILDREN and WS_CLIPSIBLINGS.

How these tests work
All these tests practice changing the clipping region by direct manipulation of the DC after a call to BeginPaint. In all cases the relevant breakpoint is CLIP_PAINT, which is called on the WM_PAINT message received by the ClipWndProc window procedure. After calling BeginPaint the function GET_CLIPRGNANDTEXT which removes a region from the clipping region. Exactly which shape is removed and using which flags depends on the bits set in the flag TESTFLAG2. In every case, however, the clipping region is manipulated before painting in the window. The whole window is then filled with a hatched brush and a text message. However, you can therefore see from this that the system has not permitted drawing in the region which has been removed from the clipping region in the function GET_CLIPRGNANDTEXT.

The tests themselves

Using ExcludeClipRect
This test is very similar to temporary window drawn directly, in that a rectangle is removed from the clipping region using the API ExcludeClipRect.

The tests using ExtSelectClipRgn
This API changes the clipping region by reference to a control region given to it by the program. The control regions used for these tests are irregular shapes created using CreatePolygonRegion. There are 5 different modes tested here and the value is passed to the API in EDI and PUSHed as the first argument:-

RGN_AND (edi=1) - the two regions are ANDed, ie. the system will only permit drawing where the clipping region and the control region overlap.

RGN_COPY (edi=5) - the clipping region is made the same as the control region (same effect as the API SelectClipRgn).

RGN_DIFF (value 4) - the system does not allow drawing in the control region (same effect as ExcludeClipRect which can be used for a rectangular shape).

RGN_OR (value 2) - the two regions are ORed, ie. the system will permit drawing in the existing clipping region and also in the control region. To test this firstly the existing clipping region is restricted using SelectClipRgn and then the control region is added.

RGN_XOR (value 3) - the system does not allow drawing where the existing clipping region and the control region overlap.