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

📄 tool.cpp

📁 在PDA中的画图程序
💻 CPP
字号:
// Tool.cpp: implementation of the CTool class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DrawInPDA.h"
#include "Tool.h"
#include "Shape.h"
#include "DrawInPDAView.h"
#include "ToolZoomOut.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


extern CDrawInPDAView *p_View;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPtrList CTool::c_ToolList;	
static CToolPoint thePointTool;
static CToolLine theLineTool;
static CToolPoly thePolyTool;
static CToolZoomIn theZoomInTool;	    //  所有工具列表
static CToolRectangle theRectangleTool; 
static CToolSelect theSelectTool;
static CToolZoomOut theZoomTool;
static CToolMove theMoveTool;
 
CPoint CTool::c_DownPoint;		// 定义:记录鼠标发生动作时的位置(MouseDown)
CPoint CTool::c_LastPoint;		// 定义:记录鼠标发生动作时的位置(MouseMove)
UINT CTool::c_nDownFlags;		// 定义:鼠标动作时键盘状态
eToolType CTool::c_CurrentTool = ttDrawPoint; // 当前工具

eSelectMode c_SelectMode = smNone;

CTool::CTool()
{

}

CTool::~CTool()
{

}

CTool::CTool(eToolType ATool)
{
	m_Tool = ATool;
	c_ToolList.AddTail(this);

}

void CTool::OnLButtonDown(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	pView->SetCapture();
	c_nDownFlags = nFlags;
	c_DownPoint = point;
}

void CTool::OnLButtonDblClk(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{

}

void CTool::OnLButtonUp(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{

}

void CTool::OnMouseMove(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	c_LastPoint = point;
}

void CTool::OnCancel()
{
   c_CurrentTool = ttSelect;
}

CTool* CTool::FindTool(eToolType ATool)
{
	POSITION pos = c_ToolList.GetHeadPosition();
	while (pos != NULL)
	{
		CTool* pTool = (CTool*)c_ToolList.GetNext(pos);
		if (pTool->m_Tool == ATool)
			return pTool;
	}
	return NULL;
}

/////////////////////////////////////////////CToolPoly
CToolPoly::CToolPoly()
    : CTool(ttDrawPolygon)
{
    bFirstClick = true;
}

CToolPoly::~CToolPoly()
{

}

void CToolPoly::OnLButtonDown(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	
	CGeoPoint local;
	p_View->VPtoDP(point.x,point.y,&local.x,&local.y);	
	if (m_Poly == NULL)
	{
		pView->SetCapture();//The SetCapture function sets the mouse capture to the specified window 
		                    //belonging to the current thread
		m_Poly = new CShapePoly();
    	pView->GetDocument()->ShapeAdd(m_Poly);//A pointer to the CDocument object associated with the view
    	m_Poly->AddPoint(local, pView);//Call this function to get a pointer to the view’s document. 
	                          //This allows you to call the document’s member functions.
		bFirstClick = false;
	}
	else if ((local.x== m_Poly->m_sPoints[0].x)&&(local.y== m_Poly->m_sPoints[0].y))
	{
		// Stop when the first point is repeated...
		ReleaseCapture();
		m_Poly->m_nPoints -= 1;
		if (m_Poly->m_nPoints < 2)
		{
			//m_pDrawObj->Remove();
		}
		else
		{
			m_Poly->Draw(pView->GetDC());
		}

		m_Poly = NULL;
		c_CurrentTool = ttSelect;
		return;
	}
	m_Poly->AddPoint(local, pView);


	CTool::OnLButtonDown(pView, nFlags, point);

}

void CToolPoly::OnMouseMove(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	CDC* pDC;
	pDC = pView->GetDC();//The GetDC function retrieves a handle to a display device context for the 
	                     //client area of a specified window or for the entire screen
	pDC->SetROP2(R2_NOT);//The SetROP2 function sets the current foreground mix mode
                         //R2_NOT:Pixel is the inverse of the screen color.
	if( !bFirstClick )
	{
		// 绘制多边形要用的!!
		//CPoint firstPoint;
		//firstPoint = m_Poly->m_sPoints[0];
		//if(m_Poly->m_nPoints>1)
		//{
		//	if(c_LastPoint!=m_Poly->m_sPoints[1])
		//	{
		//		pDC->MoveTo(firstPoint);
		//		pDC->LineTo(c_LastPoint);
		//	}
		//	pDC->MoveTo(firstPoint);
		//	pDC->LineTo(point);
		//}
		pDC->MoveTo(c_DownPoint);
		pDC->LineTo(c_LastPoint);
		pDC->MoveTo(c_DownPoint);
		pDC->LineTo(point);
	}
	
	CTool::OnMouseMove(pView, nFlags, point);
    ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));


}

