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

📄 drawshapeview.cpp

📁 用于地理信息系统(GIS)行业程序。 完整的鼠标交互式地图点、线、面的绘制
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		CClientDC clientDC(this);
		CRgn rgnFill,rgnMap,rgnScreen;	//求得屏幕区域去除地图区域的部分并填充为白色

//		float fXmin,fYmin,fXmax,fYmax;
//		int iXmin,iYmin,iXmax,iYmax;

		CBrush brColor;
		brColor.CreateSolidBrush(RGB(255,255,255));

		//x,y逻辑坐标偏移量
		int XLogOff = point.x - m_PointOld.x;
		int YLogOff = point.y - m_PointOld.y;
		MemDC.CreateCompatibleDC(NULL);
		CBitmap* pOldBitmap = MemDC.SelectObject(&m_MemBitmap);	

		//计算屏幕上的位图目标位置
		iNewX += XLogOff;
		iNewY += YLogOff;

		//将位图绘制在新的位置
		//如果位图不够大,如果某一图形有一部分在视图区内,一部分在视图区外
		//则在拖动图形时,因为不能用位图的空白部分擦除该部分,所以会出现问题

//		APtoLP(m_XminIntersect,fYmin,&iXmin,&iYmin);
// 		APtoLP(fXmax,fYmax,&iXmax,&iYmax);

//		int iXoff = point.x - m_PointOld.x;
//		int iYoff = point.y - m_PointOld.y;

		m_XminIntersect += XLogOff;
		m_YminIntersect += YLogOff;
		m_XmaxIntersect += XLogOff;
		m_YmaxIntersect += YLogOff;

		rgnMap.CreateRectRgn(m_XminIntersect,m_YminIntersect,
							m_XmaxIntersect,m_YmaxIntersect);
		rgnScreen.CreateRectRgn(0,0,iWScreen,iHScreen);
		rgnFill.CreateRectRgn(0,0,iWScreen,iHScreen);
		rgnFill.CombineRgn(&rgnScreen,&rgnMap,RGN_DIFF);
		clientDC.FillRgn(&rgnFill,&brColor);
		clientDC.BitBlt(iNewX,iNewY,iWScreen,iHScreen,&MemDC,0,0,SRCCOPY);


		rgnMap.DeleteObject();
		rgnScreen.DeleteObject();
		rgnFill.DeleteObject();
		MemDC.SelectObject(pOldBitmap);
		MemDC.DeleteDC();
	
//记录原来点
		m_PointOld = point;


	}



	
	CView::OnMouseMove(nFlags, point);
}

