基于MFC开发的单文档画图(线段,多线段,矩形,椭圆,多边形),选中,变色,平移,线段长度,区域面积计算

1.新建一个MFC应用程序,选择单个文档

 

2.添加菜单

3.添加事件响应 (以绘制线段为例,点击线段,右键,添加事件处理程序)

 4.选择类

 5.实现线段绘制

CDrawingLine::CDrawingLine(const CPoint& ptStart, const CPoint& ptEnd, 
	const COLORREF& crColor, const int nPenStyle,const int&penwindth)
{
	isSelected = false;
	m_ElementType = LINE;
	m_StartPoint = ptStart;      // 设置直线的起始点
	m_EndPoint = ptEnd;          // 设置直线的终点
	m_crColor = RGB(255,0,0);         // 设置线条颜色
	m_nPenWidth = penwindth;             // 设置笔宽
	m_nPenStyle = nPenStyle;     // 设置笔的样式

	// 声明封装的矩形
	m_EnclosingRect = CRect(ptStart, ptEnd);
	m_EnclosingRect.NormalizeRect();//正规化处理

	vector<AsixPOINT>().swap(g_point);
	AsixPOINT point;
	point .x1 = m_StartPoint.x;
	point.y1 = m_StartPoint.y;

	point.x2 = m_EndPoint.x;
	point.y2 = m_EndPoint.y;

	g_point.push_back(point);

}

void CDrawingLine::Draw(CDC *pDC, const CDrawingElement *pElement) const
{
    
	    CPen aPen(PS_SOLID,1,m_crColor);
	    CPen *oldPen=pDC->SelectObject(&aPen);
	
	// 画直线
	pDC->MoveTo(m_StartPoint);   //移动第一个点的位置
	pDC->LineTo(m_EndPoint);    //画到最后一个点

	CString str;


	/*str.Format("x1 = %d y1 = %d x2 = %d y2 = %d",m_StartPoint.x, m_StartPoint.y,m_EndPoint.x,m_EndPoint.y);
	AfxMessageBox(str);
*/
}


void CDrawingLine::Serialize(CArchive &ar)
{
	CDrawingElement::Serialize(ar);        // 调用基类的序列化函数

	if (ar.IsStoring())
	{
		ar << m_StartPoint           // 储存直线的第一个点
			<< m_EndPoint;            // 储存直线的终点
	}
	else
	{
		ar >> m_StartPoint           // 打开直线的起始点
			>> m_EndPoint;            // 打开直线的终点
	}
}

Ployline 绘制

// 派生类CDrawingCurve的构造函数
CDrawingCurve::CDrawingCurve(const CPoint &FirstPoint, 
	const CPoint &SecondPoint, 
	const COLORREF &Color,
	const int nPenStyle,const int& penwidth)
{
	isSelected = false;
	m_ElementType = CURVE;
	m_PointList.AddTail(FirstPoint);    // 把第一个点添加到表中
	m_PointList.AddTail(SecondPoint);   // 把第二个点添加到表中
	m_crColor = RGB(255,0,0);                  // 添加颜色
	m_nPenWidth = penwidth;                    // 设置笔宽
	m_nPenStyle = nPenStyle;            // 设置笔的样式

	//
	m_EnclosingRect = CRect(FirstPoint, SecondPoint);
	m_EnclosingRect.NormalizeRect();//正规化处理

}

void CDrawingCurve::AddSegment(const CPoint& pt)
{
	m_PointList.AddTail(pt);                // 把点添加到末尾

	// 给新的点修改封装的矩形
	m_EnclosingRect = CRect( min(pt.x, m_EnclosingRect.left),
		                     min(pt.y, m_EnclosingRect.top),
		                     max(pt.x, m_EnclosingRect.right),
		                     max(pt.y, m_EnclosingRect.bottom) );
}

void CDrawingCurve::Draw(CDC* pDC, const CDrawingElement* pElement /* = 0 */) const
{
	// 创建一只笔并设置颜色以及样式
	CDrawingElement *ele = NULL;
	vector<AsixPOINT>().swap(g_Curve);
	CPen aPen(PS_SOLID,1,m_crColor);
	
	CPen *oldPen=pDC->SelectObject(&aPen);
	POSITION aPosition = m_PointList.GetHeadPosition();
	AsixPOINT point;
	if(aPosition)
	{

		pDC->MoveTo(m_PointList.GetNext(aPosition));//指向下一个点
	
		
	}
	// 循环获得之后的点
	while(aPosition)
	{

		pDC->LineTo(m_PointList.GetNext(aPosition));

	}

}

椭圆绘制