void CToolPoly::OnLButtonUp(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	
}

void CToolPoly::OnLButtonDblClk(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	ReleaseCapture();//Call this function to release mouse capture
	CGeoPoint local;
	p_View->VPtoDP(point.x,point.y,&local.x,&local.y);	

	int nPoints = m_Poly->m_nPoints;
		if (nPoints > 2 &&
		(((m_Poly->m_sPoints[nPoints - 1].x == m_Poly->m_sPoints[nPoints - 2].x)&&(m_Poly->m_sPoints[nPoints - 1].y == m_Poly->m_sPoints[nPoints - 2].y))||
		m_Poly->m_sPoints[nPoints - 1].x-1== m_Poly->m_sPoints[nPoints - 2].x &&
		m_Poly->m_sPoints[nPoints - 1].y == m_Poly->m_sPoints[nPoints - 2].y))


/*	if (nPoints > 2 &&
		//(m_Poly->m_sPoints[nPoints - 1] == m_Poly->m_sPoints[nPoints - 2] ||
		(m_Poly->m_sPoints[nPoints - 1].x== m_Poly->m_sPoints[nPoints - 2].x ||
		m_Poly->m_sPoints[nPoints - 1].y == m_Poly->m_sPoints[nPoints - 2].y)) */

	{
		m_Poly->m_nPoints -= 1;

	}
	m_Poly->AddPoint(local, pView);

	m_Poly->Draw(pView->GetDC());

	m_Poly = NULL;
	bFirstClick = true;

	
}


//////////////////////////////////
/////////////////////
CToolSelect::CToolSelect()
	: CTool(ttSelect)
{
	m_bSecondClick=false;
}

void CToolSelect::OnLButtonDown(CDrawInPDAView* pView, UINT nFlags, const CPoint& point)
{
	CPoint local = point;
	//pView->ClientToDoc(local);

	CShape* pObj;
	c_SelectMode = smNone;

	if (c_SelectMode == smNone)
	{
		pObj = pView->GetDocument()->Select(local);

		if (pObj != NULL)
		{
			//MessageBox("Help, Something went wrong.", "Error", 
 //   MB_ICONERROR | MB_OK);
    //	AfxMessageBox("Selected.",MB_YESNO|MB_ICONSTOP);
	//MessageBox("Selected","Selecte",MB_ICONINFORMATION|MB_OK);
			pView->m_SelectionShape.AddTail(pObj);
			//selectMode = move;

			//if (!pView->IsSelected(pObj))
			//	pView->Select(pObj, (nFlags & MK_SHIFT) != 0);

			// Ctrl+Click clones the selection...
			//if ((nFlags & MK_CONTROL) != 0)
			//	pView->CloneSelection();
		}
	}
}
void CToolSelect::OnLButtonDblClk(CDrawInPDAView* pView, UINT nFlags, const CPoint& point){}
void CToolSelect::OnLButtonUp(CDrawInPDAView* pView, UINT nFlags, const CPoint& point){}

void CToolSelect::OnMouseMove(CDrawInPDAView* pView, UINT nFlags, const CPoint& point)
{
	CTool::OnMouseMove(pView, nFlags, point);
	//	::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
}


////////////////////////
CToolPoint::CToolPoint():CTool(ttDrawPoint)
{

}

CToolPoint::~CToolPoint()
{

}

void CToolPoint::OnLButtonDown(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	m_Point = new CShapePoint();
	p_View->VPtoDP(point.x,point.y,&m_Point->m_gPoint.x,&m_Point->m_gPoint.y);

	
	CRect rect(point - CSize(1,1), CSize(2,2));
	m_Point->m_extent = rect;		// 设置点元素的Extent

	pView->GetDocument()->ShapeAdd(m_Point);	// 将当前点存入列表
	m_Point->Draw(pView->GetDC());				// 绘制当前点

	CTool::OnLButtonDown(pView, nFlags, point);


}

void CToolPoint::OnLButtonDblClk(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	

}

void CToolPoint::OnLButtonUp(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	ReleaseCapture();
	CTool::OnLButtonUp(pView, nFlags, point);

}

void CToolPoint::OnMouseMove(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
	CTool::OnMouseMove(pView, nFlags, point);
}


//////////////////////////////
CToolLine::CToolLine()
    :CTool(ttDrawLine)
{
   bFirstClick = true;	
}

CToolLine::~CToolLine()
{

}

