简体   繁体   中英

GDI+ double buffering in C++

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

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 renders perfectly), but it flickers. If I change Graphics graphics(hdc); to Graphics graphics(hdcBuffer); , I see nothing (although I should be bitblt'ing the buffer->hWnd hdc at the bottom).

My message pipeline is set up properly (WM_PAINT calls DrawStuff), and I'm forcing a WM_PAINT message every program loop by calling RedrawWindow(window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);

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

CreateCompatibleDC(hdc) creates a DC with a 1x1 pixel monochrome bitmap as its drawing surface. You need to also CreateCompatibleBitmap and select that bitmap into the hdcBuffer if you want a drawing surface larger than that.

Edit:

the flickering is being caused by WM_ERASEBKGND, when you do this

hdc = BeginPaint(hWnd, &ps);

Inside the call to BeginPaint, Windows sends your WndProc a WM_ERASEBKGND message if it thinks the background needs to be redrawn, if you don't handle that message, then DefWindowProc handles it by filling the paint rectangle with your class brush, so to avoid the flickering, you should handle it and return TRUE.

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

Windows thinks your background should be erased because you tell it that it should, that's what RDW_ERASE means, so you should probably leave that out of your RedrawWindow call

You can try the following way:

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 = graphics.DrawImage(pMemBitmap, 0, 0);   
    if (status != Ok)
    {
        //some error
    }

   delete pMemGraphics;
   delete pMemBitmap;
}

Are you handling WM_ERASEBKGND? I believe it gets called right before WM_PAINT and usually blits the window's background color which you probably don't want to happen.

Given "Graphics graphics(hdc)", you seem to be clearing hdc instead of hdcBuffer. Sure enough, that will cause flicker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM