⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 direction2view.cpp

📁 数据结构求最短路径vc++ 实现手动作图
💻 CPP
📖 第 1 页 / 共 3 页
字号:


 	}
 	
///取消图形对象被选中的效果

 else if(pApp->m_CurrentTool == ID_DRAW_LINE)
 {      int i2;
 		int num2 = pDoc->GetDrawnum();
		for( i2=num2;i2>0;i2--)
		{
 			pDraw = pDoc->GetDraw(i2-1);
 			rect = pDraw ->GetDimRect();
	     if(rect.PtInRect(point) && pDraw->Gettype() ==2 )  /// 缺点为当对象重合时会选择多个对象
		 {  lindex1=i2-1;
             selectindex=lindex1;     //所选择对象的索引
			  pDraw -> Setselete(true);
			  break;
		  }
		  	else
          pDraw ->Setselete(false);
		}
	  if(i2>1)
      {
	  for(int j=i2-2;j>=0;j--)
	  {pDraw = pDoc->GetDraw(j);
       pDraw -> Setselete(false); 
	  }
	  }
  if(i2==0)
  selectindex=-1;

}
else if(pApp->m_CurrentTool == ID_DRAW_TEXT)	
{

//显示字体的属性对话框,保存好坐标,以便在该坐标处放文字

//point 已经过转化为逻辑坐标了

m_textset->SetText("请输入文字", m_cTextColor,m_text,point);
m_textset->ShowWindow(SW_SHOW);

}	
 	
     m_PointOrigin = point;
 	m_PointOld = point;
     SetCapture();
 	m_Dragging = 1;
 	
 	
	
	ClientDC.LPtoDP(&ScrollRect);         /// 逻辑坐标转换为设备坐标
	CRect ViewRect;
	GetClientRect(&ViewRect);              //得到当前的设备坐标
 	CRect IntRect;
 	IntRect.IntersectRect(&ScrollRect,&ViewRect);
 	ClientToScreen(&IntRect);                //设备坐标转换为屏幕坐标          
 	
 	
 	
 	::ClipCursor(&IntRect);                    //限定鼠标的移动范围
 	
 	
 	
 		
	CScrollView::OnLButtonDown(nFlags, point);
}

