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

📄 lineview.cpp

📁 实现DC画线的鼠标任意拖动,适合初学VC者.
💻 CPP
字号:
// LineView.cpp : implementation of the CLineView class
//

#include "stdafx.h"
// #include "Line.h"
#include "math.h"
#include "LineDoc.h"
#include "LineView.h"
#include "Line1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 static int flag=0;
/////////////////////////////////////////////////////////////////////////////
// CLineView

IMPLEMENT_DYNCREATE(CLineView, CView)

BEGIN_MESSAGE_MAP(CLineView, CView)
	//{{AFX_MSG_MAP(CLineView)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLineView construction/destruction

CLineView::CLineView()
{
	// TODO: add construction code here
	m_LButtondown=FALSE;
	m_LButtonUp=FALSE;
	m_font.CreatePointFont(90,"宋体");
	m_online=FALSE;
	m_mDrawline=FALSE;
	m_LineNum=0;
	m_onPoint2=NULL;
    m_onPoint1=NULL;
	DrawMark=FALSE;
}

CLineView::~CLineView()
{
}

BOOL CLineView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
 //  CRect dlg;
   //   dlg.CRect(10,10,20,20);
   
	return CView::PreCreateWindow(cs);
	
}

/////////////////////////////////////////////////////////////////////////////
// CLineView drawing

void CLineView::OnDraw(CDC* pDC)
{
	CLineDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CPen   m_pen;  
	CString str;
	double distance;
	
	
	//if(m_LButtondown==TRUE&&(m_online==TRUE||m_onPoint1==TRUE||m_onPoint2==TRUE))
	//	{
	//		
	//		
	//		pDC->SetROP2(R2_NOTXORPEN);     
	//		m_pen.CreatePen(PS_SOLID,1,RGB(0,255,20));   
	//		CPen*   pen=(CPen*)pDC->SelectObject(&m_pen);	
	//		distance=(float)sqrt(pow(m_line.Point2.x-m_line.Point1.x,2)+pow(m_line.Point2.y-m_line.Point1.y,2));
	//		pDC->SelectObject(&m_font); 
	//		pDC->SetTextColor(RGB(255,0,0));
	//		str.Format( "%f mm",distance);
	//		pDC->TextOut(m_line.Point1.x,m_line.Point1.y,str);
	//		
	//		// TODO: Add your message handler code here
	//		pDC->MoveTo(m_line.Point2);
	//		pDC->LineTo(m_line.Point1);
	//		
	//		
	//	}
	
	
	//else if( DrawMark==TRUE)
	//	  {
	//		
	//		pDC->SetROP2(R2_NOTXORPEN);     
	//		m_pen.CreatePen(PS_SOLID,1,RGB(0,255,20));   
	//		CPen*   pen=(CPen*)pDC->SelectObject(&m_pen);	
	//		distance=(float)sqrt(pow(m_line.Point1.x-m_line.Point2.x,2)+pow(m_line.Point1.y-m_line.Point2.y,2));
	//		pDC->SelectObject(&m_font); 
	//		pDC->SetTextColor(RGB(255,0,0));
	//		str.Format( "%f mm",distance);
	//		pDC->TextOut(m_line.Point1.x,m_line.Point1.y,str);
	//		
	//		// TODO: Add your message handler code here
	//		pDC->MoveTo(m_line.Point1);
	//		pDC->LineTo(m_line.Point2);
	//		
	//		
	//		pDC->Rectangle(m_line.Point1.x-3,m_line.Point1.y-3,m_line.Point1.x+3,m_line.Point1.y+3);
	//		pDC->FillSolidRect(m_line.Point1.x-3,m_line.Point1.y-3,6,6,RGB(255,0,0));
	//		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
	//		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
	//		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
	//		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
	//		pDC->FillSolidRect(m_line.Point2.x-3,m_line.Point2.y-3,6,6,RGB(255,0,0)); 
	//		
	//	}
	
	//else
	//	{
	
		
		
		pDC->SetROP2(R2_NOTXORPEN);     
		m_pen.CreatePen(PS_SOLID,1,RGB(0,255,20));   
		CPen*   pen=(CPen*)pDC->SelectObject(&m_pen);	
		distance=(float)sqrt(pow(m_line.Point1.x-m_line.Point2.x,2)+pow(m_line.Point1.y-m_line.Point2.y,2));
		pDC->SelectObject(&m_font); 
		pDC->SetTextColor(RGB(255,0,0));
		str.Format( "%0.0f mm",distance);
		
		pDC->TextOut(m_line.Point1.x+2,m_line.Point1.y+2,str);
		
		// TODO: Add your message handler code here
		pDC->MoveTo(m_line.Point1);
		pDC->LineTo(m_line.Point2);
		pDC->Rectangle(m_line.Point1.x-3,m_line.Point1.y-3,m_line.Point1.x+3,m_line.Point1.y+3);
		pDC->FillSolidRect(m_line.Point1.x-3,m_line.Point1.y-3,6,6,RGB(255,0,0));
		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
		pDC->Rectangle(m_line.Point2.x-3,m_line.Point2.y-3,m_line.Point2.x+3,m_line.Point2.y+3);
		pDC->FillSolidRect(m_line.Point2.x-3,m_line.Point2.y-3,6,6,RGB(255,0,0));
//		
//	}

		
		
		
		
	}
	