void CDrawShapeView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CClientDC client_dc(this);
	CPen pen(m_LineType,m_LineWide,m_ColorPen);
	CPen* pOldPen=client_dc.SelectObject(&pen);
	client_dc.SetROP2(R2_NOT);
	
	if(m_DrawType==1)
	{
		client_dc.MoveTo(m_PointOrg);//擦除
		client_dc.LineTo(m_PointOld);
		m_PushNum=0;
		ReleaseCapture();
	}
	else if(m_DrawType==2 && m_PushNum > 0)//连续直线
	{
		float x,y;
//		int iWScreen,iHScreen;	//屏幕宽度、高度
//		iWScreen = GetSystemMetrics(SM_CXSCREEN);
// 		iHScreen = GetSystemMetrics(SM_CYSCREEN);
		CDC MemDC;
		CClientDC client_dc(this);
		MemDC.CreateCompatibleDC(NULL);
		CBitmap* pOldBitmap = MemDC.SelectObject(&m_MemBitmap);
		client_dc.BitBlt(0,0,iWScreen,iHScreen,&MemDC,0,0,SRCCOPY);

		LPtoAP(point.x,point.y,&x,&y);
		m_PointArray[m_PushNum].x=x;
		m_PointArray[m_PushNum].y=y;
		m_PushNum++;
		//	TRACE0("Hello world!");
		CDrawShapeDoc* pDoc=GetDocument();
		CPline* pPline=pDoc->AddPline(m_ColorPen,m_ColorBrush,m_LineWide,m_LineType,
			m_Layer,gpCGraphPara->GetIdOnly(),m_bDelete,m_PushNum
			,m_PointArray,0);
		
		pPline->Draw(&MemDC,0,0,m_Color);

		client_dc.BitBlt(0,0,iWScreen,iHScreen,&MemDC,0,0,SRCCOPY);
		MemDC.SelectObject(pOldBitmap);
		MemDC.DeleteDC();
		
		
		m_PushNum=0;//必须要在AddPline函数调用后再置0,因为在该函数中要传递m_PushNum
		ReleaseCapture();
	}
	else if(m_DrawType==3 && m_PushNum > 0)//多边形区域
	{
		float x,y;
//		int iWScreen,iHScreen;	//屏幕宽度、高度
//		iWScreen = GetSystemMetrics(SM_CXSCREEN);
// 		iHScreen = GetSystemMetrics(SM_CYSCREEN);
		CDC MemDC;
		CClientDC client_dc(this);
		MemDC.CreateCompatibleDC(NULL);
		CBitmap* pOldBitmap = MemDC.SelectObject(&m_MemBitmap);
		client_dc.BitBlt(0,0,iWScreen,iHScreen,&MemDC,0,0,SRCCOPY);

		LPtoAP(point.x,point.y,&x,&y);
		m_PointArray[m_PushNum].x=x;
		m_PointArray[m_PushNum].y=y;
		m_PushNum++;
		CDrawShapeDoc* pDoc=GetDocument();
		CPline* pPline=pDoc->AddPline(m_ColorPen,m_ColorBrush,m_LineWide,m_LineType,
			m_Layer,gpCGraphPara->GetIdOnly(),m_bDelete,m_PushNum
			,m_PointArray,1);
		
		//
		pPline->Draw(&MemDC,0,0,m_Color);
		
		client_dc.BitBlt(0,0,iWScreen,iHScreen,&MemDC,0,0,SRCCOPY);
		MemDC.SelectObject(pOldBitmap);
		MemDC.DeleteDC();
		
		m_PushNum=0;//必须要在AddPline函数调用后再置0,因为在该函数中要传递m_PushNum
		ReleaseCapture();
	}
	
	client_dc.SelectObject(pOldPen);
	CView::OnRButtonDown(nFlags, point);
}

//放大
void CDrawShapeView::OnViewZoomin() 
{
	// TODO: Add your command handler code here
	m_DrawType=11;

	//装载应用程序路径下的资源,以取得确定的路径
	hZoominCursor=LoadCursorFromFile(PathName +"\\res\\icon1.ico");

}

//缩小 
void CDrawShapeView::OnViewZoomout() 
{
	// TODO: Add your command handler code here
	m_DrawType=12;
	hZoomoutCursor=LoadCursorFromFile(PathName + "\\res\\icon2.ico");
}

//框选放大
void CDrawShapeView::OnViewZoomrect() 
{
	// TODO: Add your command handler code here
	m_DrawType=13; //框选放大
	m_PushNum=0;
}

void CDrawShapeView::OnViewMove() 
{
	// TODO: Add your command handler code here
	m_DrawType=22;  //移动
	m_PushNum=0;


	hHandCursor=LoadCursorFromFile(PathName + "\\res\\H_MOVE.CUR");

	
}

//全屏显示
void CDrawShapeView::OnViewFullscreen() 
{
	// TODO: Add your command handler code here
	ShowFullScreen();
}

//设置光标形状

BOOL CDrawShapeView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default


	if(hHandCursor!=NULL && m_DrawType==22)  //move map,则设置光标
		SetCursor(hHandCursor);

	if(hZoominCursor!=NULL && m_DrawType==11)  //zoom in
		SetCursor(hZoominCursor);

	if(hZoomoutCursor!=NULL && m_DrawType==12)  //zoom out
		SetCursor(hZoomoutCursor);


	//加上该语句 

	return true;

  //注释以下语句,该语句为:调用基类获得标准光标
