unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;Button2: TButton;Button3: TButton;Button4: TButton;Button5: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button4Click(Sender: TObject);procedure Button5Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}//使用 API 函数: SetRect procedure TForm1.Button1Click(Sender: TObject); varR: TRect; beginSetRect(R, 20, 20, 80, 80);Canvas.Pen.Color := clBlue;Canvas.Rectangle(R); end;//使用 Classes.Rect 函数 procedure TForm1.Button2Click(Sender: TObject); varR: TRect; beginR := Rect(20, 20, 80, 80);Canvas.Pen.Color := clFuchsia;Canvas.Rectangle(R); end;//使用 Classes.Rect 函数, 用两个点合成 procedure TForm1.Button3Click(Sender: TObject); constpt1: TPoint = (x:20; y:20);pt2: TPoint = (x:80; y:80); varR: TRect; beginR := Rect(pt1, pt2);Canvas.Pen.Color := clSkyBlue;Canvas.Rectangle(R); end;//矩形常量 procedure TForm1.Button4Click(Sender: TObject); constR: TRect = (Left:20; Top:20; Right:80; Bottom:80); beginCanvas.Pen.Color := clTeal;Canvas.Rectangle(R); end;//使用 Classes.Bounds 函数, 我最喜欢这种办法 procedure TForm1.Button5Click(Sender: TObject); varR: TRect; beginR := Bounds(20, 20, 60, 60);Canvas.Pen.Color := clWhite;Canvas.Rectangle(R); end;end.
本例分别用五种办法初始化了同样的一个矩形, 运行效果图:
转载于:https://www.cnblogs.com/del/archive/2008/05/23/1205628.html