Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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

C++
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:
C++
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:
C++
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
Updated 12-Nov-12 8:25am
v2
Comments
chaau 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

please help me I do not know how to update the value of the dragged line to get it running correctly
 
Share this answer
 
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
C++
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

C++
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
C++
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

C++
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
C++
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
 
Share this answer
 

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