Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ line
Hi
 
my code allow user draw lines then he can do any modifications on it such as: change line properties ,delete line ,change its length and move it
 
I did code most of them but I could not do the moving part I wrote these lines for it but when I run the code the lines get disappear:
 
the part related to move shapes
 
 
 
vector<Line*> mylines; // vector of all line objects

#include "stdafx.h"
#include "Lines.h"
#include <windows.h>
#include "patron1View.h"

// the class for creating line objects and draw it
Line::Line()
   {
color=RGB(0,0,0);
   }
 
void Line::setPoints(POINT s,POINT e){
	  start=s;
	  end=e;
	
	}//setpoints

POINT Line::getspoints(){
return start;
}
 
POINT Line::getepoints(){
return end;
}
 
void Line::setColor(int c){
	switch(c){
	case 1:
		color=RGB(255,0,0);//red
		break;
	case 2:
		color=RGB(0,0,255);//blue
		break;
	case 3:
		color=RGB(20,255,0);//green
		break;
	default: color=RGB(0,0,0);//black
		break;
	}//switch
}
 
void Line::setStyle(int c){
	                                    
	 style=c; 
 
}
 

 
void Line::drawline(CDC* pDC){
	
	pDC->SetMapMode(MM_LOMETRIC);
	if(style==1){
      mypen.CreatePen(PS_DOT,1,color);
	}else if(style==2){
      mypen.CreatePen(PS_DASH,1,color);
	}else if(style==3){
      mypen.CreatePen(PS_DASHDOT,1,color);
	}else {
	 mypen.CreatePen(PS_SOLID,1,color);
	}
	
      pDC->SelectObject(&mypen);
	  pDC->MoveTo ( start.x, start.y) ;
      pDC->LineTo (end.x,end.y ) ;
}
 

 

//the below code written within view.cpp class
/////////////////////////

CScrollView::OnLButtonDown(nFlags, point){
 

 CClientDC dc(this);
	  dc.SetMapMode(MM_LOMETRIC);
	  OnPrepareDC(&dc); // set up mapping mode and viewport origin
       dc.DPtoLP(&point);
     
 

if(sel){
relativest.x=mylines.at(selind)->getspoints().x-point.x;
relativest.y=mylines.at(selind)->getspoints().y-point.y;
relativend.x=mylines.at(selind)->getepoints().x-point.x;
relativend.y=mylines.at(selind)->getepoints().y-point.y;
   }
 

	CScrollView::OnLButtonDown(nFlags, point);
}
 

void Cpatron1View::OnMouseMove(UINT nFlags, CPoint point)
{
	CClientDC dc(this);
    dc.SetMapMode(MM_LOMETRIC);
     OnPrepareDC(&dc); // set up mapping mode and viewport origin
     dc.DPtoLP(&point);
	Cpatron1Doc* doc = GetDocument(); 
 
//do move shape
	if(((nFlags& MK_LBUTTON)== MK_LBUTTON)&&sel){
	relativest.x= point.x + relativest.x;
	relativest.y= point.x + relativest.y;
	relativend.x= point.x + relativend.x;
	relativend.y= point.x +relativend.y;
     }
 
CScrollView::OnMouseMove(nFlags, point);
}
 

void Cpatron1View::OnLButtonUp(UINT nFlags, CPoint point)
{
 
      
      CClientDC dc(this);
     dc.SetMapMode(MM_LOMETRIC);
     OnPrepareDC(&dc); // set up mapping mode and viewport origin
     dc.DPtoLP(&point);
          
 
if(sel){
	relativest.x= point.x + relativest.x;
	relativest.y= point.x + relativest.y;
	relativend.x= point.x + relativend.x;
	relativend.y= point.x +relativend.y;
	mylines.at(selind)->setPoints(relativest,relativend);
     }
 
CScrollView::OnLButtonUp(nFlags, point);
}
 
 
I think they disappear according to the map mode I use the below equation did no work correctly:
 
OnLButtonDown:
relativest.x=mylines.at(selind)->getspoints().x-point.x;
relativest.y=mylines.at(selind)->getspoints().y-point.y;
relativend.x=mylines.at(selind)->getepoints().x-point.x;
relativend.y=mylines.at(selind)->getepoints().y-point.y;
on mouse move:
relativest.x= point.x + relativest.x;
relativest.y= point.x + relativest.y;
relativend.x= point.x + relativend.x;
relativend.y= point.x +relativend.y;
mylines.at(selind)->setPoints(relativest,relativend);
 