//	return CView::OnSetCursor(pWnd, nHitTest, message);
}

void CDrawShapeView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_DrawType==22) //move map
	{
		m_PushNum=0;
		ReleaseCapture();

/*
		//释放鼠标时重画
		CDrawShapeDoc* pDoc = GetDocument();
		CDC MemDC;
		CClientDC clientDC(this);
		MemDC.CreateCompatibleDC(NULL);
		CBitmap *pOldBit=MemDC.SelectObject(&m_MemBitmap);


	//遍历所有图形元素并绘制 
		// 1-直线 2-连续直线或多边形区域 3-点
		for(int i=1; i<4; i++)
		{
			for(int j=0; j <=  pDoc->GetGraphUpperBound(i); j++)
			{
				pDoc->GetGraph(i,j)->Draw(&MemDC,0,0,m_Color);
			}
		}
	
		//将内存(MemDC)中的图拷贝到屏幕上(pDC)进行显示

		clientDC.BitBlt(0,0,iWScreen,iHScreen,&MemDC,0,0,SRCCOPY);
		
	
		//绘图完成后的清理
		MemDC.SelectObject(pOldBit);
		MemDC.DeleteDC();			//清除DC


*/

		Invalidate();
	}

	CView::OnLButtonUp(nFlags, point);
}


void CDrawShapeView::GetAppPath()
{
	CString appPath;

	GetModuleFileName(NULL, appPath.GetBuffer(MAX_PATH), MAX_PATH);
	appPath.ReleaseBuffer();

	// '\\'为转义字符 
	int n=appPath.ReverseFind('\\');
	PathName=appPath.Left(n);
	 n=PathName.ReverseFind('\\');
	PathName=PathName.Left(n);
}

//更新状态栏,显示鼠标位置

void CDrawShapeView::OnUpdateIndicatorX(CCmdUI* pCmdUI)
{
	CString s;
	float Xcoord;
	//实际坐标
	Xcoord=m_xStart + m_PointMouse.x * blc;
	s.Format("%f",Xcoord);
	pCmdUI->SetText(s);
//	pCmdUI->SetCheck(1);
	pCmdUI->Enable(true);
}

void CDrawShapeView::OnUpdateIndicatorY(CCmdUI* pCmdUI)
{
	CString s;
	float Ycoord;
	
	//实际坐标
	Ycoord=m_yStart+blc*(m_hScreen-m_PointMouse.y);
	s.Format("%f",Ycoord);
	pCmdUI->SetText(s);
//	pCmdUI->SetCheck(1);
	pCmdUI->Enable(true);
}

void CDrawShapeView::Get(int abc)
{

}

//void CDrawShapeView::OnFileImport() 
//{
//	// TODO: Add your command handler code here
//	CString pFilePath;
//	CFileDialog dlg(true); 
////	dlg.m_ofn.lpstrFilter = ""
//	if(dlg.DoModal() == IDOK)
//	{
//		pFilePath = dlg.GetPathName();
//	}
//	CDrawShapeDoc* pDoc = GetDocument();
//	pDoc->ReadShapefile(pFilePath);


void CDrawShapeView::OnFileImport() 
{
	// TODO: Add your command handler code here
			CString pFilePath;
	CFileDialog dlg(true); 
	//dlg.m_ofn.lpstrFilter = "Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";

	dlg.m_ofn.lpstrFilter = "shapefile文件(*.shp)\0*.shp";
//	dlg.m_ofn.Flags = OFN_ALLOWMULTISELECT;
	if(dlg.DoModal() == IDOK)
	{
		pFilePath = dlg.GetPathName();
		CDrawShapeDoc* pDoc = GetDocument();
		pDoc->ReadShapefile(pFilePath);
		
	}

}

void CDrawShapeView::OnDrawPoint() 
{
	// TODO: Add your command handler code here
	m_DrawType = 4;
	m_PushNum=0;
}

