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

📄 mapview.cpp

📁 用VC开发的GPS监控跟踪软件,已经调试过,并在实际中使用,现与大家共享
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	// subtract lPixPerInch from center to get 1 inch to left of Center
	screenX = (float)(rc.right/2-lPixPerInch);
	screenY = (float)(rc.bottom/2);
	try { 
		m_ctrlMapX.ConvertCoord(&screenX, &screenY, &mapX1, &mapY1, miScreenToMap);
	}
	catch (COleDispatchException *e) {
		e->ReportError();
		e->Delete();
	}
	catch (COleException *e) {
		e->ReportError();
		e->Delete();	
	}
	
	double mapX2, mapY2;
	// add lPixPerInch to center to get 1 inch to right of Center
	screenX = (float)(rc.right/2+lPixPerInch);
	screenY = (float)(rc.bottom/2);
	try {
		m_ctrlMapX.ConvertCoord(&screenX, &screenY, &mapX2, &mapY2, miScreenToMap);
	}
	catch (COleDispatchException *e) {
		e->ReportError();
		e->Delete();
	}
	catch (COleException *e) {
		e->ReportError();
		e->Delete();	
	}
	CFont font;
	font.CreatePointFont(100, "Arial", pDC);

	pOldFont = pDC->SelectObject(&font);
	pDC->SetTextColor(0);	//Black
	pDC->SetBkMode(TRANSPARENT);

	// Output ScaleBar start Label = 0
	x1=(long)(startX*1000);
	y1=(long)((startY-.2)*1000);
	pDC->TextOut(x1,-y1,"0");

	// Output label for distance of 1 inch
	x1=(long)(barWidth*2+startX*1000);

	CString str;
	str.Format("%.0f",m_ctrlMapX.Distance(mapX1, mapY1, mapX2, mapY2)/2);
	// Need to center text over center line.  will move it 60 units for each character
	int iCenterAdjustment = 60*str.GetLength();
	pDC->TextOut(x1-iCenterAdjustment,-y1,str);

	// Output label for distance of 2 inches
	x1=(long)(barWidth*4+startX*1000);

	str.Format("%.0f",m_ctrlMapX.Distance(mapX1, mapY1, mapX2, mapY2));
	// Need to center text over end line.  will move it 60 units for each character
	iCenterAdjustment = 60*str.GetLength();
	pDC->TextOut(x1-iCenterAdjustment,-y1,str);

	// Output label for Units "Miles"
	x1=(long)(barWidth*2+startX*1000);
	y1=(long)((startY+.2)*1000);
	// Need to center text over center line.  will move it 60 units for each character
	str.Format("Miles");
	iCenterAdjustment = 60*str.GetLength();
	pDC->TextOut(x1-iCenterAdjustment,-y1,str);

	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldFont);

	// detach dc so destructore won't call Release it
	pDC->ReleaseAttribDC();
	pDC->Detach();
}
///标题控制
void CMapView::SetTitle(CString str)
{
   m_ctrlMapX.SetTitleText(str); 
}

void CMapView::SetTitleVisible(BOOL bState)
{
   m_ctrlMapX.GetTitle().SetVisible(bState);
}

////////////////////////////////////
void CMapView::OnMapeditSelettable() 
{
	CSelLayerDlg LayerDlg(&m_ctrlMapX);
	LayerDlg.SetEditLayerName(m_strEditLayer);
	if(LayerDlg.DoModal()==IDOK)
	m_strEditLayer=LayerDlg.GetEditLayerName();
	
}

BOOL CMapView::FindLayer(CString strLayerName)
{
	CMapXLayers layers=m_ctrlMapX.GetLayers();
    for(int i=0;i<layers.GetCount();i++)
	{
		CMapXLayer layer=layers.Item(i+1);
		if(layer.GetName()==strLayerName) return TRUE;

	}
	return FALSE;

}

