C++/MFC project [5] - draw rectangle and ellipse

foreword

Note:  The examples in this article are all implemented in the OnDraw() function in ***View().cpp file.

        To create an MFC single document, please go to C++/MFC project [1] - Create a new project and view the introduction of commonly used classes.

        For the implementation of the custom coordinate system , please go to C++/MFC Project [2] - Custom Plane Cartesian Coordinate System .

1. How to draw a rectangle

1. Use the function directly

To use the function directly, you need to pass in the coordinates (x1, y1, x2, y2) of the diagonal vertices of the rectangle.

//自定义坐标系下实现
//对角顶点为(100,100)(600,300)的矩形
CDC* pDC = GetDC();
pDC->Rectangle(100, 100, 600, 300);

2. Use the Rectangle object to achieve

CPoint P0(100, 100), P1(600, 300);
CRect rect(P0, P1);
CDC* pDC = GetDC();
pDC->Rectangle(rect);

2. Draw an ellipse - Ellipse() function

This function has four parameters, which are similar to the parameters of the drawing rectangle function, and their meanings are the coordinates of the diagonal vertices of the circumscribed rectangle of the ellipse.

//外切矩形顶点坐标(100,100)(600,300)
CDC* pDC = GetDC();
pDC->Ellipse(100, 100, 600, 300);

3. Example

1. Draw a rectangle

Requirements: The left and right borders of the client area are shrunk by 100 pixels, and the upper and lower borders are shrunk by 50 pixels. Use a 5 px wide yellow border line and fill the interior with other colors.

	rect.DeflateRect(100, 50);//左右上下收缩像素
	CPen NewPen, * OldPen;
	NewPen.CreatePen(0, 5, 0x00FFFF);//实线,5像素宽,黄色
	OldPen = pDC->SelectObject(&NewPen);//选入设备上下文,保留原画笔的指针
	CBrush NewBrush, * OldBrush;
	NewBrush.CreateSolidBrush(0xFFFF00);
	OldBrush = pDC->SelectObject(&NewBrush);//选入设备上下文,保留原画刷的指针
	pDC->Rectangle(rect);//绘制矩形,参数为rect
	pDC->SelectObject(OldPen);
	pDC->SelectObject(OldBrush);

The result of the operation is as follows:

2. Draw an ellipse

Requirement: After drawing an ellipse, draw its circumscribed rectangle and display the inscribed circle at the same time.

    rect.DeflateRect(100, 100);//上下左右向中心缩小100个像素
	int r = rect.Height() / 2;
	CRect rect1(CPoint(-r, -r), CPoint(r, r));
	pDC->Ellipse(rect1);//正方形rect1内切圆
	pDC->Ellipse(rect);//矩形内切椭圆
	pDC->Rectangle(rect);//绘制矩形函数

The result of the operation is as follows:

? ? ? Ellipses and circles are "not drawn" in the client area, only rectangles are drawn!

Put the drawing rectangle function before drawing the ellipse, and then run it, as follows:

    rect.DeflateRect(100, 100);//上下左右向中心缩小100个像素
	int r = rect.Height() / 2;
	CRect rect1(CPoint(-r, -r), CPoint(r, r));
	pDC->Ellipse(rect1);//正方形rect1内切圆
	pDC->Rectangle(rect);//绘制矩形函数
	pDC->Ellipse(rect);//矩形内切椭圆

 

We found that this time both the ellipse and the rectangle are displayed, while the circle is still not displayed. In fact, the circle is not drawn, but is covered by the graphics drawn later.

As mentioned in the previous article , the brush is filled with white by default . In other words, when we draw a rectangle or ellipse, its interior is filled, but it is the same color as the background, so this may be ignored.

If after drawing a rectangle, draw an ellipse ( pDC->Ellipse(0, 0, 400, 300); ), what is the result?

As shown in the figure below, you can see that the ellipse drawn later covers part of the rectangle drawn earlier.

 So, how to prevent the graphics drawn first from being overwritten? Just set the transparent brush.

3. Use of transparent brushes

    rect.DeflateRect(100, 100);//上下左右向中心缩小100个像素
	CBrush* pOldBrush = (CBrush*)pDC->SelectStockObject(5);//透明画刷宏定义参数为5
	int r = rect.Height() / 2;
	CRect rect1(CPoint(-r, -r), CPoint(r, r));
	pDC->Ellipse(rect1);//正方形rect1内切圆
	pDC->Ellipse(rect);//矩形内切椭圆
	pDC->Rectangle(rect);//绘制矩形函数
	pDC->SelectObject(pOldBrush);

Here, the drawing order is still circle, ellipse, and rectangle, and the running results are as follows:

Guess you like

Origin blog.csdn.net/zhou2622/article/details/129865014