Reputation:
I want to load a bitmap to a window in my application, I will use GDI to draw the bitmap to the window. Should I also draw the bitmap when handling the WM_PAINT
message so that the bitmap persists on the window?
Upvotes: 1
Views: 2551
Reputation: 6566
Yes, you should draw your bitmap in WM_PAINT or WM_ERASEBKGND handler just like this:
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &Ps);
// Load the bitmap from the resource
bmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_MY_COOL_PIC));
// Create a memory device compatible with the above DC variable
MemDC = CreateCompatibleDC(hDC);
// Select the new bitmap
HBITMAP hOldBmp = SelectObject(MemDC, bmp);
// Copy the bits from the memory DC into the current dc
BitBlt(hDC, 10, 10, 450, 400, MemDC, 0, 0, SRCCOPY);
// Restore the old bitmap
SelectObject(MemDC, hOldBmp);
DeleteObject(bmp);
DeleteDC(MemDC);
EndPaint(hWnd, &Ps);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
Upvotes: 2