BOOL CMapView::FindLayer(CString strLayerName,CMapXLayer& layer)
{
	CMapXLayers layers=m_ctrlMapX.GetLayers();
    for(int i=0;i<layers.GetCount();i++)
	{
		layer=layers.Item(i+1);
		if(layer.GetName()==strLayerName) 
		return TRUE;

	}
	return FALSE;

}
/////////////////////////////////////////////
void CMapView::OnPolyToolUsedMap(short ToolNum, long Flags, LPDISPATCH Points, BOOL bShift, BOOL bCtrl, BOOL FAR* EnableDefault) 
{
	// see CSampleDlg for example on usage on creating polygons or lines

	// calc distance so far
	if (ToolNum == MYTOOL_DISTANCE) {
		CMapXPoints pts;
		long n;
		long i;
		try {
			pts.AttachDispatch(Points, FALSE); // don't auto release
			double dDistanceTot = 0.0;
			n = pts.GetCount();
			for (i=1; i<n; i++) {
				CMapXPoint pt1 = pts.Item(i);
				CMapXPoint pt2 = pts.Item(i+1);
				double d = m_ctrlMapX.Distance(pt1.GetX(), pt1.GetY(), pt2.GetX(), pt2.GetY());
				dDistanceTot += d;
			}
			CString str;
			str.Format("Distance Tot: %f", dDistanceTot);
			((CMainFrame *)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(0, str);
		}
		catch (COleDispatchException *e) {
			e->ReportError();
			e->Delete();
		}
		catch (COleException *e) {
			e->ReportError();
			e->Delete();
		}
	}
	else if(Flags==miPolyToolEnd&&ToolNum==CUSTOM_DRAW_REGION_TOOL)
	{
		CMapXFeatureFactory cFactory=m_ctrlMapX.GetFeatureFactory();
        CMapXLayer layer; 
		if(m_strEditLayer!="")
			layer=m_ctrlMapX.GetLayers().Item(m_strEditLayer);
        else {
		   	AfxMessageBox("你没有选择可编辑图层,请选择编辑图层");
            return; 
        }

		CMapXFeature feature;
		CMapXPoints points;
		points.AttachDispatch(Points, FALSE); // don't auto release

		COleVariant vtPoints;
		vtPoints.vt = VT_DISPATCH;
		vtPoints.pdispVal = points.m_lpDispatch;
		vtPoints.pdispVal->AddRef();
		feature=cFactory.CreateRegion(vtPoints); 

		layer.AddFeature(feature);
	

	}
}
void CMapView::OnToolUsed(short ToolNum, double X1, double Y1, double X2, double Y2, double Distance, BOOL Shift, BOOL Ctrl, BOOL* EnableDefault)
{
	CString str;
	str.Format("Tool=%d, [%f,%f] [%f, %f], dist=%f, %s %s\n",
		ToolNum, X1,Y1,X2,Y2,Distance, (Shift)?"Shift":"",(Ctrl)?"Ctrl":"");
	TRACE(str);
	//AfxMessageBox(str);

	// this info tool will pop up a modal dlg 
	// with info from the dataset(1)
	// Note that we are only getting info from the MapX Dataset
	// you could use the SourceRows collection to get back
	// to the source data to display info from dao, odbc or whatever
	if (ToolNum == MYTOOL_INFO) {
		OnInfoToolUsed(m_ctrlMapX);
	}
	
	if(ToolNum>=CUSTOM_DRAW_TEXT_TOOL&&ToolNum <= CUSTOM_DRAW_ARC_TOOL) 
    {
		
		CMapXFeatureFactory cFactory=m_ctrlMapX.GetFeatureFactory();
        CMapXLayer layer; 
		if(m_strEditLayer!="")
			layer=m_ctrlMapX.GetLayers().Item(m_strEditLayer);
        else {
		   	AfxMessageBox("你没有选择可编辑图层,请选择编辑图层");
            return; 
        }

		CMapXPoint point;
		point.CreateDispatch(point.GetClsid());	//Creates a dispatch for the point
		CMapXFeature feature;
		CMapXPoints points;
		points.CreateDispatch(points.GetClsid());

		CMapXRectangle rect;
		rect.CreateDispatch(rect.GetClsid());
        switch(ToolNum)
		{
		case CUSTOM_DRAW_TEXT_TOOL:
            {
				
				point.Set(X1,Y1);
				COleVariant vtPoint;
				vtPoint.vt = VT_DISPATCH;
				vtPoint.pdispVal = point.m_lpDispatch;
				vtPoint.pdispVal->AddRef();	
				feature=cFactory.CreateText(vtPoint,"测试文档  gg"); 
                CMapXStyle style=feature.GetStyle();
				style.PickText();
				feature.SetStyle(style.m_lpDispatch);  
				layer.AddFeature(feature);

            } 
			break;
        case CUSTOM_DRAW_LINE_TOOL:
			{

                points.AddXY(X1,Y1);
				points.AddXY(X2,Y2);

				COleVariant vtPoints;
				vtPoints.vt = VT_DISPATCH;
				vtPoints.pdispVal = points.m_lpDispatch;
				vtPoints.pdispVal->AddRef();
				feature=cFactory.CreateLine(vtPoints); 

				layer.AddFeature(feature);


			}
			break;
		case CUSTOM_DRAW_RECT_TOOL:
			{

			    VARIANT scx,scy;
				m_ctrlMapX.ConvertCoordV(&scx,&scy,COleVariant(X1),COleVariant(Y1),miMapToScreen);
                VARIANT scx1,scy1;
				m_ctrlMapX.ConvertCoordV(&scx1,&scy1,COleVariant(X2),COleVariant(Y2),miMapToScreen);
				
				VARIANT mapx1,mapy1,mapx2,mapy2;
				

				m_ctrlMapX.ConvertCoordV(&scx,&scy1,&mapx1,&mapy1,miScreenToMap);
				m_ctrlMapX.ConvertCoordV(&scx1,&scy,&mapx2,&mapy2,miScreenToMap);
				
				double x2,y2,x4,y4;
				//mapx1.ChangeType(VT_R8);
				x2= mapx1.dblVal;
				
				//mapy1.ChangeType(VT_R8);
				y2=mapy1.dblVal;

				//mapx2.ChangeType(VT_R8);
				x4=mapx2.dblVal;

				//mapy2.ChangeType(VT_R8*;
				y4=mapy2.dblVal;


				points.AddXY(X1,Y1);
				points.AddXY(x2,y2);
				points.AddXY(X2,Y2);
				points.AddXY(x4,y4);




				COleVariant vtPoints;
				vtPoints.vt = VT_DISPATCH;
				vtPoints.pdispVal = points.m_lpDispatch;
				vtPoints.pdispVal->AddRef();
				feature=cFactory.CreateRegion(vtPoints); 

				layer.AddFeature(feature);


			}
            break;
	   case CUSTOM_DRAW_ARC_TOOL:	
		   {
				points.AddXY(X1,Y1);
				points.AddXY(X2,Y2);
				
				feature=cFactory.CreateArc(points.Item(1).m_lpDispatch,points.Item(2).m_lpDispatch); 

				layer.AddFeature(feature);


		   }
		   break;
       case CUSTOM_DRAW_ELLIPSE_TOOL:
			{

				rect.Set(X1,Y1,X2,Y2);
            	feature=cFactory.CreateEllipticalRegion(rect.m_lpDispatch , 0, 30); 
				layer.AddFeature(feature);
			}
			break;
	/*	Select Case ToolNum
            Case 
                ptItem.Set x1, y1
                Set ftrItem = ftrFactory.CreateText(ptItem, "TEXT", _
                    miPositionCC, Map1.DefaultStyle)
                lyrEdit.AddFeature ftrItem
                Set ftrItem = Nothing
            Case CUSTOM_DRAW_SYMBOL_TOOL
                ptItem.Set x1, y1
                Set ftrItem = ftrFactory.CreateSymbol(ptItem, Map1.DefaultStyle)
                lyrEdit.AddFeature ftrItem
                Set ftrItem = Nothing
            Case CUSTOM_DRAW_LINE_TOOL
                ptsItem.AddXY x1, y1
                ptsItem.AddXY x2, y2, 2
                Set ftrItem = ftrFactory.CreateLine(ptsItem, Map1.DefaultStyle)
                lyrEdit.AddFeature ftrItem
                Set ftrItem = Nothing
            Case CUSTOM_DRAW_ELLIPSE_TOOL
                rctItem.Set x1, y1, x2, y2
                Set ftrItem = ftrFactory.CreateEllipticalRegion(rctItem, 0, 30, Map1.DefaultStyle)
                lyrEdit.AddFeature ftrItem
                Set ftrItem = Nothing
        End Select
    Ed If*/


		} 
	}
}
void CMapView::OnInfoToolUsed(CMapX &cMapX)
{
	/*	try {
			if (cMapX.GetDatasets().GetCount() == 0) {
				AfxMessageBox("This info tool requires that a dataset be added.");
				return;
			}
			CMapXPoint pt;
			CMapXFeatures fs;
			CMapXFeature f;
			CMapXDataset ds;

			pt.CreateDispatch(pt.GetClsid());
			pt.Set(X1,Y1);
			
			fs = cMapX.GetLayers().Item("USA").SearchAtPoint(pt);
			// just use first feature from collection. Since usa is a layer of
			// all regions, we will not get back > 1 feature in the collection
			// because there are no overlapping regions
			if (fs.GetCount() != 1) {
				return;
			}
			f = fs.Item(1);
			ds = cMapX.GetDatasets().Item(1);
			
			CInfoDlg dlg;
			COleVariant vRow;
			COleVariant vVal;

			vRow.vt = VT_DISPATCH;
			vRow.pdispVal = f.m_lpDispatch;
			vRow.pdispVal->AddRef();

			vVal = ds.GetValue(vRow, COleVariant(2L));
			vVal.ChangeType(VT_BSTR);
			dlg.m_strState = vVal.bstrVal;

			vVal = ds.GetValue(vRow, COleVariant(3L));
			vVal.ChangeType(VT_R8);
			dlg.m_dTotal = vVal.dblVal;

			vVal = ds.GetValue(vRow, COleVariant(4L));
			vVal.ChangeType(VT_R8);
			dlg.m_dFemale = vVal.dblVal;
			
			vVal = ds.GetValue(vRow, COleVariant(5L));
			vVal.ChangeType(VT_R8);
			dlg.m_dMale = vVal.dblVal;

			dlg.DoModal();
		}
		catch (COleDispatchException *e) {
			e->ReportError();
			e->Delete();
		}
		catch (COleException *e) {
			e->ReportError();
			e->Delete();
		}*/

}

void CMapView::OnMapeditAddtext() 
{
	//m_ctrlMapX.SetCurrentTool(miTextTool);
	m_ctrlMapX.SetCurrentTool(CUSTOM_DRAW_TEXT_TOOL);
	
}
void CMapView::OnUpdateMapeditAddtext(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool()==miTextTool);
	
}
void CMapView::OnMapeditAddsymbol() 
{
    m_ctrlMapX.SetCurrentTool(miSymbolTool);	
}
void CMapView::OnUpdateMapeditAddsymbol(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool()==miSymbolTool);
	
}

