下面是很入以前的一个程序,模仿windows的画图程序中画曲线.其实原理简单得要命 ,就是一个polybezier函数.
那一位有兴趣的可以封装成一个类,方便以后用.
unit unit1;
interface
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls;
type
tstep=(sstartend,sctrl1,sctrl2);
tform1 = class(tform)
button1: tbutton;
procedure formcreate(sender: tobject);
procedure formmousedown(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
procedure formmousemove(sender: tobject; shift: tshiftstate; x,
y: integer);
procedure formmouseup(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
procedure formpaint(sender: tobject);
private
{ private declarations }
isdown:boolean;
isok:boolean; //标识一次bezier线的绘制是否完成
step:tstep; //指示到那个阶段了
startp:tpoint; //以下是四个点坐标
endp:tpoint;
ctrp1:tpoint;
ctrp2:tpoint;
public
{ public declarations }
end;
var
form1: tform1;
implementation
{$r *.dfm}
procedure tform1.formcreate(sender: tobject);
begin
self.doublebuffered:=true;
isdown:=false;
step:=sstartend;
isok:=false;
end;
procedure tform1.formmousedown(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
begin
isdown:=true;
if button=mbleft then
begin
if (step=sstartend) then
begin
startp.x:=x;
startp.y:=y;
endp:=point(x,y);
isok:=false;
end
else if (step=sctrl1) then
begin
ctrp1:=point(x,y);
ctrp2:=point(x,y);
end
else if (step=sctrl2) then
begin
ctrp2:=point(x,y);
end;
end;
end;
procedure tform1.formmousemove(sender: tobject; shift: tshiftstate; x,
y: integer);
begin
if isdown and (shift=[ssleft]) then
begin
if (step=sstartend) then
begin
endp.x:=x;
endp.y:=y;
end
else if step=sctrl1 then
begin
ctrp1:=point(x,y);
ctrp2:=point(x,y);
end
else if (step=sctrl2) then
begin
ctrp2:=point(x,y);
end;
self.invalidate;
end;
end;
procedure tform1.formmouseup(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
begin
if isdown and (button=mbleft) then
begin
isdown:=false;
if step=sstartend then step:=sctrl1
else if step=sctrl1 then step:=sctrl2
else if step=sctrl2 then begin step:=sstartend; isok:=true; end;
end;
end;
procedure tform1.formpaint(sender: tobject);
begin
if not isok then
begin
if step=sstartend then
begin
self.canvas.moveto(startp.x,startp.y);
self.canvas.lineto(endp.x,endp.y);
end
else begin
self.canvas.polybezier([startp,ctrp1,ctrp2,endp]);
end;
end else begin
self.canvas.polybezier([startp,ctrp1,ctrp2,endp]);
end;
end;
end.