本例分別用五種辦法初始化了同樣的一個矩形, 運行效果圖:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = 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;
var
Form1: TForm1;
implementation
{$R *.dfm}
//使用 API 函數: SetRect
procedure TForm1.Button1Click(Sender: TObject);
var
R: TRect;
begin
SetRect(R, 20, 20, 80, 80);
Canvas.Pen.Color := clBlue;
Canvas.Rectangle(R);
end;
//使用 Classes.Rect 函數
procedure TForm1.Button2Click(Sender: TObject);
var
R: TRect;
begin
R := Rect(20, 20, 80, 80);
Canvas.Pen.Color := clFuchsia;
Canvas.Rectangle(R);
end;
//使用 Classes.Rect 函數, 用兩個點合成
procedure TForm1.Button3Click(Sender: TObject);
const
pt1: TPoint = (x:20; y:20);
pt2: TPoint = (x:80; y:80);
var
R: TRect;
begin
R := Rect(pt1, pt2);
Canvas.Pen.Color := clSkyBlue;
Canvas.Rectangle(R);
end;
//矩形常量
procedure TForm1.Button4Click(Sender: TObject);
const
R: TRect = (Left:20; Top:20; Right:80; Bottom:80);
begin
Canvas.Pen.Color := clTeal;
Canvas.Rectangle(R);
end;
//使用 Classes.Bounds 函數, 我最喜歡這種辦法
procedure TForm1.Button5Click(Sender: TObject);
var
R: TRect;
begin
R := Bounds(20, 20, 60, 60);
Canvas.Pen.Color := clWhite;
Canvas.Rectangle(R);
end;
end.