Roger Don
Roger Don

Reputation: 9

C++ Win32 GDI double buffering

Could you give the simplest way to achieve double buffering for this example code (to prevent flickering):

HWND hwnd = FindWindow(0, "Untitled - Notepad");
HDC hDC_Desktop = GetDC(hwnd);

...

        while( )
        {
                    RECT rect = { 10, 10, 10 + 50, 10 + 50 };

                    FillRect(hDC_Desktop, &rect, ColorBrush);
                    InvalidateRect (hwnd, NULL, TRUE);
        }

Upvotes: 0

Views: 3599

Answers (1)

Jason De Arte
Jason De Arte

Reputation: 555

The reason it's "flickering" is because the target window is getting invalidated and it is being redrawn. Since it's not your window - you don't necessarily have control over that.

If this was your own window there is a simple strategy to speed up your drawing speed and reduce flicker: Use a Memory DC to draw on and capture WM_ERASEBKGND to suppress background redraws.

In depth explanation and strategy for fixing it (in your application's window): http://www.catch22.net/tuts/win32/flicker-free-drawing

If your intent is to draw on another application, might I suggest creating a window on top of that application and draw on that.

Upvotes: 2

Related Questions