void CDrawShapeView::Move(float a_fXcoord,float a_fYcoord)
{
	m_xStart = a_fXcoord;
	m_yStart = a_fYcoord;
	Invalidate();
}

void CDrawShapeView::ShowFullScreen()
{
		
	float Xmin,Ymin,Xmax,Ymax;

	GetMapBound(Xmin,Ymin,Xmax,Ymax);


	//屏幕左右,上下各空出15个像素的高度,使得图形在屏幕中央
	
	float blc1=(float)(Xmax - Xmin)/(m_wScreen-30);		//横向比例尺
	float blc2=(float)(Ymax - Ymin)/(m_hScreen-30);		//纵向比例尺
	
	if(blc1 < blc2)  //取较大的比例尺作为比例尺取值
	{
		blc1=blc2;
	}
	blc=blc1;
	
	//屏幕左下角实际坐标,在现有左下角的基础上再缩小20个像素距离
	//可使得图像居中于屏幕
	m_xStart = Xmin - 20*blc;
	m_yStart = Ymin - 20*blc; //原来是10
	
	m_DrawType=0;
	Invalidate();
}

void CDrawShapeView::GetGraphBound(float &Xmin, float &Ymin, float &Xmax, float &Ymax)
{
//		float Xmin,Ymin,Xmax,Ymax;	//边界矩形的左下角,右上角坐标 
		float XminTemp,YminTemp,XmaxTemp,YmaxTemp;	//临时边界矩形的左下角,右上角坐标 
		CDrawShapeDoc* pDoc=GetDocument();

		//赋值
		Xmin=(float)1E+20;
		Ymin=(float)1E+20;
		Xmax=(float)1E-20;
		Ymax=(float)1E-20;
		for(int i=1; i<4; i++)
		{
			for(int j=0; j <=  pDoc->GetGraphUpperBound(i); j++)
			{
				pDoc->GetGraph(i,j)->GetBoundRect(XminTemp,YminTemp,XmaxTemp,YmaxTemp);
				if(XminTemp < Xmin)
				{
					Xmin = XminTemp;
				}

				if(YminTemp < Ymin)
				{
					Ymin = YminTemp;
				}

				if(XmaxTemp > Xmax)
				{
					Xmax = XmaxTemp;
				}

				if(YmaxTemp > Ymax)
				{
					Ymax = YmaxTemp;
				}

			}
		}
		
}

BOOL CDrawShapeView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
	return TRUE;
//	return CView::OnEraseBkgnd(pDC);
}

void CDrawShapeView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
//	this->ShowWindow(SW_SHOWMAXIMIZED);
	// TODO: Add your specialized code here and/or call the base class
	
}

void CDrawShapeView::GetMapBound(float& a_Xmin, float& a_Ymin, float& a_Xmax, float& a_Ymax)
{
	float x1,y1,x2,y2;		//边界矩形左下、右上点坐标
	
	a_Xmin=(float)1E+20;
	a_Ymin=(float)1E+20;
	a_Xmax=(float)1E-20;
	a_Ymax=(float)1E-20;
	
	CDrawShapeDoc* pDoc=GetDocument();
	
	
	//遍历所有图形元素并得到总图形的边界矩形 
	for(int i=1; i<4; i++)
	{
		for(int j=0; j <=  pDoc->GetGraphUpperBound(i); j++)
		{
			CDraw* pDraw=pDoc->GetGraph(i,j);
			
			//j!=0 时才能进入,所以一定能得到该图形元素的指针,得到其边界矩形
			
			//虚函数,实际上是调用各类图形元素得到相应边界矩形
			pDraw->GetBoundRect(x1,y1,x2,y2);
			
			if(x1 < a_Xmin)
			{
				a_Xmin=x1;
			}
			if(y1 < a_Ymin)
			{
				a_Ymin=y1;
			}
			if(x2 > a_Xmax)
			{
				a_Xmax=x2;
			}
			if(y2 > a_Ymax)
			{
				a_Ymax=y2;
			}
		}
	}
	
	
}

⌨️ 快捷键说明

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