CDrawingEllipse::CDrawingEllipse(const CPoint &ptStart, const CPoint &ptEnd, 
	const COLORREF &crColor, const int nPenStyle, 
	const WORD wElement,const int&penwidth) : m_wElement(wElement)
{
	
	isSelected = false;
	m_ElementType = ELLIPSE;
	m_crColor = RGB(255,0,0);          
	m_nPenWidth = penwidth;              
	m_nPenStyle = nPenStyle;  
	m_EnclosingRect = (CRect(ptStart,ptEnd));
	m_EnclosingRect.NormalizeRect();//正规化处理

	vector<AsixPOINT>().swap(g_point);
	AsixPOINT point;
	point .x1 = ptStart.x;
	point.y1 = ptStart.y;

	point.x2 = ptEnd.x;
	point.y2 = ptEnd.y;

	g_point.push_back(point);
	/*CString str;
	str.Format("x1 = %d y1 = %d x2 = %d  y2 = %d ",ptStart.x, ptStart.y,ptEnd.x,ptEnd.y);
	AfxMessageBox(str);*/
}

void CDrawingEllipse::Draw(CDC *pDC, const CDrawingElement *pElement/* = 0*/) const
{
	CPen aPen(PS_SOLID,1,m_crColor);
	CPen *oldPen=pDC->SelectObject(&aPen);
	//CBrush *pBrush=new CBrush(RGB(0, 255, 128));
 //   pDC->SelectObject(pBrush); 
	pDC->Ellipse(m_EnclosingRect);
   // 选择一个空的笔刷
	
    CBrush* pOldBrush = static_cast<CBrush*>(pDC->SelectStockObject(NULL_BRUSH)); 
	pDC->SelectObject(pOldBrush);              
}

void CDrawingEllipse::Serialize(CArchive &ar)
{
	CDrawingElement::Serialize(ar);       
	m_PointList.Serialize(ar);             
}

矩形绘制

CDrawingRectangle::CDrawingRectangle(const CPoint &ptStart, const CPoint &ptEnd, 
	const COLORREF &crColor, const int nPenStyle,const int& penwidth)
{
	isSelected =false;
	m_ElementType = RECTANGLE;
	m_crColor = RGB(255,0,0);           
	m_nPenWidth = penwidth;             
	m_nPenStyle = nPenStyle;      
	m_EnclosingRect = CRect(ptStart, ptEnd);
	m_EnclosingRect.NormalizeRect();//正规化处理

	vector<AsixPOINT>().swap(g_point);
	AsixPOINT point;
	point .x1 = ptStart.x;
	point.y1 = ptStart.y;

	point.x2 = ptEnd.x;
	point.y2 = ptEnd.y;

	g_point.push_back(point);

}

void CDrawingRectangle::Draw(CDC *pDC, const CDrawingElement *pElement) const
{

	CPen aPen(PS_SOLID,1,m_crColor);
	CPen *oldPen=pDC->SelectObject(&aPen);
	
	// Select the brush
	CBrush* pOldBrush = static_cast<CBrush*>(pDC->SelectStockObject(NULL_BRUSH)); 

	// Now draw the rectangle
	pDC->Rectangle(m_EnclosingRect);
	pDC->SelectObject(pOldBrush);    

	//CString str;
	//str.Format("%d %")
             
}


void CDrawingRectangle::Serialize(CArchive &ar)
{
	CDrawingElement::Serialize(ar);   
}

擦除绘制图形

CErasure::CErasure(const CPoint &ptStart, const CPoint &ptEnd, 
	const COLORREF &crColor, const int nPenStyle,const int& penwidth)
{
	isSelected = false;
	m_crColor = RGB(255,255,255);           
	m_nPenWidth = penwidth;             
	m_nPenStyle = nPenStyle;      
	m_EnclosingRect = CRect(ptStart, ptEnd);
	m_EnclosingRect.NormalizeRect();//正规化处理



}

void CErasure::Draw(CDC *pDC,const CDrawingElement *pElement) const
{

	CPen aPen(PS_SOLID,1,RGB(255,255,255));
	CPen *oldPen=pDC->SelectObject(&aPen);

	CBrush *pBrush=new CBrush(RGB(255, 255, 255));
    pDC->SelectObject(pBrush); 


	pDC->Rectangle(m_EnclosingRect);
	CBrush* pOldBrush = static_cast<CBrush*>(pDC->SelectStockObject(NULL_BRUSH)); 
	pDC->SelectObject(pOldBrush);

}




void CErasure::Serialize(CArchive &ar)
{
	CDrawingElement::Serialize(ar);   
}

线段长度,面积计算


//求椭圆的面积
double CMFCDrawAppView::CalEllipseArea(int x1,int y1,int x2,int y2)
{
	double laxis = (x2 - x1)/2;
	double saxis = (y2 - y1)/2;

	double area = 3.14 * laxis * saxis;

	return area;
}

//求线段的长度
double CMFCDrawAppView::LineOfLength(int x1,int y1,int x2,int y2)
{
	int x = x2- x1;
	int y = y2 - y1;
	double  length = (x *x) + (y *y);
	double result = sqrt(length);
	return result;
	
}
double CMFCDrawAppView::RactangeArea(int x1,int y1,int x2,int y2)
{
	int width = x2 - x1;
	int height = y2 - y1;
	double area = width * height;
	return area;
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值