|
A rectangle is a geometric figure that has four sides
(for this reason, it is called a quadrilateral figure). It is known for its
horizontal and its vertical measures. The horizontal measure of a rectangle
is referred to as its length and sometimes its width. The vertical measure
is known as its height. In fact, on a Cartesian coordinate, a rectangle is
represented by its four corners (x1, y1, x2, y2) as illustrated on the left
figure below:
|
To support rectangles, the Win32 provides a structure
named RECT. It is defined as follows:
typedef struct _RECT {
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *PRECT;
The RECT structure can be illustrated as
follows:
Notice that the left and the top measures are the same
as the Left and the Top values of a control but the right
and the bottom values are not the width and the height of a control. This
is not an anomaly. It was done like that on purpose.
You can use the Win32 RECT structure wherever
you need to define a rectangle in your VCL applications. Besides the
RECT structure, the VCL provides its own class to represent a
rectangle. It is called TRect and it is defined as follows:
struct TRect
{
TRect() {}
TRect(const TPoint& TL, const TPoint& BR) { left=TL.x; top=TL.y;
right=BR.x; bottom=BR.y; }
TRect(int l, int t, int r, int b) { left=l; top=t; right=r; bottom=b; }
TRect(RECT& r)
{
left = r.left;
top = r.top;
right = r.right;
bottom = r.bottom;
}
int Width () const { return right - left; }
int Height() const { return bottom - top ; }
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); }
__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 };
};
The TRect class can be illustrated as follows:
It is important to notice that, to conform to the
RECT structure, the TRect class provides two constructors:
TRect(int l, int t, int, r, int b) and TRect(RECT& r). The
first is used to simply reproduce the member variables of the RECT
structure and the second is used to create a copy of RECT. Once
these two constructors have been defined making the RECT member
variables available, the class declares two new methods. The Width()
member function returns the distance from the left to the right borders of
the rectangle. The Height() method performs the equivalent
calculation vertically.
One of the ways you can use the TRect class
consists of declaring a rectangle using its default constructor and then
specifying a value for each on of its members. Here is an example:
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TRect Recto = TRect;
Recto.Top = 100;
Recto.Left = 120;
Recto.Right = 420;
Recto.Bottom = 288;
}
//---------------------------------------------------------------------------
If you do not know or cannot get the right and the
bottom measures of the rectangle but know its width and its height, you
can still use them to declare and initialize a rectangle. The TRect
class provides one more alternative you can use to declare a rectangle.
Suppose you have the coordinates of the top-left and the bottom-right
borders of the rectangle, you can declare and initialize it. The
constructor you would use is:
TRect(TPoint TopLeft, TPoint BottomRight);