Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..

C++
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.
Posted
Updated 9-Dec-12 20:44pm
v3

1 solution

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

C++
 //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;
};
 
Share this answer
 
v2
Comments
J.Surjith Kumar 10-Dec-12 3:11am    
will u give me some practical example.
Mohibur Rashid 10-Dec-12 3:15am    
Of what?
Mohibur Rashid 10-Dec-12 21:38pm    
Please recheck the answer for example
J.Surjith Kumar 11-Dec-12 0:28am    
It shows an error as undeclared it,iteration,list and vector.
Mohibur Rashid 11-Dec-12 0:44am    
include vector header file
e.g.
#include <vector>

read about vector too

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900