Search code examples
windowsgraphicsgdi+gdi

Exclude rectangle from being painted in Windows


I would like to exclude rectangles from being painted in a window during the WM_PAINT. The opposite is possible with the SelectClipRgn(hdc, hRegion); however I want to paint everything except the region.

Thanks in advance, Jasper de Keijzer


Solution

  • You can only do that indirectly. (afaik)

    1. Create a bitmap of the size of your form (or control)
    2. Create a Graphics object, that draws to that bitmap (Pixelformat 32bppArgb) ' Graphics.FromImage(...)
    3. Clear the bitmap with transparent color
    4. Draw everything into that picture
    5. Draw / Fill the rectangle that should be excluded with transparent color
    6. Draw the bitmap to your form (or control)

    Beware to use the right ComposingMode for your graphics objects. For drawing to the bitmap use SourceCopy (otherwise the area will not be completely transparent) and SourceOver for drawing the bitmap to your form (or control).