如何使用OnDraw函数动态绘制多行。 [英] How to draw multiple lines dynamically using OnDraw function.

查看:177
本文介绍了如何使用OnDraw函数动态绘制多行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建SDI应用程序以在用户按下并释放鼠标按钮时绘制线条。我在OnDraw函数中给出了moveto和lineto,即使在调整窗口大小时也会显示行。我在OnLButtonUp函数中使用了Invalidate(),它使整个客户区无效。要绘制多条线,它不起作用。这是我的代码..



Im Creating SDI application to draw the lines when the user press and release the mouse button. I given moveto and lineto within the OnDraw function to make the lines present even when the window is resized. I used Invalidate() in the OnLButtonUp function it invalidates the entire client area. To draw multiple lines it doesn''t work. Here is my code..

void CserializationTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	l[0]=point.x;
	l[1]=point.y;
	CView::OnLButtonDown(nFlags, point);
}

void CserializationTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	PtLine=CPoint(point.x,point.y);
	Invalidate();
	CView::OnLButtonUp(nFlags, point);
}

void CserializationTestView::OnDraw(CDC* pDC)
{
	CserializationTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDC->MoveTo(l[0],l[1]);
	pDC->LineTo(PtLine);
	
	// TODO: add draw code for native data here
}



要绘制多条线,我必须做什么。


To draw multiple lines what i have to do.

推荐答案

你必须记住所有点数,然后每次提取被调用它将使用存储的值绘制线。



您可以使用向量来存储所有点击的点。如果你只是用vector来存储你的数据,那将是最好的做法



You will have to remember all the points and then every time ondraw is being called it will use the stored values to draw lines.

You can use vector to store all your clicked points. It would be best practice if you just use vector to store your data

 //a simple working example
void CserializationTestView::OnDraw(CDC* pDC)
{
	CappDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	// TODO: add draw code for native data here
	for(std::vector<CCoords>::iterator it=list.begin();it!=list.end();++it)
	{
		pDC->MoveTo(it->start);
		pDC->LineTo(it->End);
	}
	
}
void CserializationTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
	list[list.size()-1].End=point;
	Invalidate();
}
void CserializationTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
	
	CCoords nlist;
	nlist.start=point;
	list.push_back(nlist);
}
//definition of CCoords
class CCoords
{
public:
	CPoint start;
	CPoint End;
};


这篇关于如何使用OnDraw函数动态绘制多行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