刚刚给你做的代码,测试过的,没问题的
一个点集类,用于擦除上次画的图
type
TPoints = class
private
FCount : SmallInt;
FPoints : array of TPoint;
function GetCount :SmallInt;
function Getpt(Index: Integer) :TPoint;
public
constructor Create;
destructor Destroy; override;
function Add(pt: TPoint): SmallInt;
procedure Remove(Index: Integer);
procedure RemoveAll;
property Items[Index: Integer]: TPoint read Getpt;
published
property Count :SmallInt read GetCount;
end;
constructor TPoints.Create;
begin
inherited Create;
FCount := 0;
SetLength(FPoints, 0);
end;
destructor TPoints.Destroy;
begin
FCount := 0;
SetLength(FPoints, 0);
inherited;
end;
function TPoints.Add(pt: TPoint): SmallInt;
begin
FCount := Length(FPoints) + 1;
SetLength(FPoints, FCount);
FPoints[FCount - 1] := pt;
Result := FCount - 1;
end;
procedure TPoints.Remove(Index: Integer);
var i: integer;
begin
if Index <= FCount - 1 then
begin
for i := Index to FCount - 2 do
begin
FPoints := FPoints[i + 1];
end;
FCount := FCount - 1;
SetLength(FPoints, FCount);
end;
end;
procedure TPoints.RemoveAll;
begin
FCount := 0;
SetLength(FPoints, 0);
end;
function TPoints.Getpt(Index: Integer) :TPoint;
begin
if Index <= FCount - 1 then
begin
Result := FPoints[Index];
end;
end;
function TPoints.GetCount: SmallInt;
begin
FCount := Length(FPoints);
Result := FCount;
end;
var //变量
isDraw :Boolean;
pts: TPoints;
//全部代码
procedure TForm1.FormCreate(Sender: TObject);
begin
pts := TPoints.Create;
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
isDraw := True;
pts.RemoveAll; //清除上次画图数据
pts.Add(Point(X, Y));
image1.Canvas.Pen.Mode := pmCopy;
image1.Canvas.MoveTo(X, Y);
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if isDraw then
begin
pts.Add(Point(X, Y));
with image1.Canvas do
begin
LineTo(X, Y);
end;
end;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
isDraw := False;
end;
//清除
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
if pts.Count > 0 then
begin
with image1.Canvas do
begin
Pen.Mode := pmNotXor;
MoveTo(pts.Items[0].X, pts.Items[0].Y);
for i := 1 to pts.Count - 1 do
begin
LineTo(pts.Items.X, pts.Items.Y);
end;
Pen.Mode := pmCopy;
end;
end;
pts.RemoveAll;
end;