canvas画任意形状,在线等(急)。 ( 积分: 150 )

  • 主题发起人 主题发起人 dg_hy
  • 开始时间 开始时间
D

dg_hy

Unregistered / Unconfirmed
GUEST, unregistred user!
我可以使用Canvas画直线、圆...
但是,
1,我如何使用Canvas画出任意的形状?就是拿鼠标在上面乱画。
2,我如何撤销上一次画图的操作。
 
我可以使用Canvas画直线、圆...
但是,
1,我如何使用Canvas画出任意的形状?就是拿鼠标在上面乱画。
2,我如何撤销上一次画图的操作。
 
问题1,在MouseMove中处理即可
问题2,使用异或,重画一次即可擦除上次画的图
 
刚刚给你做的代码,测试过的,没问题的
一个点集类,用于擦除上次画的图
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;
 
在 Image1MouseDown中加一句
pts.RemoveAll; //清除上次画图数据
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部