MFC realizes the mouse to draw basic graphics (point, line, polyline, rectangle, circle)

1 Experiment purpose and requirements

  • Familiar with the development environment, use MFC package function to draw simple graphics;
  • In OnDraw, use MFC package functions (MoveTo, LineTo, Rectangle, Ellipse, etc.) to draw points, lines, rectangles, ellipses/circles.
  • Finally complete the mouse drawing graphics

2 basic principles

MFC is Microsoft Basic Class Library

3 Main instruments and equipment

Window10,Visual Studio 2019

4 Experimental procedures/data processing and results

4.1 Click to automatically generate pattern version

  • (1) Design window
    Insert picture description here

  • (2) Use the class wizard to quickly establish message mapping
    Insert picture description here

  • (3) Click to jump to write processing function

①Point function

void CEXP1View::OnDrawPoint()
{
	// TODO: 在此添加命令处理程序代码
	CDC* pDC = GetDC();
	pDC->SetPixel(200, 200, RGB(255, 0, 0));
}

②Line function

void CEXP1View::OnDrawLinear()
{
	// TODO: 在此添加命令处理程序代码
	CClientDC dc(this);
	dc.MoveTo(20, 20);
	dc.LineTo(100, 100);
}

③Polyline function

void CEXP1View::OnDrawLine()
{
	// TODO: 在此添加命令处理程序代码
	CClientDC dc(this);
	POINT p[4];

	p[0].x = 400;
	p[0].y = 300;

	p[1].x = 500;
	p[1].y = 500;

	p[2].x = 300;
	p[2].y = 200;

	p[3].x = 200;
	p[3].y = 500;

	dc.Polyline(p, 4);
}}

④Rectangular function

void CEXP1View::OnDrawRectangular()
{
	// TODO: 在此添加命令处理程序代码
	CClientDC dc(this);
	dc.Rectangle(500, 500, 800, 700);

⑤Elliptic function

void CEXP1View::OnDrawElliptic()
{
	// TODO: 在此添加命令处理程序代码
	CClientDC dc(this);
	dc.Ellipse(400, 300, 500,500);
}
  • (4) Operation results:
    Insert picture description here

4.2 Mouse drawing generation

  • (1) Modify CEXP1View.h to declare the parameter variables required by the drawing function
#pragma once
#include<afxtempl.h>
class CEXP1View : public CView
{
protected: // 仅从序列化创建
	CEXP1View() noexcept;
	DECLARE_DYNCREATE(CEXP1View)
// 特性
public:
	CEXP1Doc* GetDocument() const;
	//起始点与终点
	CPoint m_ptStart, m_ptEnd;
	//制图类型
	UINT m_Type;
	//数组
	CArray<CPoint, CPoint> m_ptArray;

// 操作
public:
……
}
  • (2) Modify the drawing function in (3) in 4.1:
void CEXP1View::OnDrawPoint()
{
	// TODO: 在此添加命令处理程序代码

	//CDC* pDC = GetDC();
	//pDC->SetPixel(200, 200, RGB(0, 0, 255));
	
	m_Type = 0;
}

void CEXP1View::OnDrawLinear()
{
	// TODO: 在此添加命令处理程序代码
	//CClientDC dc(this);
	//dc.MoveTo(20, 20);
	//dc.LineTo(100, 100);
	
	m_Type = 1;
}

void CEXP1View::OnDrawLine()
{
	// TODO: 在此添加命令处理程序代码
	//CClientDC dc(this);
	//POINT p[4];

	//p[0].x = 200;
	//p[0].y = 300;

	//p[1].x = 300;
	//p[1].y = 400;

	//p[2].x = 300;
	//p[2].y = 450;

	//p[3].x = 200;
	//p[3].y = 500;

	//dc.Polyline(p, 4);
	m_Type = 4;
}

void CEXP1View::OnDrawRectangular()
{
	// TODO: 在此添加命令处理程序代码
	/*CClientDC dc(this);
	dc.Rectangle(400, 400, 500, 500);*/
	m_Type = 2;
}

void CEXP1View::OnDrawRound()
{
	// TODO: 在此添加命令处理程序代码
	//CClientDC dc(this);
	//dc.Ellipse(200, 200, 300, 300);
	m_Type = 3;
}
  • (3) Write the processing function for pressing and lifting the left mouse button, and double-clicking the left button (this function is used to cancel the drawing of the polyline)

Left button up

void CEXP1View::OnLButtonUp(UINT nFlags, CPoint point){
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CView::OnLButtonUp(nFlags, point);
	ReleaseCapture();
	//记鼠标录抬起位置
	m_ptEnd = point;
	m_ptArray[1] = point;
	/*m_ptArray.Add(point);*/
    //设备环境对象
	CClientDC dc(this);
	CDC* pDC = GetDC();
	//使用case流程语句实现跳转
	switch (m_Type){
		case 0: //点
			pDC->SetPixel(m_ptStart.x,m_ptStart.y, RGB(0, 0, 255));
			break;
		case 1: //直线
			dc.MoveTo(m_ptStart);
			dc.LineTo(m_ptEnd);
			break;
		case 2: //矩形
			dc.Rectangle(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y);
			break;	
		case 3: //椭圆和圆
			dc.Ellipse(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y);
			break;
		case 4: //折线
			if (mask == 1) {
				dc.Polyline(m_ptArray, 2);
			}
			else{
				m_ptArray[0].x = 0;
				m_ptArray[1].x = 0;
			}
			m_ptArray[0] = m_ptArray[1];
			break;
		default:
			break;
	}
	//释放
	ReleaseDC(pDC);
}

Left click

void CEXP1View::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	SetCapture();
	//记录按下位置的坐标
	m_ptStart = point;
	//添加按下位置到数组中
	//注意起始点的位置是x与y一样!!!
	
	mask = 1;
	if ((m_ptArray[0].x != 0 )& (m_ptArray[0].y != 0) &(m_ptArray[1].x != 0) &(m_ptArray[1].y != 0))
	{
		m_ptArray[1] = point;
	}
	else
	{
		m_ptArray[0] = point;
	}
	//m_ptArray.Add(point);
	CView::OnLButtonDown(nFlags, point);
}

Double left click

void CEXP1View::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	//用于清空数组,结束折线绘制
	mask = 0;
	m_ptArray[0].x = 0;
	m_ptArray[1].x = 0;
	CView::OnLButtonDblClk(nFlags, point);
}
  • result

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45549336/article/details/109043530