void CDirection2View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default


		
		if(!m_Dragging) return;    //没按下鼠标则退出
		m_Dragging = 0;	
		
		::ReleaseCapture();        //关闭鼠标捕获
		
		::ClipCursor(NULL);         //取消对鼠标的移动限制
 		
		CClientDC ClientDC(this);
		
		OnPrepareDC(&ClientDC);
		
		ClientDC.DPtoLP(&point);     //设备坐标转换为逻辑坐标
		
		ClientDC.SelectObject(&m_PenDotted);
		
		ClientDC.SetROP2(R2_NOT);
 		
 		ClientDC.SetBkMode(TRANSPARENT);
		
		ClientDC.SelectStockObject(NULL_BRUSH);
		CDirection2Doc* pDoc = GetDocument();
		CDirection2App *pApp = (CDirection2App *) AfxGetApp();
		
		CString str1="name";
		CString str2="name";
		
		CBasepic *pDraw;
         CLine   *pLine ;
    	CRectFill *Rectf1;
		CRectFill *Rectf2;
		BOOL flag = FALSE;
        CPoint  pointmid;
        ///Invalidate(CRect) 更新的区域
		int i;
		int num;
	//	int lnum;
 		switch(pApp -> m_CurrentTool)
		{
		case ID_DRAW_LINE: //直线
			
		    num = pDoc->GetDrawnum();
			for(i=0;i<num;i++)
			{
				pDraw = pDoc->GetDraw(i);
			    CRect rect = pDraw ->GetDimRect();
				if(rect.PtInRect(point) && pDraw->Gettype() == 2)   //为矩形
				{ 
					lindex2=i;
				//selectindex=lindex2;	
				}

			}
			
			if(lindex2 != -1 && lindex1 !=-1 && lindex1 !=lindex2 )  //不为同一个矩形,则做直线
 			{


			
				CPoint point1,point2;
                BOOL in=false;
				Rectf1=(CRectFill *)(pDoc ->GetDraw(lindex1));
				Rectf2=(CRectFill *)(pDoc ->GetDraw(lindex2));

	//判断此两个矩形之间是否有直线存在,如果存在侧不要做直线
                int linenum1= Rectf1->Getlinenum();
                int linenum2= Rectf2->Getlinenum();//得到两个矩形的相关的直线数
            for(int k=0;k<linenum1;k++)
			{
				for(int j=0;j<linenum2;j++)
				{
					if(Rectf1->Getline(k)== Rectf2->Getline(j))
					{  in=true;
					}
				}
			
			}
		   ///在矩形中增加相关的直线
          if(!in)
          {     
			  Rectf1->AddLine(pDoc ->GetDrawnum());
              Rectf2->AddLine(pDoc ->GetDrawnum());



				point1=Rectf1->Getmid();
				point2=Rectf2->Getmid();
				
				ClientDC.MoveTo(point1);
				ClientDC.LineTo(point2);
				
				//函数的格式CLine(int x1,int y1,CString name,
 				//LOGFONT  text,COLORREF color,int x2,int y2,int index1,int index2,int width)
			    pDraw = new CLine(point1.x,point1.y,str1,m_text,m_cTextColor,point2.x,point2.y,lindex1,lindex2,m_CurrentPenThickness,m_PenColor,m_CurrentLineStyle);
			
            ClientDC.SetROP2(R2_COPYPEN);
 			pDraw->Draw(&ClientDC);
 			pDoc -> AddDraw(pDraw);
			pDoc->UpdateAllViews(this,0,pDraw);
		
		  }
 			}

				lindex1=-1;
				lindex2=-1;	
			break;
case ID_DRAW_SELECT:  //选择对象
			
			///移动对象。
 			if(index!=-1 )
			{
 				CBasepic * pDraw2=pDoc->GetDraw(index);
 			if(pDraw2->Gettype()!=1)
			{	pDraw2->Setpoint(point.x-m_PointOrigin.x,point.y-m_PointOrigin.y);
 		//		pDraw2->Draw(&ClientDC);

				if(pDraw2->Gettype()==2)  //当为矩形时要移动直线到相应位置
				{
                  Rectf1=(CRectFill *)pDoc->GetDraw(index);
				//pointmid= Rectf1->Getmid();  //得到中点
              
				
				 for(num = Rectf1->Getlinenum();num>0;num-- ) //得到直线的索引
				 {   
					 pLine = (CLine *)pDoc->GetDraw(Rectf1->Getline(num-1));
				//判断直线移动的是哪一个端点
                  pLine->Setpoint(point.x-m_PointOrigin.x,point.y-m_PointOrigin.y,index);
				 }
				}
			}
			}
			index=-1;
 			break;
 			
case DI_DRAW_RECT:  //作矩形对象
			
			
			ClientDC.Rectangle(m_PointOrigin.x-20,m_PointOrigin.y-20,
				m_PointOrigin.x+20,m_PointOrigin.y+20);///     recrwidth=10;     rectchang=10;

			(CBasepic *)pDraw  = new CRectFill(m_PointOrigin.x,m_PointOrigin.y,str2,m_text,m_cTextColor,recrwidth,rectchang,m_PenColor,m_FillColor);
			
            ClientDC.SetROP2(R2_COPYPEN);
 			pDraw->Draw(&ClientDC);
 			pDoc -> AddDraw(pDraw);
			pDoc->UpdateAllViews(this,0,pDraw);
			break;
case ID_DRAW_TEXT:  //作矩形对象			
CPoint PointOrigin = m_PointOrigin;
ClientDC.DPtoLP(&PointOrigin);
if(m_IsRect)
{
	CRect rect(PointOrigin.x,PointOrigin.y,m_PointOld.x,m_PointOld.y);
	rect.NormalizeRect();
	ClientDC.DrawFocusRect(rect);
	m_IsRect= false;

}
		

}


 //用于擦除留下的图形
ClientDC.SetROP2(R2_COPYPEN);
this->Invalidate();		
OnDraw(&ClientDC);			


	CScrollView::OnLButtonUp(nFlags, point);
}