void CToolLine::OnLButtonDown(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{    float x1,x2,y1,y2;
	if( bFirstClick )
	{	
		m_Line = new CShapeLine();
		p_View->VPtoDP(int(point.x),int(point.y),&x1,&y1);

		m_Line->m_sPoint[0].m_gPoint.x=x1;
		m_Line->m_sPoint[0].m_gPoint.y=y1;
		//m_Line->m_sPoint[0].m_gPoint.y = point.y;
		//m_Line->m_sPoint[0].Draw(pView->GetDC());
		bFirstClick = !bFirstClick;
	}
	else
	{	p_View->VPtoDP(int(point.x),int(point.y),&x2,&y2);
		m_Line->m_sPoint[1].m_gPoint.x=x2;
		m_Line->m_sPoint[1].m_gPoint.y=y2;
//  	m_Line->m_sPoint[1].m_gPoint.y = point.y;
//  	m_Line->m_sPoint[1].Draw(pView->GetDC());

		pView->GetDocument()->ShapeAdd(m_Line);
//		m_Line->Draw(pView->GetDC());
		bFirstClick = !bFirstClick;
		ReleaseCapture();
		return;
	}
	CTool::OnLButtonDown(pView, nFlags, point);

}

void CToolLine::OnMouseMove(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	CDC* pDC;
	pDC = pView->GetDC();
	pDC->SetROP2(R2_NOT);
	CPen Pen(0, PS_SOLID, RGB(255,0,0));
    CPen* pOldPen = pDC->SelectObject(&Pen);
	if( !bFirstClick )
	{
		pDC->MoveTo(c_DownPoint);
		pDC->LineTo(c_LastPoint);
		pDC->MoveTo(c_DownPoint);
		pDC->LineTo(point);
	}
	pDC->SelectObject(pOldPen);
	
	CTool::OnMouseMove(pView, nFlags, point);
	//::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));

}

void CToolLine::OnLButtonUp(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{

}

void CToolLine::OnLButtonDblClk(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{

}

//////////////////////////////////////////CToolRectangle
CToolRectangle::CToolRectangle()
         :CTool(ttDrawRectangle)
{
      m_nClick = 0;
}

CToolRectangle::~CToolRectangle()
{

}
void CToolRectangle::OnLButtonDown(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	CGeoPoint local;
	m_nClick++;
	p_View->VPtoDP(point.x,point.y,&local.x,&local.y);	
	if( m_nClick == 1 )
	{
		m_pShape = new CShapeRectangle();
		m_pShape->m_sPoints[m_nClick-1].m_gPoint.x = local.x;
		m_pShape->m_sPoints[m_nClick-1].m_gPoint.y = local.y;
	}
	else if( m_nClick == 2 )
	{
		m_pShape->m_sPoints[m_nClick-1].m_gPoint.x = local.x;
		m_pShape->m_sPoints[m_nClick-1].m_gPoint.y = local.y;
		pView->GetDocument()->ShapeAdd(m_pShape);
		m_pShape->Draw(pView->GetDC());
		m_nClick = 0;
		m_pShape = NULL;
		ReleaseCapture();
		return;
	}
	
	CTool::OnLButtonDown(pView, nFlags, point);

}

void CToolRectangle::OnMouseMove(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	CDC* pDC;
	pDC = pView->GetDC();
	pDC->SetROP2(R2_NOT);
	if( m_nClick != 0 )
	{
		// 绘制多边形要用的!!
		//CPoint firstPoint;
		//firstPoint = m_Poly->m_sPoints[0];
		//if(m_Poly->m_nPoints>1)
		//{
		//	if(c_LastPoint!=m_Poly->m_sPoints[1])
		//	{
		//		pDC->MoveTo(firstPoint);
		//		pDC->LineTo(c_LastPoint);
		//	}
		//	pDC->MoveTo(firstPoint);
		//	pDC->LineTo(point);
		//}
		pDC->MoveTo(c_DownPoint);
		pDC->LineTo(c_DownPoint.x,c_LastPoint.y);
		pDC->LineTo(c_LastPoint);
		pDC->LineTo(c_LastPoint.x,c_DownPoint.y);
		pDC->LineTo(c_DownPoint);

		pDC->MoveTo(c_DownPoint);
		pDC->LineTo(c_DownPoint.x,point.y);
		pDC->LineTo(point);
		pDC->LineTo(point.x,c_DownPoint.y);
		pDC->LineTo(c_DownPoint);
		
	}
	
	CTool::OnMouseMove(pView, nFlags, point);
	::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));

}

void CToolRectangle::OnLButtonUp(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	CTool::OnLButtonUp(pView, nFlags, point);
}
void CToolRectangle::OnLButtonDblClk(CDrawInPDAView *pView, UINT nFlags, const CPoint &point)
{
	CTool::OnLButtonDblClk(pView, nFlags, point);
}




⌨️ 快捷键说明

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