zwz_good  

program RandRect;
//程序5-7 RandRect
uses
  Windows, Messages;
var
  cxClient, cyClient: LongWord;

procedure DrawRectangle(hWindow: HWND);
var
  hWndc: HDC;
  hBrush1: HBRUSH;
  rc: TRect;
begin
  if (0=cxClient) or (0=cyClient) then
    exit;

  SetRect(rc, random(cxClient), random(cyClient), random(cxClient), random(cyClient));
  hBrush1:= CreateSolidBrush(RGB(random(256), random(256), random(256)));

  hWndc:= GetDC(hWindow);
  FillRect(hWndc, rc, hBrush1);
  ReleaseDC(hWindow, hWndc);
  DeleteObject(hBrush1);
end;

function WndProc(hWindow: HWND; Msg, wParam, lParam: LongInt): LRESULT; stdcall;
var
  ps: TPaintStruct;
  hWndc: HDC;
begin
  Result:= 0;
  case Msg of
  WM_CREATE:
  begin

  end;
  WM_PAINT:
  begin  //只要自己处理重绘消息,以下两句必须有,   否则有问题。
    hWndc:= BeginPaint(hWindow, ps);
    EndPaint(hWindow, ps);
  end;
  WM_SIZE:
  begin
    cxClient:= LoWord(lParam);
    cyClient:= HiWord(lParam);
  end;
  WM_DESTROY:
  begin
    PostQuitMessage(0);
  end
  else
    Result:= DefWindowProc(hWindow, Msg, wParam, lParam);
  end;
end;
const
  szAppName = 'RandRect';
var
  wndclass1: TWndClass;
  Msg: TMsg;
  hWindow: HWND;
begin
  wndclass1.style:= CS_VREDRAW or CS_HREDRAW;
  wndclass1.lpfnWndProc:= @WndProc;
  wndclass1.cbClsExtra:= 0;
  wndclass1.cbWndExtra:= 0;
  wndclass1.hInstance:= HInstance;
  wndclass1.hIcon:= LoadIcon(0, 0);
  wndclass1.hCursor:= LoadCursor(0, 0);
  wndclass1.hbrBackground:= GetStockObject(0);
  wndclass1.lpszMenuName:= nil;
  wndclass1.lpszClassName:= szAppName;

  if RegisterClass(wndclass1) = 0 then
  begin
    MessageBox(0, 'This program requires windows NT!', szAppName, MB_ICONERROR);
    exit;
  end;

  hWindow:= CreateWindow(szAppname, 'Random Rectangles', WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, HInstance, nil);

  ShowWindow(hWindow, CmdShow);
  UpdateWindow(hWindow);

  while True do
  begin
    if PeekMessage(Msg, 0, 0, 0, PM_REMOVE)  then
    begin
      if Msg.message = WM_QUIT then
        break;
      TranslateMessage(Msg);
      DispatchMessage(Msg);
    end
    else
    begin
      DrawRectangle(hWindow);
      Sleep(10);
    end;
  end;
end.

posted on 2009-04-26 15:43  zwz_good  阅读(141)  评论(0编辑  收藏  举报