程序5-7 RandRect

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值