Applications Accessories: The Rectangle



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;

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:

TRect = record
	Left: Integer;
	Top: Integer;
	Right: Integer;

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:

procedure TForm1.FormCreate(Sender: TObject);
var Recto : TRect;
	Recto.Top    := 100;
	Recto.Left   := 120;
	Recto.Right  := 420;
	Recto.Bottom := 288;




Home Copyright © 2010 FunctionX, Inc.