unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin;
type
TAPoint=Array of TPoint;
TLine=Class//线对象.
PointList:TAPoint;//线对象的点列表.
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
SpinEdit1: TSpinEdit;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button3Click(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
FStartLine:Boolean;
LineList:TList;
FPointList:TAPoint;
procedure ClearLine;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
LineList:=TList.Create; //创建列表保存对象.
FStartLine:=False; //是否在画线.
end;
procedure TForm1.ClearLine;//清除线对象.
Var i:Integer;
Line:TLine;
begin
For i:=LineList.Count-1 downto 0 do//释放每一个对象.
begin
Line:=LineList;
Line.Free;
LineList.Delete(i);
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ClearLine;//先清除线对象.
LineList.Free;//释放列表.
end;
procedure TForm1.FormPaint(Sender: TObject);
Var i:Integer;
Line:TLine;
begin
For i:=0 to LineList.Count-1 do
begin
Line:=LineList;
Canvas.Polyline(Line.PointList);//画每一条线对象.
end;
if FStartLine then
begin
if High(FPointList)>=1 then
Canvas.Polyline(FPointList);//如果正在画线,则把未完成的线也画出来.
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FStartLine:=True;
SetLength(FPointList,0); //开始画线清除临时线的点列表.
end;
procedure TForm1.Button2Click(Sender: TObject);
Var Line:TLine;
begin
FStartLine:=False;
if High(FPointList)>=1 then//如果已经有两个以上的点
begin
Line:=TLine.Create;//创建一个线对象.
SetLength(Line.PointList,0);//初始化线对象的点列表.
Line.PointList:=Copy(FPointList,0,Sizeof(TPoint)*(High(FPointList)+1));
//复制当前画好的临时点列表.
LineList.Add(Line);//把对象加入到列表.
Invalidate;//重画Form
end;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Var Count:Integer;
begin
if FStartLine then//如果正在画线.
begin
Count:=High(FPointList)+1;
SetLength(FPointList,Count+1);
FPointList[Count]:=Point(x,y);//增加一个点.
Invalidate;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
Var OldLine,NewLine:TLine;
i,j:Integer;
begin
if (SpinEdit1.Value>0) and (LineList.Count>0) then//如果需要复制数大于0并且已经存在一个以上的线条.
begin
OldLine:=LineList[LineList.Count-1];//取出旧线条.
For i:=0 to SpinEdit1.Value-1 do//复制需要的份数.
begin
NewLine:=TLine.Create;//创建新线条.
SetLength(NewLine.PointList,0);//初始化点列表.
NewLine.PointList:=Copy(OldLine.PointList,0,Sizeof(TPoint)*(High(OldLine.PointList)+1));
//复制点列表.
For j:=0 to High(NewLine.PointList) do//对点进行移位.
begin
NewLine.PointList[j].X:=NewLine.PointList[j].X+10;
NewLine.PointList[j].y:=NewLine.PointList[j].y+10;
end;
LineList.Add(NewLine);//加和到列表.
end;
Invalidate;//重画.
end;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FStartLine and (High(FPointList)>=0) then//这种方法简单不过鼠标移动时会造成闪动.
begin
Invalidate;
Canvas.MoveTo(FPointList[High(FPointList)].x,FPointList[High(FPointList)].y);
Canvas.LineTo(x,y);
end;
end;
end.