System.Types.TRect
Delphi
TRect = record private function GetWidth: Integer; procedure SetWidth(const Value: Integer); function GetHeight: Integer; procedure SetHeight(const Value: Integer); function GetSize: TSize; procedure SetSize(const Value: TSize); function GetLocation: TPoint; public constructor Create(const Origin: TPoint); overload; // empty rect at given origin constructor Create(const Origin: TPoint; Width, Height: Integer); overload; // at TPoint of origin with width and height constructor Create(const Left, Top, Right, Bottom: Integer); overload; // at x, y with width and height constructor Create(const P1, P2: TPoint; Normalize: Boolean = False); overload; // with corners specified by p1 and p2 constructor Create(const R: TRect; Normalize: Boolean = False); overload; class operator Equal(const Lhs, Rhs: TRect): Boolean; class operator NotEqual(const Lhs, Rhs: TRect): Boolean; class operator Add(const Lhs, Rhs: TRect): TRect; class operator Multiply(const Lhs, Rhs: TRect): TRect; class function Empty: TRect; inline; static; procedure NormalizeRect; function IsEmpty: Boolean; function Contains(const Pt: TPoint): Boolean; overload; function Contains(const R: TRect): Boolean; overload; function IntersectsWith(const R: TRect): Boolean; class function Intersect(const R1: TRect; const R2: TRect): TRect; overload; static; procedure Intersect(const R: TRect); overload; class function Union(const R1: TRect; const R2: TRect): TRect; overload; static; procedure Union(const R: TRect); overload; class function Union(const Points: Array of TPoint): TRect; overload; static; procedure Offset(const DX, DY: Integer); overload; procedure Offset(const Point: TPoint); overload; procedure SetLocation(const X, Y: Integer); overload; procedure SetLocation(const Point: TPoint); overload; procedure Inflate(const DX, DY: Integer); overload; procedure Inflate(const DL, DT, DR, DB: Integer); overload; function CenterPoint: TPoint; function SplitRect(SplitType: TSplitRectType; Size: Integer): TRect; overload; function SplitRect(SplitType: TSplitRectType; Percent: Double): TRect; overload; property Width: Integer read GetWidth write SetWidth; property Height: Integer read GetHeight write SetHeight; property Size: TSize read GetSize write SetSize; property Location: TPoint read GetLocation write SetLocation; case Integer of 0: (Left, Top, Right, Bottom: Longint); 1: (TopLeft, BottomRight: TPoint); end;
C++
struct TRect: public RECT{ TRect() { init(0,0,0,0); } TRect(const TPoint& TL) { init(TL.x, TL.y, TL.x, TL.y); } TRect(const TPoint& TL, int width, int height) { init (TL.x, TL.y, TL.x + width, TL.y + height); } TRect(int l, int t, int r, int b) { init(l, t, r, b); } TRect(const TPoint& TL, const TPoint& BR) { init(TL.x, TL.y, BR.x, BR.y); Normalize(); } TRect(const RECT& r) { init(r.left, r.top, r.right, r.bottom); } void init(int l, int t, int r, int b) { left = l; top = t; right = r; bottom = b; } TPoint& TopLeft() { return *((TPoint* )this); } TPoint& BottomRight() { return *((TPoint* )this+1); } const TPoint& TopLeft() const { return *((TPoint* )this); } const TPoint& BottomRight() const { return *((TPoint* )this+1); } int Width() const { return right - left; } int Height() const { return bottom - top ; } static TRect Empty() { return TRect(); } void Normalize() { if (top > bottom) { top = top ^ bottom; bottom = top ^ bottom; top = top ^ bottom; } if (left > right) { left = left ^ right; right = left ^ right; left = left ^ right; } } bool operator ==(const TRect& rc) const { return left == rc.left && top==rc.top && right == rc.right && bottom==rc.bottom; } bool operator !=(const TRect& rc) const { return !(rc==*this); } bool IsEmpty() const { return (right == left) || (bottom == top); } bool Contains(const TPoint& p) const { return ((p.x >= left) && (p.y >= top) && (p.x < right) && (p.y < bottom)); } bool PtInRect(const TPoint& p) const { return Contains(p); } bool Contains(const TRect& r) const { return Contains(r.TopLeft()) && Contains(r.BottomRight()); } bool Overlaps(const TRect &r) const { return IntersectsWith(r); } bool Intersects(const TRect &r) const { return IntersectsWith(r); } bool IntersectsWith(const TRect &r) const { return !( (BottomRight().x < r.TopLeft().x) || (BottomRight().y < r.TopLeft().y) || (r.BottomRight().x < TopLeft().x) || (r.BottomRight().y < TopLeft().y) ); } static TRect Intersect(const TRect &r1, const TRect &r2); void Intersect(const TRect &r); void Union(const TRect &r); static TRect Union(const TRect &r1, const TRect& r2); static TRect Union(const TPoint* points, int npoints) { TPoint tl, br; if (npoints > 0) { tl.SetLocation(points[0]); br.SetLocation(points[0]); for (int i = npoints; --i > 0;) { if (points[i].x < tl.x) tl.x = points[i].x; if (points[i].x > br.x) br.x = points[i].x; if (points[i].y < tl.y) tl.y = points[i].y; if (points[i].y > br.y) br.y = points[i].y; } } return TRect(tl, br); } bool IntersectRect(const TRect &R1, const TRect &R2); bool UnionRect(const TRect &R1, const TRect &R2); void Offset(int DX, int DY) { left += DX; right += DX; top += DY; bottom += DY; } void SetLocation(int X, int Y) { Offset(X - left, Y - top); } void SetLocation(const TPoint& p) { Offset(p.x - left, p.y - top); } void Inflate(int DX, int DY) { left -= DX; right += DX; top -= DY; bottom += DY; } void Inflate(int l, int t, int r, int b) { left -= l; right += r; top -= t; bottom += b; } void NormalizeRect() { int i; if (left > right) { i = left; left = right; right = i; } if (top > bottom) { i = top; top = bottom; bottom = i; } } TPoint CenterPoint() const { return TPoint((left+right)/2, (top+bottom)/2); } TRect CenteredRect(const TRect &CenteredRect) const { int w = CenteredRect.Width(); int h = CenteredRect.Height(); int x = (right + left)/2; int y = (top + bottom)/2; return TRect(x-w/2, y-h/2, x+(w+1)/2, y+(h+1)/2); } #if 0 TRect SplitRect(TSplitRectType SplitType, int Size) const; TRect SplitRect(TSplitRectType SplitType, double Percent) const; #endif #if defined(_Windows) bool SubtractRect(const TRect &R1, const TRect &R2) { return ::SubtractRect(this, &R1, &R2) != 0; } #endif LONG GetWidth() const { return right - left; } void SetWidth(LONG width) { right = left + width; } LONG GetHeight() const { return bottom - top; } void SetHeight(LONG height) { bottom = top + height; } TSize GetSize() const { TSize r; r.cx = GetWidth(); r.cy = GetHeight(); return r; } void SetSize(const TSize& newSize) { SetWidth(newSize.cx); SetHeight(newSize.cy); } TPoint GetLocation() const { return TPoint(left, top); } __property LONG Left = { read=left, write=left }; __property LONG Top = { read=top, write=top }; __property LONG Right = { read=right, write=right }; __property LONG Bottom = { read=bottom, write=bottom }; __property TSize Size = { read=GetSize, write=SetSize }; __property TPoint Location = { read=GetLocation, write=SetLocation }; };
Propriétés
| Type | Visibilité | Source | Unité | Parent |
|---|---|---|---|---|
struct class |
public | System.Types.pas System.Types.hpp |
System.Types | System.Types |
Description
TRect définit un rectangle.
TRect représente les dimensions d'un rectangle. Les coordonnées sont spécifiées soit sous la forme de quatre entiers distincts représentant les bords gauche, supérieur, droit et inférieur du rectangle, soit sous la forme de deux points représentant les emplacements des coins supérieur gauche et inférieur droit.
Typiquement, les valeurs TRect représentent des emplacements en pixels, où l'origine du système de coordonnées en pixels correspond au coin supérieur gauche de l'écran (en coordonnées écran) ou au coin supérieur gauche de la zone client d'un contrôle (en coordonnées client). Quand une valeur TRect représente un rectangle sur l'écran, par convention les bords supérieur et gauche sont considérés comme étant à l'intérieur du rectangle et les bords inférieur et droit à l'extérieur du rectangle. Cette convention permet à la largeur du rectangle d'être considérée Droite - Gauche et la hauteur d'être considérée Inférieur - Supérieur.