WM_PAINT + back buffer [GDI]
Hello. My first post by the way :) .... I am basically writing a Solitaire card game by hand just using Win32 API [GDI] for graphics.
Anyways, I have a few questions regarding the WM_PAINT message and using a back buffer to eliminate flicker. Basically I'm trying to piece this information together from multiple sources (websites; books; etc.) and wouldn't mind some input as to whether I am doing it correctly. i.e. Did i free all resources properly? Ex: I've seen SelectObject(hdcBuffer, bmpBuffer); examples that say you need to store a HBITMAP that gets returned which you should restore at end, but this didn't work for me.
Relevant code:
Code:
case WM_PAINT:
PAINTSTRUCT paintStruct; // Hold information about the area to be repainted.
// Load relevant information into the PAINTSTRUCT.
BeginPaint(windowHandle, &paintStruct);
repaint(paintStruct); // Call custom function for repainting the window.
// Cleanup.
EndPaint(windowHandle, &paintStruct);
break;
Code:
void repaint(PAINTSTRUCT &paintStruct)
{
// Create a buffer.
HDC hdcBuffer = CreateCompatibleDC(paintStruct.hdc);
// Create a bitmap in memory to draw to.
HBITMAP bmpBuffer = CreateCompatibleBitmap(paintStruct.hdc, paintStruct.rcPaint.right, paintStruct.rcPaint.bottom);
// Select bitmap into the buffer.
SelectObject(hdcBuffer, bmpBuffer);
HBRUSH background; // Brush used for painting the background.
// Create a dark green brush.
background = CreateSolidBrush(RGB(0,155,0));
// Repaint the buffer with the background brush defined above.
FillRect(hdcBuffer, &paintStruct.rcPaint, background);
// Redraws all relevant cards to the buffer. Basically draws stuff to hdcBuffer.
deck.redrawAll(hdcBuffer, paintStruct.rcPaint);
// Copy back buffer into screen device context for display.
BitBlt(paintStruct.hdc, 0, 0, paintStruct.rcPaint.right, paintStruct.rcPaint.bottom, hdcBuffer, 0, 0, SRCCOPY);
DeleteDC(hdcBuffer); // Free resources.
DeleteObject(bmpBuffer); // Free resources.
// Delete brush created earlier.
DeleteObject(background);
}