could you please help me in doing this
 
thanks
Posted 12-Nov-12 8:17am
Edited 12-Nov-12 8:25am
Nelek47.5K
v2
Comments
Andrew Cherednik at 12-Nov-12 16:47pm
   
1. Can you clarify what is relativest and relativend, and where they are defined.
2. I do not believe it is a good idea to call drawing functions inside Mouse events. I think yon need to call Invalidate() within these events and do all the drawing within OnPaint()
3. Can you show where you defined and initialised sel and selind variables
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

relativest >> it save the start point for the line
relativend >> it save the end point for the line
all variables defined in the view class
 
I did the draw inside the on draw function
I only set the points on the mouse events:
here the ondraw function
 
void Cpatron1View::OnDraw(CDC* pDC)
{
	pDC->SetMapMode ( MM_LOMETRIC) ;
	Cpatron1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
 
**********************************************
 
//draw lines
for(int i=0;i<(int)mylines.size();i++){
mylines.at(i)->drawline(pDC);
}
 
}
 
here I set the sel value to true if the line selected by user
 
 
void Cpatron1View::OnLButtonDown(UINT nFlags, CPoint point)
{   Cpatron1Doc* doc = GetDocument(); 
	
      CClientDC dc(this);
	  dc.SetMapMode(MM_LOMETRIC);
	  OnPrepareDC(&dc); // set up mapping mode and viewport origin
       dc.DPtoLP(&point);
     
for(int i=0;i<(int)mylines.size();i++){
		
		sel=HitTestLine(mylines.at(i)->getspoints(),      mylines.at(i)->getepoints(), point, 6);
		selind=i;
		if(sel== true){
		mylines.at(selind)->setColor(1);
		selcurve=false;
		Invalidate();
		break;
		}
	}
	
}//for

 
	
	CScrollView::OnLButtonDown(nFlags, point);
}
 
 
//here set the relativest and relativend value and dragged to true
void Cpatron1View::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	CClientDC dc(this);
     dc.SetMapMode(MM_LOMETRIC);
     OnPrepareDC(&dc); // set up mapping mode and viewport origin
     dc.DPtoLP(&point);
	
         
//prepare to move the shapes
	if(sel){
		relativest.x=mylines.at(selind)->getspoints().x-point.x;
		relativest.y=mylines.at(selind)->getspoints().y-point.y;
		relativend.x=mylines.at(selind)->getepoints().x-point.x;
		relativend.y=mylines.at(selind)->getepoints().y-point.y;
		dragged=true;
   }
   
 
	 
	CScrollView::OnLButtonDblClk(nFlags, point);
}
 
 

// here set the new points for the shape as the mouse moved
 
 
void Cpatron1View::OnMouseMove(UINT nFlags, CPoint point)
{
	CClientDC dc(this);
    dc.SetMapMode(MM_LOMETRIC);
     OnPrepareDC(&dc); // set up mapping mode and viewport origin
     dc.DPtoLP(&point);
	
	   
	
 
	
	//do move shape
	if(((nFlags& MK_LBUTTON)== MK_LBUTTON) && dragged){
	
	relativest.x += point.x ;
	relativest.y += point.y ;
	relativend.x += point.x ;
	relativend.y += point.y;
	mylines.at(selind)->setPoints(relativest,relativend);
	Invalidate();
}
 
	
	CScrollView::OnMouseMove(nFlags, point);
}
 
 
here change the dragged to false to stop moving the shape
 
void Cpatron1View::OnLButtonUp(UINT nFlags, CPoint point)
{
      
      CClientDC dc(this);
     dc.SetMapMode(MM_LOMETRIC);
     OnPrepareDC(&dc); // set up mapping mode and viewport origin
     dc.DPtoLP(&point);
          
      
	
	
	if(dragged){
		 
		dragged=false;
     }
 
	CScrollView::OnLButtonUp(nFlags, point);
}
 
please help me in find what wrong with moving part
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

please help me I do not know how to update the value of the dragged line to get it running correctly
  Permalink  

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

  Print Answers RSS
0 Maciej Los 655
1 OriginalGriff 453
2 CPallini 239
3 CHill60 180
4 Peter Leow 175
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,898
2 Maciej Los 3,159
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 16 Nov 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