void CDirection2View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

 	CClientDC ClientDC(this);
 	OnPrepareDC(&ClientDC);
 	
    CDirection2App *pApp =(CDirection2App *)AfxGetApp();
  if( pApp->m_CurrentTool !=ID_DRAW_TEXT)	
	ClientDC.DPtoLP(&point);  
 
 	
 	
 	if(!m_Dragging)           //鼠标未按下时,或者是在作直线的时候|| pApp->m_CurrentTool == ID_DRAW_LINE
 	{
 		
 		CSize ScrollSize = GetTotalSize();
 		CRect ScrollRect(0,0,ScrollSize.cx,ScrollSize.cy);
 		
 		///鼠标若在绘图区域则改变区域内鼠标的形状
 		if(ScrollRect.PtInRect(point) && pApp->m_CurrentTool != ID_DRAW_SELECT)
 			::SetCursor(m_hCross);
 		else if(pApp->m_CurrentTool == ID_DRAW_SELECT)
 			::SetCursor(m_hArrow);
 		
 		return;
 	}

  //  if(pApp->m_CurrentTool != ID_DRAW_SELECT)
                //设备坐标转换为逻辑坐标	
 	
	
	ClientDC.SetROP2(R2_NOT);                 ///设计当前绘图方式为颜色
 	ClientDC.SelectObject(&m_PenDotted);       ///选入点状画笔
 	ClientDC.SetBkColor(TRANSPARENT);          //设计背景透明
 	ClientDC.SelectStockObject(NULL_BRUSH);     //选入空画刷
 	CDirection2Doc* pDoc = GetDocument();


 	CPoint point1;
 	CRectFill *CRect1;
	CPoint point2;
     CText  *CText1;
 	//int *a;
switch (pApp->m_CurrentTool)
{
case ID_DRAW_SELECT:  //为移动矩形和文字对象(不移动直线对象)
	if(index != -1)  //移动的对象
	{
 if((pDoc->GetDraw(index))->Gettype() == 2) //判断移动对象是否为矩形还是文字
{
 			CRect1 = (CRectFill *)pDoc->GetDraw(index);// 返回对象

 
 			
 		//	CRect1 ->Setpoint(m_PointOld.x-m_PointOrigin.x,m_PointOld.y-m_PointOrigin.y);
 			
		//	CRect1->Draw(&ClientDC);
 //移动矩形时 直线也移动。
            point1=CRect1->Getmid();
            point2=CRect1->GetHW();

ClientDC.Rectangle(point1.x+point2.x+m_PointOld.x-m_PointOrigin.x,
				   point1.y+point2.y+m_PointOld.y-m_PointOrigin.y,
				   point1.x-point2.x+m_PointOld.x-m_PointOrigin.x,
				   point1.y-point2.y+m_PointOld.y-m_PointOrigin.y);

ClientDC.Rectangle(point1.x+point2.x+point.x-m_PointOrigin.x,
				   point1.y+point2.y+point.y-m_PointOrigin.y,
				   point1.x-point2.x+point.x-m_PointOrigin.x,
				   point1.y-point2.y+point.y-m_PointOrigin.y);
}
else if((pDoc->GetDraw(index))->Gettype() == 3) ///移动文字
{
	        CText1 = (CText *)pDoc->GetDraw(index);
            point1=CText1->Getmid();
            point2=CText1->GettextHW();

ClientDC.Rectangle(point1.x-point2.x+m_PointOld.x-m_PointOrigin.x,
				   point1.y-point2.y+m_PointOld.y-m_PointOrigin.y,
				   point1.x+point2.x+m_PointOld.x-m_PointOrigin.x,
				   point1.y+point2.y+m_PointOld.y-m_PointOrigin.y);

ClientDC.Rectangle(point1.x-point2.x+point.x-m_PointOrigin.x,
				   point1.y-point2.y+point.y-m_PointOrigin.y,
				   point1.x+point2.x+point.x-m_PointOrigin.x,
				   point1.y+point2.y+point.y-m_PointOrigin.y);

//            ClientDC.Rectangle(point1.x+m_PointOld.x-m_PointOrigin.x,
//				               point1.y+m_PointOld.y-m_PointOrigin.y,
//                               point2.x+m_PointOld.x-m_PointOrigin.x,
//				               point2.y+m_PointOld.y-m_PointOrigin.y);

//            ClientDC.Rectangle(point1.x+point.x-m_PointOrigin.x,
//				               point1.y+point.y-m_PointOrigin.y,
//                               point2.x+point.x-m_PointOrigin.x,
//				               point2.y+point.y-m_PointOrigin.y);

}
	}
            break;

case ID_DRAW_LINE:   //为作直线

		//1 为直线,2为矩形对象 ,3为文字 
 		if(lindex1 !=-1)  //&& (pDraw->Gettype() ==2 ||pDraw->Gettype() ==3)
 		{

 		CRect1 = (CRectFill *)pDoc->GetDraw(lindex1);
 		point1=CRect1->Getmid();
 		ClientDC.MoveTo(point1);
		ClientDC.LineTo(m_PointOld);
 		ClientDC.MoveTo(point1);
		ClientDC.LineTo(point);
		}
break;
case ID_DRAW_TEXT:
	{
CDC * pDC = GetDC();
CRect rect(m_PointOrigin.x,m_PointOrigin.y,m_PointOld.x,m_PointOld.y);

rect.NormalizeRect();
pDC->DrawFocusRect(rect);
rect.SetRect(m_PointOrigin.x,m_PointOrigin.y,point.x,point.y);
rect.NormalizeRect();
pDC->DrawFocusRect(rect);
m_IsRect = TRUE;
ReleaseDC(pDC);
break;
	
	
	}
}

 		
 
	m_PointOld = point;   
	
	CScrollView::OnMouseMove(nFlags, point);
}

