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 FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses GDIPOBJ,GDIPAPI; var PtArr:array of TRect; x1,y1,x2,y2:Integer; flag:Boolean; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin flag:=True; x1:=x; y1:=y; x2:=x; y2:=y; end; procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var i:Integer; begin if flag then begin Canvas.DrawFocusRect(Rect(x1,y1,x2,y2)); x2:=x; y2:=y; Canvas.DrawFocusRect(Rect(x1,y1,x2,y2)); end else begin Text:=''; end; for i := Low(PtArr) to High(PtArr) do if PtInRect(ptarr[i], Point(X,Y)) then Text := Format('在第 %d 个矩形中', [i+1]); end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if not flag then Exit; SetLength(PtArr,Length(PtArr)+1); PtArr[High(PtArr)]:=Rect(x1,y1,x2,y2); flag:=False; Repaint; end; procedure TForm1.FormPaint(Sender: TObject); var g:TGPGraphics; p:TGPPen; i:Integer; begin g:=TGPGraphics.Create(Canvas.Handle); g.Clear(aclWhite); p:=TGPPen.Create(aclRed,2); for I := Low(PtArr) to High(PtArr) do begin g.DrawRectangle(p,MakeRect(ptarr[i])); end; p.Free; g.Free; end; end.