程序5-4 Bezier

program Bezier;

uses
  Windows,
  Messages;

procedure DrawBezier(hdc1: HDC; apt: array of TPoint);
begin
    PolyBezier(hdc1, apt, 4); //貝塞爾曲線
    // 繪製控制點與端點連線
    MoveToEx(hdc1, apt[0].X, apt[0].Y, nil);
    LineTo(hdc1, apt[1].X, apt[1].Y);

    MoveToEx(hdc1, apt[2].X, apt[2].Y, nil);
    LineTo(hdc1, apt[3].X, apt[3].Y);
end;

function WndProc(hWindow: HWND; msg, wParam, lParam: LongInt): LRESULT; stdcall;
const
{$J+}
  apt: array[0..3] of TPoint = (
  (X:0; Y:0), (X:0; Y:0), (X:0; Y:0), (X:0; Y:0)
  );
{$J-}
var
  ps: TPaintStruct;
  hdc1: HDC;
  cxClient, cyClient: Integer;

begin
  Result:= 0;
  case msg of
  WM_CREATE:
  begin

  end;
  WM_DESTROY:
  begin
    PostQuitMessage(0);
  end;
  WM_SIZE:
  begin
    cxClient:= LoWord(lParam);
    cyClient:= HiWord(lParam);
    //起點
    apt[0].X:= cxClient div 4;
    apt[0].Y:= cyClient div 2;
    //第一控制點
    apt[1].X:= cxClient div 2;
    apt[1].Y:= cyClient div 4;
    //第二控制點
    apt[2].X:= cxClient div 2;
    apt[2].Y:= 3 * cyClient div 4;
    //終點
    apt[3].X:= 3 * cxClient div 4;
    apt[3].Y:= cyClient div 2;
  end;
  WM_LBUTTONDOWN,
  WM_RBUTTONDOWN,
  WM_MOUSEMOVE:
  begin
    if ((wParam and MK_LBUTTON <> 0) or (wParam and MK_RBUTTON <> 0)) then
    begin
      hdc1:= GetDC(hWindow);
      //清除舊線
      SelectObject(hdc1, GetStockObject(WHITE_PEN));//TE_BRUSH
      DrawBezier(hdc1, apt);

      if wParam and MK_LBUTTON <> 0 then
      begin
        apt[1].X:= LoWord(lParam);
        apt[1].Y:= HiWord(lParam);
      end;
      if wParam and MK_RBUTTON <> 0 then
      begin
        apt[2].X:= LoWord(lParam);
        apt[2].Y:= HiWord(lParam);
      end;
      //繪製新線
      SelectObject(hdc1, GetStockObject(BLACK_PEN));//TE_BRUSH
      DrawBezier(hdc1, apt);

      ReleaseDC(hWindow, hdc1);
    end;
  end;
  WM_PAINT:
  begin
    hdc1:= BeginPaint(hWindow, ps);
    DrawBezier(hdc1, apt);
   
    EndPaint(hWindow, ps);
  end
  else
    Result:= DefWindowProc(hWindow, msg, wParam, lParam);
  end;
end;
const
  szAppName = 'Bezier';
var
  wndclass1: TWndClass;
  hWindow: HWND;
  msg: TMsg;
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, 'Bezier Splines', 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.

發佈了21 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章