void CDirection2View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
CSize Scrollsize = GetTotalSize();
CRect scrollRect(0,0,Scrollsize.cx,Scrollsize.cy);

	
}

void CDirection2View::OnChushihua() 
{
	// TODO: Add your command handler code here
//字体设计
CFontDialog dlg;
dlg.m_cf.lpLogFont =&m_text;
dlg.m_cf.rgbColors=m_cTextColor;
dlg.m_cf.Flags|=CF_INITTOLOGFONTSTRUCT; //设置标志
if(dlg.DoModal() == IDOK)
{
dlg.GetCurrentFont(&m_text);
m_cTextColor = dlg.GetColor();
}	

}

void CDirection2View::OnColorpen() 
{
	// TODO: Add your command handler code here
CColorDialog Colordialog(m_PenColor,CC_FULLOPEN,this);
if(Colordialog.DoModal()==IDOK)
{
m_PenColor = Colordialog.GetColor();

}	
}

void CDirection2View::OnColorfill() 
{
	// TODO: Add your command handler code here
CColorDialog Colordialog(m_FillColor,CC_FULLOPEN,this);
if(Colordialog.DoModal()==IDOK)
{
m_FillColor = Colordialog.GetColor();

}	
}

void CDirection2View::OnPenstyle() 
{
if(m_huitu != NULL)
{
	if(m_huitu->IsWindowVisible())
		m_huitu->ShowWindow(SW_HIDE);
	else
		m_huitu->ShowWindow(SW_SHOW);
}	
}

void CDirection2View::OnUpdatePenstyle(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
   pCmdUI->SetCheck(m_huitu !=NULL && m_huitu->IsWindowVisible());
}

void CDirection2View::OnRecthw() 
{
	// TODO: Add your command handler code here
if(m_rect != NULL)
{
	if(m_rect->IsWindowVisible())
		m_rect->ShowWindow(SW_HIDE);
	else
		m_rect->ShowWindow(SW_SHOW);
}	
}

void CDirection2View::OnUpdateRecthw(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
   pCmdUI->SetCheck(m_rect !=NULL && m_rect->IsWindowVisible());	
}
 void CDirection2View::Setpenstyle(WPARAM wParam,LPARAM lParam)
 {
 
m_CurrentPenThickness=LOWORD(lParam);   //画笔宽度

m_CurrentLineStyle=HIWORD(lParam);     //画笔式样
 
 }



void CDirection2View::Setrectwh(WPARAM wParam,LPARAM lParam)
{
	 recrwidth=LOWORD(lParam);
    rectchang=HIWORD(lParam);
}

void CDirection2View::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

    //根据双击的对象范围来响应,跳出相应对象的属性对话框
        CBasepic *pDraw;
        CLine   *pLine;
    	CRectFill *Rectf1;
		CText *text;
        int num;

 // CRectSet *m_rectset;
 // CTextSet *m_textset;
 // CLineSet  * m_lineset;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -