Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VC++
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.
Posted 9-Dec-12 20:20pm
Edited 9-Dec-12 20:44pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
};
 

  Permalink  
v2
Comments
J.Surjith Kumar at 10-Dec-12 3:11am
   
will u give me some practical example.
Mohibur Rashid at 10-Dec-12 3:15am
   
Of what?
Mohibur Rashid at 10-Dec-12 21:38pm
   
Please recheck the answer for example
J.Surjith Kumar at 11-Dec-12 0:28am
   
It shows an error as undeclared it,iteration,list and vector.
Mohibur Rashid at 11-Dec-12 0:44am
   
include vector header file
e.g.
#include <vector>
 
read about vector too
J.Surjith Kumar at 11-Dec-12 1:55am
   
ok finally it shows only list as undeclared
J.Surjith Kumar at 11-Dec-12 1:57am
   
ok now it shows only list is undeclared.
J.Surjith Kumar at 11-Dec-12 2:08am
   
atlast it shows the only error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
Mohibur Rashid at 11-Dec-12 4:44am
   
#include <vector>
J.Surjith Kumar at 11-Dec-12 4:48am
   
i included this header
J.Surjith Kumar at 10-Dec-12 3:20am
   
I used Invalidate() in the OnLButtonUp() function which invalidates the entire client area for every time it has been called. So that i cant draw multiple lines for that i need a solution.
Richard MacCutchan at 10-Dec-12 4:52am
   
You need to hold a list of all the lines (or points) in your drawing, and then redraw all the lines from those points in your OnDraw() function. Also note that you cannot use the same array elements to hold different points. I was sure I had explained this already, but perhaps someone else is working on the exact same problem.
Richard MacCutchan at 10-Dec-12 4:58am
   
Is this question anything to do with you?
J.Surjith Kumar at 10-Dec-12 5:16am
   
Have u given any example code anywhere regarding this issues.
J.Surjith Kumar at 10-Dec-12 5:29am
   
If i give Invalidate(FALSE) in the OnLButtonUp function. Multiple lines can be drawn but when i resize the window the lines which is drawn last is only shown.
JackDingler at 10-Dec-12 12:53pm
   
You keep making the same comments over and over, and the answers will be the same.
 
You need to keep a list of the lines you wish to draw. There is no magic going on in the background that reads your mind to figure out what to draw and what not to draw.
 
When you call an Invalidate(FALSE), you're simply triggering a paint w/o a background erase. When you resize, a background erase occurs automatically.
 
In your OnDraw / OnPaint, you need to walk through your list of lines and draw each one. Your mouse functions should simply update your list of lines.

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

  Print Answers RSS
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100