GDI Topics: Rectangles 

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 (X_{1}, Y_{1}) and ends at another point (X_{2}, Y_{2}). 
To draw a rectangle, you can use the CDC::Rectangle() method. Its syntax is: BOOL Rectangle(int x1, int y1, int x2, int y2); As seen on the figure and the formula, a rectangle spans from coordinates (x_{1}, y_{1}) to (x_{2}, y_{2}). Here is an example:
When drawing a rectangle, if the value of x_{2} is less than that of x_{1}, then the x_{2} coordinate would mark the left beginning of the figure. This scenario would also apply if the y_{2} coordinate were lower than y_{1}. To draw a rectangle, you can also use a RECT or a CRect object. The syntax you would use is: BOOL Rectangle(LPCRECT lpRect); In this case, you must have defined a RECT or a CRect 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 x_{1}  x_{2} = y_{1}  y_{2}.
The CDC class provides another function member you can use to draw a rectangle. This time you can control how the edges of the rectangle would be drawn. The method used is called DrawEdge and its syntax is: BOOL DrawEdge(LPRECT lpRect, UINT nEdge, UINT nFlags); The lpRect argument is passed as a pointer to a RECT or CRect, which is the rectangle that would be drawn. The nEdge 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 nFlags value specifies what edge(s) would be drawn. It can have one of the following values:
Here is an example:



Home  Copyright © 20032015, FunctionX  