/////////////////////////////////////////////////////////////////////////////
// CLineView printing

BOOL CLineView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLineView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLineView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CLineView diagnostics

#ifdef _DEBUG
void CLineView::AssertValid() const
{
	CView::AssertValid();
}

void CLineView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CLineDoc* CLineView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLineDoc)));
	return (CLineDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLineView message handlers

void CLineView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CRect pt(m_line.Point1,m_line.Point2);
	if(m_LineNum==0)
	{
	   m_line.Point1=point;
	   	m_line.Point2=point;
	}
	
	if (m_LineNum>=1&& Distance(point,m_line.Point2)<=3)
				{
	            	m_onPoint2=TRUE;
				}
				else
				{
					m_onPoint2=FALSE;
				}
				if(m_LineNum>=1&&Distance(point,m_line.Point1)<=3)
				{
					m_onPoint1=TRUE;
					
				}
				else
				{
					m_onPoint1=FALSE;
					
				}
			
				if (m_LineNum>=1&&pt.PtInRect(point)&&PointLineDistance(point,m_line.Point1,m_line.Point2)<10&&m_onPoint2==FALSE&&m_onPoint1==FALSE)
				
				{
					m_online=TRUE; 
				} 
					
				else 
				{
					m_online=FALSE;
				}
					
				
				
				
				
	
	
	 
      //  DrawMark=TRUE;

	   if (m_online==TRUE)
	   {
		   P3=point;
		 //   DrawMark=FALSE;
		    
	   }

	m_LButtondown=TRUE;
 Invalidate();

 
	CView::OnLButtonDown(nFlags, point);

}

