Delphi实例之橡皮筋画图的实现

在《Delphi7基础教程》这本书的练习中提到过一个橡皮筋画图的例子,书上的源码是错误的!不知道是打印的错误还是本身源码就有问题,我将它改了过来。

Delphi实例之橡皮筋画图的实现-LMLPHP

在Form1上放置一个Image组件,Image1的Align设为Client。

 unit Unit1;

 interface

 uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls; type
TForm1 = class(TForm)
Image1: TImage;
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1;
Prior,Origin:TPoint;{Oringin用于记录起始位置也就是鼠标按下的位置,Prior用于记录最后的位置也就是放开鼠标的位置}
IsDown:boolean=false;{用于判断鼠标是否仍处于按下的状态} implementation {$R *.dfm} procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IsDown:=true;
Image1.Canvas.Moveto(x,y);
Origin:=Point(x,y);
Prior:=Point(x,y){记录下按下时的位置,此时Origin和Prior重合}
end; procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer); var
pX,pY:integer;
begin
Form1.DoubleBuffered:=true;
if isDown then
begin
pX:=Prior.X;
pY:=prior.Y;{着两个值用于保存上一次的位置}
Prior:=Point(x,y);
Image1.Canvas.MoveTo(Origin.x,origin.Y);
Image1.Canvas.Lineto(Prior.X,Prior.Y);{显示现在的线}
Image1.Canvas.Pen.Mode:=pmNotXor;{通过异或操作删掉上一条线}
Image1.Canvas.MoveTo(Origin.x,origin.Y);
Image1.Canvas.Lineto(pX,pY); end; end; procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IsDown:=false ;
Image1.Canvas.Pen.Mode:=pmCopy; Image1.Canvas.MoveTo(Origin.x,origin.Y);
Image1.Canvas.Lineto(Prior.X,Prior.Y); {放开鼠标,画出最后的线} end; end.
05-11 19:32