If you're still going through Petzold, you might be getting a bit ahead of yourself. This wndproc will draw random rectangles to a memory dc and then the WM_PAINT Blits it -
Code:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static HDC memDC;
static HBITMAP hBitmap;
static int cScreenX,cScreenY;
HBRUSH hBrush;
switch (message)
{
case WM_CREATE:
srand((unsigned)time(NULL));
//Get Dimensions for Memory DC
cScreenX=GetSystemMetrics(SM_CXSCREEN);
cScreenY=GetSystemMetrics(SM_CYSCREEN);
hdc=GetDC(hWnd);
memDC = CreateCompatibleDC(hdc);
//Create Bitmap to 'select' into Memory DC
//to paint onto
hBitmap = CreateCompatibleBitmap(hdc,cScreenX,cScreenY);
SelectObject(memDC,hBitmap);
//Get brush used for main window, so that
//Memory DC will match
hBrush = (HBRUSH)GetClassLong(hWnd,GCL_HBRBACKGROUND);
SelectObject(memDC,hBrush);
//Blit Brush to fill whole dimension of Memory DC
PatBlt(memDC,0,0,cScreenX,cScreenY,PATCOPY);
ReleaseDC(hWnd,hdc);
case WM_SIZE:
{
int a = rand()%250;
int b = rand()%250;
//Draw Rectangle to Memory DC
Rectangle(memDC,a,b,50,50);
InvalidateRect(hWnd,0,TRUE);
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hWnd, &ps) ;
//Blit Memory DC to window
BitBlt(hdc,0,0,cScreenX,cScreenY,memDC,0,0,SRCCOPY);
EndPaint (hWnd, &ps) ;
return 0 ;
case WM_DESTROY:
DeleteDC(memDC);
DeleteObject(hBitmap);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}