noob GDI troubles

Started by
0 comments, last by Mastaba 18 years, 4 months ago
My program makes colored snow. It's just a learning tool. Anyway, when I move my program off screen, and then move it back into view.....the entire client window gets erased and the background brush is restored. I lose all the snow I made. How can I prevent that? Ideally, I want to save the client area and then restore it. I think I need to work with WM_PAINT, but I'm at a loss.

#define WIN32_LEAN_AND_MEAN   //restricts to API
#include <windows.h>		  //master Windows header
#include <stdio.h> 
#include <stdlib.h> 

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

const int xw = (640 + (GetSystemMetrics(SM_CXFIXEDFRAME)*2))+1,
		  yw = (480 + GetSystemMetrics(SM_CYCAPTION)+ (GetSystemMetrics(SM_CYFIXEDFRAME)*2)+ GetSystemMetrics(SM_CYMENU))+1;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
	 static TCHAR szAppName[] = TEXT ("PK");
     HWND         hwnd;
     MSG          msg;
	 WNDCLASSEX  wndclass;

	 wndclass.cbSize = sizeof(WNDCLASSEX);
	 wndclass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	 wndclass.lpfnWndProc = WndProc;
	 wndclass.cbClsExtra = 0;
	 wndclass.cbWndExtra = 0;
	 wndclass.hInstance = hInstance;
	 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	 wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	 wndclass.lpszMenuName = NULL;
	 wndclass.lpszClassName = szAppName;
	 wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);


     if (!RegisterClassEx (&wndclass))
     {
		return 0 ;
     }
     
     hwnd = CreateWindowEx(
	 NULL,
	 szAppName,
	 TEXT ("Photo Knight"),
	 WS_SYSMENU | WS_MINIMIZEBOX,
	 CW_USEDEFAULT,
	 CW_USEDEFAULT,
	 xw,
	 yw,
	 NULL,
	 NULL,
	 hInstance,
	 NULL);
     
     ShowWindow (hwnd, iCmdShow);
     UpdateWindow (hwnd);


 while(true)
 {
	 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
	 {
		 if(msg.message == WM_QUIT)
		 {
			 break;
		 }
		 else
		 {
		 TranslateMessage(&msg);
		 DispatchMessage(&msg);
		 }
	 }
	 else
	 {
		 HDC hdc = GetDC(hwnd);
		 SetPixel(hdc,rand()%xw,rand()%yw,RGB(rand()%256,100,100));
		 ReleaseDC(hwnd,hdc);
	 }
	 }
 }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
        case WM_CREATE:
			{
			return 0;
			}
			break;
        case WM_DESTROY:
			{
            PostQuitMessage(0); 
            return 0; 
			}
			break;
		case WM_PAINT:
			{
			PAINTSTRUCT ps;
			BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);
			return 0;
			}
		   break;
			
		default:
			{
			}
			break;
	}
    return DefWindowProc(hwnd, message, wParam, lParam); 
} 

Advertisement
The easiest solution is to draw the pixels on a bitmap, and blit the bitmap to window client area.
.

This topic is closed to new replies.

Advertisement