LRESULT CALLBACK GraphDemoWndProc(HWND, UINT, UINT, LONG); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow) { HWND hWnd; WNDCLASS WndClass; MSG Msg; char szClassName[] = "GraphDemo"; WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = GraphDemoWndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = 0; WndClass.hCursor = LoadIcon(NULL, IDI_APPLICATION); WndClass.hIcon = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = szClassName; if(!RegisterClass(&WndClass)) { MessageBox(0, "Can't create window", "Error", MB_OK); return 0; } hWnd = CreateWindow(szClassName, "GraphDemo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if(!hWnd) { MessageBox(0,"","", MB_OK); return 0; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); while(GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK GraphDemoWndProc(HWND hWnd, UINT Message, UINT wParam, LONG lParam) { HDC hDC, hCompatibleDC; PAINTSTRUCT PaintStruct; RECT Rect; HBITMAP hCompatibleBitmap, hOldBitmap; HPEN hOldPen; static HPEN Pens[5]; HBRUSH hOldBrush; static HBRUSH Brushes[6]; int i; switch(Message) { case WM_PAINT: randomize(); for(i = 0; i <= 4; i++) { Pens[i] = (CreatePen(i, 1, RGB(random(255), random(255), random(255)))); Brushes[i] = (CreateHatchBrush(i, RGB(random(255), random(255),random(255)))); } GetClientRect(hWnd, &Rect); hDC = BeginPaint(hWnd, &PaintStruct); hCompatibleDC = CreateCompatibleDC(hDC); GetClientRect (hWnd, &Rect); hCompatibleBitmap = CreateCompatibleBitmap(hDC, Rect.right, Rect.bottom); hOldBitmap = SelectObject(hCompatibleDC, hCompatibleBitmap); PatBlt(hCompatibleDC, 0, 0, Rect.right, Rect.bottom, PATCOPY); for(i = 0; i <= 9999; i++) SetPixel(hCompatibleDC, random(Rect.right), random(Rect.bottom), RGB(random(255), random(255), random(255))); for(i=0; i <= 9; i++) { hOldPen = SelectObject(hCompatibleDC, Pens(random(4))); MoveToEx(hCompatibleDC, random(Rect.right, random(Rect.bottom), NULL)); LineTo(hCompatibleDC, random(Rect.right), random(Rect.bottom)); SelectObject(hCompatibleDC, hOldPen); } for(i = 0; i <= 5; i++) { hOldBrush = SelectObject(hCompatibleDC, Brushes(random(4))); Rectangle(hCompatibleDC, random(Rect.right), random(Rect.bottom), random(Rect.right), random(Rect.bottom)); Ellipse(hCompatibleDC, random(Rect.right), random(Rect.bottom), random(Rect.right), random(Rect.bottom)); SelectObject(hCompatibleDC, hOldBrush); } BitBlt(hDC, PaintStruct.rcPaint.left, PaintStruct.rcPaint.top, PaintStruct.rcPaint.right, PaintStruct.rcPaint.bottom, hCompatibleDC, PaintStruct.rcPaint.left, PaintStruct.rcPaint.top, SRCCOPY); for(i = 0; i<=4;i++) { DeleteObject(Pens[i]); DeleteObject(Brushes[i]); } SelectObject(hCompatibleDC, hOldBitmap); DeleteObject(hCompatibleBitmap); DeleteDC(hCompatibleDC); EndPaint(hWnd, &PaintStruct); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,Message,wParam, lParam); } |