unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormPaint(Sender: TObject); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses GDIPOBJ,GDIPAPI; var //定义4个指针变量,并赋予初值 pts:array[0..3] of TGPPoint = ((x:30;y:100),(x:120;y:10),(x:170;y:150),(x:220;y:100)); ptn:Integer = -1; flag:Boolean; //定义标志变量,它决定了是否该绘画 procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin flag:=True; //鼠标左键被按下,可以绘画了,所以标志位致True end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var R:TRect; i:Integer; begin if flag then begin if ptn = -1 then Exit; pts[ptn].X:=x; //如果鼠标按下,同时还命中了以四个坐标点所画的蓝色矩形,那么保存移动到的新位置 pts[ptn].Y:=y; Repaint; end else //-----------------鼠标在客户区移动而没有按下鼠标左键时执行以下代码---------------------------- begin ptn:=-1; for I := 0 to 3 do begin R:=Rect(pts[i].X-2,pts[i].Y-2,pts[i].X+2,pts[i].Y+2); if PtInRect(R,Point(x,y)) then ptn:=i; //PtInRect是矩形命中函数,参数1是个矩形,参数2是个坐标 end; end; //------------------如果鼠标移动到任何一个蓝色矩形,那么么ptn就有正值(不是-1)了, //-------------------而当鼠标左键按下同时又命中蓝色矩形时,这部分代码就暂时不执行了------------- end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin flag:=False; //鼠标左键抬起就不再画了,所以标志位致False end; procedure TForm1.FormPaint(Sender: TObject); var g:TGPGraphics; p:TGPPen; i:Integer; begin Form1.DoubleBuffered:=True; g:=TGPGraphics.Create(Canvas.Handle); g.Clear(aclWhite); p:=TGPPen.Create(aclRed,1); g.DrawBezier(p,pts[0],pts[1],pts[2],pts[3]); //根据4个坐标点变量画塞贝尔曲线 p.SetColor(aclBlue); //画笔颜色设为蓝色并开始逐个按4个坐标点变量画4个很小的矩形 for I := 0 to 3 do begin g.DrawRectangle(p,MakeRect(Rect(pts[i].X-2,pts[i].Y-2,pts[i].X+2,pts[i].Y+2))); end; p.Free; g.Free; end; end.