GDI
Topics: Rectangle, DrawEdge


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:
void CExoView::OnDraw(CDC* pDC)
{
pDC>Rectangle(20, 20, 226, 144);
}


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:
void CExoView::OnDraw(CDC* pDC)
{
CRect Recto(328, 125, 48, 25);
pDC>Rectangle(&Recto);
}


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:

Value 
Description 

BDR_RAISEDINNER 
The
interior edge will be raised 

BDR_SUNKENINNER 
The
interior edge will be sunken 

BDR_RAISEDOUTER 
The
exterior edge will be raised 

BDR_SUNKENOUTER 
The
exterior edge will be sunken 
These
values can be combined using the bitwise OR
operator. On the other hand, you can use the
following constants instead:

Value 
Used
For 

EDGE_DUMP 
BDR_RAISEDOUTER
 BDR_SUNKENINNER 

EDGE_ETCHED 
BDR_SUNKENOUTER
 BDR_RAISEDINNER 

EDGE_RAISED 
BDR_RAISEDOUTER
 BDR_RAISEDINNER 

EDGE_SUNKEN 
BDR_SUNKENOUTER
 BDR_SUNKENINNER 
The
nFlags 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
topright to the bottomleft corners 

BF_DIAGONAL_ENDBOTTOMRIGHT 
A
diagonal line will be drawn from the
topleft to the bottomright corners 

BF_DIAGONAL_ENDTOPLEFT 
A
diagonal line will be drawn from the
bottomright to the topleft corners 

BF_DIAGONAL_ENDTOPRIGHT 
A
diagonal line will be drawn from the
bottomleft to the topright corners 
Here
is an example:
void CExoView::OnDraw(CDC* pDC)
{
CRect Recto(20, 20, 225, 115);
pDC>DrawEdge(&Recto, BDR_RAISEDOUTER  BDR_SUNKENINNER, BF_RECT);
}


