使用鼠标C ++移动线的问题 [英] problem in moving line using mouse C++
问题描述
嗨
我的代码允许用户绘制线条,然后他可以对其进行任何修改,例如:更改线条属性,删除线条,更改其长度并移动它
我已经编写了大多数代码,但是我不能为它的活动部分写这些行,但是当我运行代码时,这些行就消失了:
与移动形状有关的部分
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);
}
我认为它们根据地图模式消失了.我使用以下等式无法正常工作:
OnLButtonDown:
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
推荐答案
相对的>>它保存了直线的起点
相对数>>它保存了直线的终点
视图类中定义的所有变量
我在on draw函数中进行了绘制
我只在鼠标事件上设置点:
这里的ondraw函数
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);
}
}
如果用户选择的行,在这里我将sel值设置为true
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);
}
//这里设置relativest和relativend值,并拖动到true
//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);
}
在这里将拖动更改为false即可停止移动形状
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
请帮助我,我不知道如何更新拖动线的值以使其正常运行
please help me I do not know how to update the value of the dragged line to get it running correctly
这篇关于使用鼠标C ++移动线的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!