Delphi实例之橡皮筋画图的实现
在《Delphi7基础教程》这本书的练习中提到过一个橡皮筋画图的例子,书上的源码是错误的!不知道是打印的错误还是本身源码就有问题,我将它改了过来。
在Form1上放置一个Image组件,Image1的Align设为Client。
1 unit Unit1; 2 3 interface 4 5 6 uses 7 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 8 Dialogs, ExtCtrls; 9 10 type 11 TForm1 = class(TForm) 12 Image1: TImage; 13 procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; 14 Shift: TShiftState; X, Y: Integer); 15 procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, 16 Y: Integer); 17 procedure Image1MouseUp(Sender: TObject; Button: TMouseButton; 18 Shift: TShiftState; X, Y: Integer); 19 private 20 { Private declarations } 21 public 22 { Public declarations } 23 end; 24 25 var 26 Form1: TForm1; 27 Prior,Origin:TPoint;{Oringin用于记录起始位置也就是鼠标按下的位置,Prior用于记录最后的位置也就是放开鼠标的位置} 28 IsDown:boolean=false;{用于判断鼠标是否仍处于按下的状态} 29 30 implementation 31 32 {$R *.dfm} 33 34 procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; 35 Shift: TShiftState; X, Y: Integer); 36 begin 37 IsDown:=true; 38 Image1.Canvas.Moveto(x,y); 39 Origin:=Point(x,y); 40 Prior:=Point(x,y){记录下按下时的位置,此时Origin和Prior重合} 41 end; 42 43 procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, 44 Y: Integer); 45 46 var 47 pX,pY:integer; 48 begin 49 Form1.DoubleBuffered:=true; 50 if isDown then 51 begin 52 pX:=Prior.X; 53 pY:=prior.Y;{着两个值用于保存上一次的位置} 54 Prior:=Point(x,y); 55 Image1.Canvas.MoveTo(Origin.x,origin.Y); 56 Image1.Canvas.Lineto(Prior.X,Prior.Y);{显示现在的线} 57 Image1.Canvas.Pen.Mode:=pmNotXor;{通过异或操作删掉上一条线} 58 Image1.Canvas.MoveTo(Origin.x,origin.Y); 59 Image1.Canvas.Lineto(pX,pY); 60 61 62 end; 63 64 end; 65 66 procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton; 67 Shift: TShiftState; X, Y: Integer); 68 begin 69 IsDown:=false ; 70 Image1.Canvas.Pen.Mode:=pmCopy; 71 72 Image1.Canvas.MoveTo(Origin.x,origin.Y); 73 Image1.Canvas.Lineto(Prior.X,Prior.Y); {放开鼠标,画出最后的线} 74 75 end; 76 77 end.
Delphi实例之橡皮筋画图的实现
时间: 2024-10-11 05:35:28