Demo entry 6660880

图形绘制

   

Submitted by 高鑫 on Nov 17, 2017 at 03:25
Language: C++. Code size: 2.2 kB.

void CMyDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
	//在该消息的响应函数中根据选中的绘图类型,调用CDC类绘制图形
	if(m_bDrawShape)
	{
		//开始绘图
		CClientDC dc(this);	//从堆栈中构造一个DC对象
		CPen *pOldPen, pen;
		pen.CreatePen(PS_SOLID,1,m_crDrawColor);//创建画笔
		pOldPen = (CPen*)dc.SelectObject(&pen);//将画笔选入设备环境
		CBrush *pOldBrush,brush;
		brush.CreateSolidBrush(m_crFillColor);
		pOldBrush = (CBrush*)dc.SelectObject(&brush);
		dc.SetROP2(R2_NOTXORPEN);
		switch(m_nDrawShape)
		{
		case SHAPE_PEN:
			//画笔
			dc.MoveTo(m_ptOrigin);
			dc.LineTo(point);
			m_ptOrigin = point;
			break;
		case SHAPE_LINE:
			//直线
			dc.MoveTo(m_ptOrigin);
			dc.LineTo(m_ptPrv);		//擦除上次绘制直线
			dc.MoveTo(m_ptOrigin);
			dc.LineTo(point);
			m_ptPrv = point;
			break;
		case SHAPE_RECT:
			//矩形
			dc.MoveTo(m_ptOrigin);
			dc.Rectangle(m_ptOrigin.x,m_ptOrigin.y,m_ptPrv.x,m_ptPrv.y);
			dc.MoveTo(m_ptOrigin);
			dc.Rectangle(m_ptOrigin.x,m_ptOrigin.y,point.x,point.y);
			m_ptPrv = point;
			break;
		case SHAPE_ELLIPSE:
			//椭圆
			dc.MoveTo(m_ptOrigin);
			dc.Ellipse(m_ptOrigin.x,m_ptOrigin.y,m_ptPrv.x,m_ptPrv.y);
			dc.MoveTo(m_ptOrigin);
			dc.Ellipse(m_ptOrigin.x,m_ptOrigin.y,point.x,point.y);
			m_ptPrv = point;
			break;
		case SHAPE_CIRCLE:
			//圆 初始点为圆心,到目标点的直线距离为半径
			int r = sqrt (pow(double((m_ptPrv.x - m_ptOrigin.x)),2) + pow(double((m_ptPrv.y - m_ptOrigin.y)),2));
			dc.MoveTo(m_ptOrigin);
			dc.Ellipse(m_ptOrigin.x - r,m_ptOrigin.y - r,m_ptOrigin.x + r,m_ptOrigin.y + r);
			r = sqrt (pow(double((point.x - m_ptOrigin.x)),2) + pow(double((point.y - m_ptOrigin.y)),2));
			dc.MoveTo(m_ptOrigin);
			dc.Ellipse(m_ptOrigin.x - r,m_ptOrigin.y - r,m_ptOrigin.x + r,m_ptOrigin.y + r);
			m_ptPrv = point;
			break;
		case SHAPE_ERASER://橡皮擦
			CBrush brush(RGB(255, 255, 255));
			CClientDC dc(this);
			CRect rect;
			rect.top = point.y - 8;
			rect.left = point.x - 8;
			rect.bottom = rect.top + 8;
			rect.right = rect.left + 8;
			dc.FillRect(rect, &brush);
			break;
		//其余图形的case语句在下面添加
		}

		dc.SelectObject(pOldPen);
		dc.SelectObject(pOldBrush);
		brush.DeleteObject();
		pen.DeleteObject();


	}
	CView::OnMouseMove(nFlags, point);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).