GDI + double buffering in C ++

I haven't written anything with GDI for a while (and never with GDI +), and I'm just working on an interesting project, but for life I can't figure out how to double the GDI + buffer

void DrawStuff(HWND hWnd) {
    HDC          hdc;
    HDC          hdcBuffer;
    PAINTSTRUCT  ps;
    hdc = BeginPaint(hWnd, &ps);
    hdcBuffer = CreateCompatibleDC(hdc);
    Graphics graphics(hdc);
    graphics.Clear(Color::Black);

    // drawing stuff, i.e. bunnies:

    Image bunny(L"bunny.gif");
    graphics.DrawImage(&bunny, 0, 0, bunny.GetWidth(), bunny.GetHeight());  

    BitBlt(hdc, 0,0, WIDTH , HEIGHT, hdcBuffer, 0,0, SRCCOPY);
    EndPaint(hWnd, &ps);
}

The above works (everything displays perfectly), but it flickers. If I change Graphics graphics(hdc);to Graphics graphics(hdcBuffer);, I don't see anything (although I have to hit the buffer → hWnd hdc at the bottom).

My message pipeline is configured correctly (WM_PAINT calls DrawStuff), and I force the WM_PAINT message every program cycle, calling RedrawWindow(window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);

I'm probably going to do the wrong way to do this, any ideas? The MSDN documentation is cryptic at best.

+5
source share
3 answers

CreateCompatibleDC(hdc) DC 11 . CreateCompatibleBitmap hdcBuffer, , .

Edit:

WM_ERASEBKGND,

hdc = BeginPaint(hWnd, &ps);

BeginPaint Windows WndProc a WM_ERASEBKGND, , , , DefWindowProc , , , , TRUE.

case WM_ERASEBKGND:
   return TRUE; // tell Windows that we handled it. (but don't actually draw anything)

Windows , , , , RDW_ERASE, , , RedrawWindow

+6

...

void DrawAll(CDC *pDC)
{
    CRect rect;
    GetClientRect(&rect);

    Bitmap *pMemBitmap = new Bitmap(rect.Width(), rect.Height());

    Graphics* pMemGraphics = Graphics::FromImage(pMemBitmap);

    Graphics graphics(pDC->m_hDC);

    // use pMemGraphics  do something....

    Status status;  
    if ((status = graphics.DrawImage(pMemBitmap, 0, 0)) !=Ok)
    {
        //some error
    }

   delete pMemGraphics;
}
+2

Are you processing WM_ERASEBKGND? I believe that it is called right before WM_PAINT and usually the background color of the window shines, which you probably do not want.

+1
source

All Articles