Click here to Skip to main content
12,690,002 members (24,699 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ VisualC++
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
	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);
	CView::OnLButtonUp(nFlags, point);

void CserializationTestView::OnDraw(CDC* pDC)
	CserializationTestDoc* pDoc = GetDocument();
	// TODO: add draw code for native data here

To draw multiple lines what i have to do.
Posted 9-Dec-12 21:20pm
Updated 9-Dec-12 21:44pm

1 solution

Rate this: bad
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();
	if (!pDoc)
	// TODO: add draw code for native data here
	for(std::vector<CCoords>::iterator it=list.begin();it!=list.end();++it)
void CserializationTestView::OnLButtonUp(UINT nFlags, CPoint point)
void CserializationTestView::OnLButtonDown(UINT nFlags, CPoint point)
	CCoords nlist;
//definition of CCoords
class CCoords
	CPoint start;
	CPoint End;

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
#include <vector>

read about vector too
J.Surjith Kumar 11-Dec-12 1:55am
ok finally it shows only list as undeclared
J.Surjith Kumar 11-Dec-12 1:57am
ok now it shows only list is undeclared.
J.Surjith Kumar 11-Dec-12 2:08am
atlast it shows the only error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
Mohibur Rashid 11-Dec-12 4:44am
#include <vector>
J.Surjith Kumar 11-Dec-12 4:48am
i included this header
J.Surjith Kumar 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 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 10-Dec-12 4:58am
Is this question anything to do with you?
J.Surjith Kumar 10-Dec-12 5:16am
Have u given any example code anywhere regarding this issues.
J.Surjith Kumar 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170113.4 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2017
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