随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

本例分别用五种办法初始化了同样的一个矩形, 运行效果图:


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.

posted on 2008-05-23 11:50  万一  阅读(...)  评论(...编辑  收藏