unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormResize(Sender: TObject); procedure FormPaint(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses GDIPOBJ,GDIPAPI; var RectArr:array[0..31] of TGPRect; //声明矩形数组 procedure TForm1.FormPaint(Sender: TObject); var g:TGPGraphics; p:TGPPen; begin form1.DoubleBuffered:=True; //双缓冲开启就没那么闪烁了 g:=TGPGraphics.Create(Canvas.Handle); g.Clear(aclWhite); p:=TGPPen.Create(aclRed,2); g.DrawRectangles(p,PGPRect(@RectArr),Length(RectArr)); //根据矩形数组画一组矩形 p.Free; g.Free; end; procedure TForm1.FormResize(Sender: TObject); var wi,hi,i:Integer; rect:TGPRect; begin rect.X:=0; rect.Y:=0; rect.Width:=ClientWidth; rect.Height:=ClientHeight; wi:=rect.Width div Length(RectArr); //计算32个矩形宽高的平均间距 hi:=rect.Height div Length(RectArr); for I := 0 to Length(RectArr) - 1 do //通过间距求出每一个矩形的坐标和宽高,并保存到数组中; begin inc(rect.X,wi); inc(rect.Y,hi); Dec(rect.Width,wi*2); //因为坐标值是由0而不是1开始的,因此坐标XY加一份平均间距,宽高就要减两份 Dec(rect.Height,hi*2); RectArr[i]:=rect; //保存到数组 end; Repaint; end; end.