void CMapView::OnMapeditAddline() 
{
	m_ctrlMapX.SetCurrentTool(CUSTOM_DRAW_LINE_TOOL);
	
}

void CMapView::OnUpdateMapeditAddline(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool()==CUSTOM_DRAW_LINE_TOOL);
	
}

void CMapView::OnMapeditAddpolygon() 
{
	m_ctrlMapX.SetCurrentTool(CUSTOM_DRAW_REGION_TOOL);
	
}

void CMapView::OnUpdateMapeditAddpolygon(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool()==CUSTOM_DRAW_REGION_TOOL);
	
	
}

	

void CMapView::OnMapeditAddrect() 
{
		
	m_ctrlMapX.SetCurrentTool(CUSTOM_DRAW_RECT_TOOL);
}

void CMapView::OnUpdateMapeditAddrect(CCmdUI* pCmdUI) 
{
	
	pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool()==CUSTOM_DRAW_RECT_TOOL);	
}

void CMapView::OnMapeditAddepplise() 
{
	m_ctrlMapX.SetCurrentTool(CUSTOM_DRAW_ELLIPSE_TOOL);
	
}

void CMapView::OnUpdateMapeditAddepplise(CCmdUI* pCmdUI) 
{

	pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool()==CUSTOM_DRAW_ELLIPSE_TOOL);
}

void CMapView::OnMapeditAddarc() 
{
	
	m_ctrlMapX.SetCurrentTool(CUSTOM_DRAW_ARC_TOOL);
	
}

void CMapView::OnUpdateMapeditAddarc(CCmdUI* pCmdUI) 
{
		pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool()==CUSTOM_DRAW_ARC_TOOL);
	
}



void CMapView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	CMainFrame* pFrame=((CMainFrame*)AfxGetMainWnd());
	pFrame->m_pMapView=this;	
}

void CMapView::OnDestroy() 
{

	ClearAllTarget();

⌨️ 快捷键说明

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