C++/MFC project [4] - drawing a straight line segment

1. MoveTo() function and LineTo() function

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

The two functions have been applied in the previous section C++/MFC project [2] - custom plane Cartesian coordinate system .

The MoveTo() function only sets the starting point and does not draw the line.

The LineTo() function draws a line. After the line is drawn, the starting point becomes the parameter point in the LineTo() function, which is the end point of the line drawn this time (it will be reflected in the following example, which is easy to understand) .

//绘制起点为P0(10,10)、终点为P1(100,20)的直线

CDC* pDC = GetDC();

//一:定义两点P0,P1,传参画线
CPoint P0,P1;
P0=(10,10);
P1=(100,20);
pDC->MoveTo(P0);
pDC->LineTo(P1);

//二、直接传参横纵坐标
pDC->MoveTo(10,10);
pDC->LineTo(100,20);

Two, brush CPen class and brush CBrush class

Both CPen and CBrush classes belong to the GDI tool class. CPen defaults to a solid black line with a width of 1 pixel, and CBrush defaults to filling white. The two must be selected into the device context before use ; the device context must be restored after use .

1. Create a brush function

CreatePen(int PenStyle, int PenWidth, PenColor), the parameter meanings are: pen style, pen width, pen color.

Brush styles include solid line, dotted line, dotted line, etc. Interested students can search and learn by themselves.

There are two ways to express the color of the brush. For details, see the first article of C++/MFC (3. A brief introduction to color representation).

2. Create a brush function

CreateSolidBrush (BrushColor), parameter meaning: brush color.

3. Select the device context

① brush

OldPen = pDC->SelectObject(&NewPen);

OldPen is the original brush pointer, and NewPen is the newly created brush.

② Painting

OldBrush = pDC->SelectObject(&NewBrush);

OldBrush is the original brush pointer, and NewBrush is the newly created brush.

4. Restore the device context

① brush

pDC->SelectObject(OldPen);

pDC is the pointer of CDC class, and OldPen is the pointer of the original paintbrush.

② Painting

pDC->SelectObject(OldBrush);

pDC is the CDC class pointer, and OldBrush is the original brush pointer.

3. Example

1. Draw a triangle

//自定义坐标系下实现
//绘制顶点为p0,P1,P2的三角形
CPoint P0(-200, -100), P1(200, -100), P2(0, 200);
pDC->MoveTo(P0);
pDC->LineTo(P1);
pDC->LineTo(P2);
pDC->LineTo(P0);

The result of the operation is as follows: 

Note: Please check the custom coordinate system in C++/MFC Project [2] - Custom Plane Cartesian Coordinate System  .

2. Draw a straight line according to the requirements

Requirement: Draw a solid green line 1 pixel wide from P0 to P1 and a solid blue line 3 pixels wide from P1 to P2.

//自定义坐标系下实现
CPoint P0(-100, -100), P1(30, 20), P2(100, 150);
CPen greenPen, bluePen, * pOldPen; //OldPen用来保存原指针
greenPen.CreatePen(0, 1, RGB(0, 255, 0));//创建画笔,(样式,宽度,颜色)
pOldPen = pDC->SelectObject(&greenPen);//选入上下文
pDC->MoveTo(P0);
pDC->LineTo(P1);//当前位置保持在P1位置
pDC->SelectObject(pOldPen);//恢复上下文
bluePen.CreatePen(0, 3, RGB(0, 0, 255));//创建画笔,(样式,宽度,颜色)
pOldPen = pDC->SelectObject(&bluePen);//选入上下文
pDC->LineTo(P2);
pDC->SelectObject(pOldPen);//恢复上下文

The result of the operation is as follows: 

Note: LineTo only retains the starting point color, not the end point color, so P1 point is blue.

3. Draw the "diamond" pattern

Diamond - each vertex is an equal point on a circle, and all points are connected.

    //自定义坐标系下实现
    int r = 200, n = 12;//半径为200,等分为12份
	CPoint P[12];
	double Theta = 2 * PI / n;//等分角的角度
	for (int i = 0; i < n; i++)
	{
		P[i].x = ROUND(r * cos(i * Theta));//计算坐标,宏ROUND将结果转换为整型变量
		P[i].y = ROUND(r * sin(i * Theta));
	}
	for (int i = 0; i <= n - 2; i++)
	{
		for (int j = i + 1; j <= n - 1; j++)//连接各个点,避免线段的重复绘制
		{
			pDC->MoveTo(P[i]);
			pDC->LineTo(P[j]);
		}
	}

Note: The ROUND macro is defined as: #define ROUND(d) int(floor(d) + 0.5)

The result of the operation is as follows:

 In this example, the circle is divided into 12 equal parts, and the n value and the size of the array P are modified, and the following results can also be obtained:

    

 As above, they are the results of ten equal divisions and fifteen equal divisions respectively, which have the beauty of symmetry. It is not difficult to find that as the value of n increases, the graph tends to be more round.

Guess you like

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