Delphi TRect函数例子

时间:2022-01-22 03:43:37
{
 
在网上看到个这个例子感觉还不错,将它移到自己的博客里没事的时候看看;
 
TRect
   作用:保存一个矩形的左上角和右下角的坐标值;
 
   声明:
 
    type TRect = packed record
    case integer of
 
      0:(Left, Top, Right, Botton:Integer);
      1:(TopLeft, BottonRight:TPoint);
    end;
 
    由上面的TRect可知,TRect是一个记录类型(TRect),保存了矩形的左上角右下角两个点,4个坐标
    或2个点的值。
    这个声明是Record类型使用Case子句的典型的例子。
    当我们通TopLeft和BottonRight两个点创建矩形时,可以传递两个TPoint值或用Point函数产生他们;
 
}
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  rectangle1, rectangle2:TRect;
begin
  //用Rectangle来手工设置一个矩形;
  Rectangle1.Left := 0;
  rectangle1.Top := 0;
  rectangle1.Right := 40;
  rectangle1.Bottom := 60;
 
  //再用Rect函数设置第二个矩形;
  Rectangle2 := Rect(Point(20, 40), Point(60, 80));
 
  //显示每个矩形的左上角和左下角的坐标
 
  ShowMessageFmt('Rectangle 1 coords = %d, %d, %d, %d',
                 [rectangle1.left,
                  rectangle1.top,
                  rectangle1.Right,
                  rectangle1.Bottom]);
 
  ShowMessageFmt('Rectangle 2 coords = %d, %d, %d, %d',
                 [rectangle2.Left,
                  rectangle2.Top,
                  rectangle2.Right,
                  rectangle2.Bottom]);
 
end;
 
 
end.