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.