GDI Topics: Rectangles and Squares |
|
Introduction |
A rectangle is a geometric figure made of four sides that compose four right angles. Like the line, to draw a rectangle, you must define where it starts and where it ends. This can be illustrated as follows: |
The drawing of a rectangle typically starts from a point defined as (X1, Y1) and ends at another point (X2, Y2). To draw a rectangle, you can use the TCanvas::Rectangle() method. Its syntax is: void __fastcall Rectangle(int X1, int Y1, int X2, int Y2); As seen on the figure and the formula, a rectangle spans from coordinates (x1, y1) to (x2, y2). Here is an example:
When drawing a rectangle, if the value of x2 is less than that of x1, then the x2 coordinate would mark the left beginning of the figure. This scenario would also apply if the y2 coordinate were lower than y1. To draw a rectangle, you can also use a RECT or a TRect object. The syntax you would use is: void __fastcall Rectangle(TRect Rect); In this case, you must have defined a RECT or a TRect value and pass it as a pointer to the Rectangle() method. Here is an example:
A square is a rectangle whose sides are all equal. Therefore, to draw a square, when specifying the arguments of the Rectangle() method, make sure that |x1 - x2| = |y1 - y2|. |
A Rectangle With Edges |
The Win32 library provides another function you can use to draw a rectangle. This time you can control how the edges of the rectangle would be drawn. The function used is called DrawEdge and its syntax is: BOOL DrawEdge(HDC hdc, LPRECT qrc, UINT edge, UINT grfFlags); The hdc argument represents a handle of the canvas on which you want to draw. The qrc argument is passed as a pointer to a RECT or TRect, which is the rectangle that would be drawn. The edge value specifies how the interior and the exterior of the edges of the rectangle would be drawn. It can be a combination of the following constants:
These values can be combined using the bitwise OR operator. On the other hand, you can use the following constants instead:
The grfFlags value specifies what edge(s) would be drawn. It can have one of the following values: |
Value | Description |
BF_RECT | The entire rectangle will be drawn |
BF_TOP | Only the top side will be drawn |
BF_LEFT | Only the left side will be drawn |
BF_BOTTOM | Only the bottom side will be drawn |
BF_RIGHT | Only the right side will be drawn |
BF_TOPLEFT | Both the top and the left sides will be drawn |
BF_BOTTOMLEFT | Both the bottom and the left sides will be drawn |
BF_TOPRIGHT | Both the top and the right sides will be drawn |
BF_BOTTOMRIGHT | Both the bottom and the right sides will be drawn |
BF_DIAGONAL_ENDBOTTOMLEFT | A diagonal line will be drawn from the top-right to the bottom-left corners |
BF_DIAGONAL_ENDBOTTOMRIGHT | A diagonal line will be drawn from the top-left to the bottom-right corners |
BF_DIAGONAL_ENDTOPLEFT | A diagonal line will be drawn from the bottom-right to the top-left corners |
BF_DIAGONAL_ENDTOPRIGHT | A diagonal line will be drawn from the bottom-left to the top-right corners |
Here is an example:
//--------------------------------------------------------------------------- void __fastcall TForm1::FormPaint(TObject *Sender) { TRect Recto(20, 20, 225, 115); HDC hDC = Canvas->Handle; DrawEdge(hDC, &Recto, BDR_RAISEDOUTER | BDR_SUNKENINNER, BF_RECT); } //--------------------------------------------------------------------------- |
Home | Copyright © 2004-2016, FunctionX, Inc. | |