void CLineView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	CString str;
	float distance;
	


		if (m_LButtondown==TRUE)
			
		{  

			if(m_onPoint1==FALSE&&m_onPoint2==FALSE&&m_online==FALSE)
			{
	
				CPen   m_pen;   
			    dc.SetROP2(R2_NOTXORPEN);     
		        m_pen.CreatePen(PS_SOLID,1,RGB(0,255,20));   
			    CPen*   pen=(CPen*)dc.SelectObject(&m_pen);	
			    dc.MoveTo(m_line.Point1);
			    dc.LineTo(m_line.Point2);
			    distance=(float)sqrt(pow(m_line.Point2.x-m_line.Point1.x,2)+pow(m_line.Point2.y-m_line.Point1.y,2));
                dc.SelectObject(&m_font); 
		        dc.SetTextColor(RGB(255,0,0));
		        str.Format( "%0.0f mm",distance);
		    	dc.TextOut(m_line.Point1.x+2,m_line.Point1.y+2,str);
			    dc.MoveTo(m_line.Point1);
			    dc.LineTo(point);
		    	m_line.Point2=point;
			
			  
			}
			else if (m_onPoint1==TRUE&&m_onPoint2==FALSE&&m_online==FALSE)//如果选中第一个点
			{
				
				CPen   m_pen;   
				dc.SetROP2(R2_NOTXORPEN);     
				m_pen.CreatePen(PS_SOLID,1,RGB(0,255,20));   
				CPen*   pen=(CPen*)dc.SelectObject(&m_pen);	
				distance=(float)sqrt(pow(m_line.Point2.x-m_line.Point1.x,2)+pow(m_line.Point2.y-m_line.Point1.y,2));
				dc.SelectObject(&m_font); 
				dc.SetTextColor(RGB(255,0,0));	
				dc.MoveTo(m_line.Point2);
				dc.LineTo(m_line.Point1);
				str.Format( "%0.0f mm",distance);
				dc.TextOut(m_line.Point1.x+2,m_line.Point1.y+2,str);
				
				dc.MoveTo(m_line.Point2);
				dc.LineTo(point);
				m_line.Point1=point;
				Invalidate();
			
				
			}
			else if (m_onPoint2==TRUE&&m_online==FALSE)//如果选中第二个点
			{
				//if(flag==1)//交换point1 和point2的标志
				//					
				//				{
				//					P3=m_line.Point1;
				//				   m_line.Point1=m_line.Point2;
				//			      	m_line.Point2=P3;
				//					flag=0;
				//				}	
				//				
				
              
				CPen   m_pen;   
				dc.SetROP2(R2_NOTXORPEN);     
				m_pen.CreatePen(PS_SOLID,1,RGB(0,255,20));   
				CPen*   pen=(CPen*)dc.SelectObject(&m_pen);	
				dc.MoveTo(m_line.Point1);
				dc.LineTo(m_line.Point2);
				distance=(float)sqrt(pow(m_line.Point2.x-m_line.Point1.x,2)+pow(m_line.Point2.y-m_line.Point1.y,2));
				dc.SelectObject(&m_font); 
				dc.SetTextColor(RGB(255,0,0));
				str.Format( "%0.0f mm",distance);
				dc.TextOut(m_line.Point1.x+2,m_line.Point1.y+2,str);
				dc.MoveTo(m_line.Point1);
				dc.LineTo(point);
				m_line.Point2=point;

			

			}
			
			else if (m_online==TRUE)
			{

				m_line.Point1.y =(m_line.Point1.y-P3.y)+point.y;
				m_line.Point2.y=(m_line.Point2.y-P3.y)+point.y;
				m_line.Point1.x=(m_line.Point1.x-P3.x)+point.x;
				m_line.Point2.x=(m_line.Point2.x-P3.x)+point.x;
			//	//str.Format("%f, %f ",m_line.Point1.x,m_line.Point1.y);
			//				//				dc.TextOut(500,500,str);
		         	P3=point;
				
			      Invalidate();

			}
			

		  }
		
			   
			
 

	


	CView::OnMouseMove(nFlags, point);
}



void CLineView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	    m_LButtondown=FALSE;
		m_LButtonUp=TRUE;
		if (m_online==FALSE&&m_onPoint1==FALSE&&m_onPoint2==FALSE)
		{
         	m_LineNum++;
		}
		Invalidate();
    	m_online=FALSE;
		m_onPoint1=FALSE;
		m_onPoint2=FALSE;
		
		
	
	CView::OnLButtonUp(nFlags, point);
}

double CLineView::Distance(CPoint p1, CPoint p2)//两点之间的距离
{
	double distance=0;
	distance =sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
	return distance;

}

double CLineView::PointLineDistance(CPoint p1, CPoint p2, CPoint p3)//点p1到有点p2 p3 组成的直线的距离
{
 double distance=0;
 double k=0;
 if(p2.x-p3.x==0)
 {
	distance =p1.y-p2.y;
 }
 else
 {
	 k=(p2.y-p3.y)/(p2.x-p3.x);
	 distance=(double)fabs(-k*p1.x+p1.y+k*p2.x-p2.y)/sqrt(pow(k,2)+1);
 }
 
 return distance;
 
}

⌨️ 快捷键说明

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