zwz_good  

program Clover;
//程序5-8 Clover
uses
  Windows, Messages, Math;

function WndProc(hWindow: HWND; Msg, wParam, lParam: LongInt): LRESULT; stdcall;
const
{$J+}
  hRgnClip: HRGN = 0;
  cxClient: Integer = 0;  // 窗体当前宽度
  cyClient: Integer = 0;  // 窗体当前高度
{$J-}
var
  ps: TPaintStruct;
  hWndc: HDC;
  hRgnTmp: array[0..5] of HRGN;
  i: integer;
  hCursor1: HCURSOR;
  fAngle, fRadius: Real;
  twopi: Real;
begin
  Result:= 0;
  case Msg of
  WM_SIZE:
  begin
    cxClient:= LoWord(lParam);
    cyClient:= HiWord(lParam);
  //  hCursor1:= SetCursor(LoadCursor(0, IDC_WAIT));
   // ShowCursor(True);
   // Sleep(10);    //自己加的
    if hRgnClip <> 0 then
    begin
      DeleteObject(hRgnClip);
      hRgnClip:= 0;
    end;
    //左椭圆
    hRgnTmp[0]:= CreateEllipticRgn(0, cyClient div 3, cxClient div 2, 2*cyClient div 3);
    //右椭圆
    hRgnTmp[1]:= CreateEllipticRgn(cxClient div 2, cyClient div 3, cxClient, 2*cyClient div 3);
    //上椭圆
    hRgnTmp[2]:= CreateEllipticRgn(cxClient div 3, 0, 2*cxClient div 3, cyClient div 2);
    //下椭圆
    hRgnTmp[3]:= CreateEllipticRgn(cxClient div 3, cyClient div 2, 2*cxClient div 3, cyClient);
    //临时指向某个区域,,使其有效。
    hRgnTmp[4]:= CreateRectRgn(0, 0, 1, 1);
    hRgnTmp[5]:= CreateRectRgn(0, 0, 1, 1);
    hRgnClip:= CreateRectRgn(0, 0, 1, 1);
    //左右组合
    CombineRgn(hRgnTmp[4], hRgnTmp[0], hRgnTmp[1], RGN_OR);
    //上下组合
    CombineRgn(hRgnTmp[5], hRgnTmp[2], hRgnTmp[3], RGN_OR);

    CombineRgn(hRgnClip, hRgnTmp[4], hRgnTmp[5], RGN_XOR);
   // CombineRgn(hRgnClip, hRgnTmp[4], hRgnTmp[5], RGN_AND);
    for i := 0 to 5 do
      DeleteObject(hRgnTmp[i]);

   // Sleep(10);//自己加的
   // SetCursor(hCursor1);
   // ShowCursor(False);
  end;
  WM_PAINT:
  begin
    hWndc:= BeginPaint(hWindow, ps);
    //SelectObject(hWndc, hRgnClip);
    SelectClipRgn(hWndc, hRgnClip);
    SetViewportOrgEx(hWndc, cxClient div 2, cyClient div 2, nil);
//返回直角三角形斜边的长度
//作为Cos值的系数
    fRadius := Hypot(cxClient / 2.0, cyClient / 2.0);

    twopi:= 2 * pi;
    while (fAngle < twopi) do
    begin
      MoveToEx(hWndc, 0, 0, nil);
      LineTo(hWndc, Trunc(fRadius*Cos(fAngle)), Trunc(-fRadius*Sin(fAngle)));
      fAngle:= fAngle + twopi / 360;
    end;

    EndPaint(hWindow, ps);
  end;
  WM_DESTROY:
  begin
    DeleteObject(hRgnClip);//可别忘了
    PostQuitMessage(0);
  end
  else
    Result:= DefWindowProc(hWindow, Msg, wParam, lParam);
  end;
end;

const
  szAppName = 'Clover';
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, 'Draw a Clover', WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, HInstance, nil);
  ShowWindow(hWindow, CmdShow);
  UpdateWindow(hWindow);

  while Getmessage(Msg, 0, 0, 0) do
  begin
    TranslateMessage(Msg);
    DispatchMessage(Msg);
  end;
end.

posted on 2009-04-26 16:48  zwz_good  阅读(162)  评论(0编辑  收藏  举报