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:

Rectangle

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 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 (x1, y1) to (x2, y2). Here is an example:

void CExoView::OnDraw(CDC* pDC)
{
	pDC->Rectangle(20, 20, 226, 144);
}
Another Rectangle

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 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);
}
Rectangle from Rect

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 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 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 CExoView::OnDraw(CDC* pDC)
{
	CRect Recto(20, 20, 225, 115);
	pDC->DrawEdge(&Recto, BDR_RAISEDOUTER | BDR_SUNKENINNER, BF_RECT);
}
Drawing 3-D edges of a rectangle

 

 

Home Copyright © 2003-2004 FunctionX, Inc.