winapibrushpen

how to create a pen that is the same color as that of a solid brush?


I would like to get the color (colorref) of a SolidBrush to create a pen of the same color, using only Windows API functions (no dot net and stuff like that) using C/C++/Asm/Delphi, etc.

If it helps, this is what I'm trying to accomplish: I've drawn a curve using PolyLine in a resizable window.

Letting Windows clear the background every time the window is resized causes a very noticeable amount of flicker. To reduce the flicker to a minimum, I can get the background brush that Windows is using to clear the window (GetClassLongPr) and, create a pen of that same color to redraw just the curve with that pen, effectively erasing it without clearing the entire client area (which is what causes the flicker.) Of course, I cannot create that pen unless I find a way to get the colorref from the brush handle returned by GetClassLongPtr.

I can't figure out how to create a pen that is of the color used by the solid brush windows uses to erase the client area.

NOTE: I am aware that another way of solving the flicker problem is to create a clipping region to cause just the curve to be erased but, region handling is much slower than simply redrawing the curve with a pen set to the background color.

After this long story, the question is: how do I create a pen that is the same color as that of the solid brush used to erase the window client area ?

Thank you.


Solution

  • You get the solid brush parameters with GetObject() into a LOGBRUSH structure, then you use CreatePen() to create the Pen.

    The idea however to avoid flickering is to use double buffering: Do not draw to the DC you get from GetDC(), but to a memory DC. Do not use WM_ERASEBKGND to erase the background, process WM_ERASEBKGND and return nonzero without doing anything, erase it in the WM_PAINT instead. Create the memory DC with CreateCompatibleDC() then use BitBlt() to transfer your drawing to the actual DC.

    All this if at all using GDI anyway, do prefer Direct2D nowadays.