I have read many examples on the internet but I'm still stuck. I'm trying to process the WM_PAINT message sent to my application.
In my application, I always draw in the same DC, named g_hDC
. It works perfectly. When WM_PAINT
is received, I just try to draw the content of my g_hDC
into the DC returned by BeginPaint
. I guess g_hDC
contains the last bitmap that I drawn. So I just want to restore it.
case WM_PAINT:
PAINTSTRUCT ps;
int ret;
HDC compatDC;
HDC currentDC;
HDC paintDC;
HBITMAP compatBitmap;
HGDIOBJ oldBitmap;
paintDC = BeginPaint(g_hWnd, &ps);
currentDC = GetDC(g_hWnd);
compatDC = CreateCompatibleDC(paintDC);
compatBitmap=CreateCompatibleBitmap(paintDC, CONFIG_WINDOW_WIDTH, CONFIG_WINDOW_HEIGHT);
oldBitmap=SelectObject(compatDC, compatBitmap);
ret = BitBlt(compatDC,
ps.rcPaint.left,
ps.rcPaint.top,
ps.rcPaint.right - ps.rcPaint.left,
ps.rcPaint.bottom - ps.rcPaint.top,
currentDC,
ps.rcPaint.left,
ps.rcPaint.top,
SRCCOPY);
ret = BitBlt(paintDC,
ps.rcPaint.left,
ps.rcPaint.top,
ps.rcPaint.right - ps.rcPaint.left,
ps.rcPaint.bottom - ps.rcPaint.top,
compatDC,
ps.rcPaint.left,
ps.rcPaint.top,
SRCCOPY);
DeleteObject(SelectObject(compatDC, oldBitmap));
DeleteDC(compatDC);
DeleteDC(currentDC);
EndPaint(g_hWnd, &ps);
break;
But it just draws a white rectangle ... I tried many possibilities and nothing works. Can you please help me?
Use this to delete bitmat:
DeleteObject( SelectObject(compatDC,oldBitmap) );
- without DeleteBitmap on prev line.SelectObject
returns current (old) selection as return value - and you delete it. In your case you are trying to delete still selected bitmap.PS: I don't see
CreateCompatibleDC
- where you are creating compatDC? AddcompatDC = CreateCompatibleDC( hdc );
beforeCreateCompatibleBitmap
.