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

📄 aotest1view.cpp

📁 编译需要安装desktop或者Arcgis Engine.利用arcgis的ArcObjects(AO),实现加载地图
💻 CPP
字号:
// AoTest1View.cpp : implementation of the CAoTest1View class
//

#include "stdafx.h"
#include "AoTest1.h"

#include "AoTest1Doc.h"
#include "AoTest1View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*///////////////////////////////////////////////
首先打开上一节所建项目。选择菜单Tools->Options->Directories页在Include files里添加两个路径(如果你的ArcGIS的安装目录在其它盘,需做相应改动):
C:\PROGRAM FILES\ARCGIS\COM
C:\PROGRAM FILES\ARCGIS\BIN

编译需要安装desktop或者Arcgis Engine.利用arcgis的ArcObjects(AO),实现加载地图,具有放大 缩小 平移 显示全图等漫游功能。
//////////////////////////////*/

/////////////////////////////////////////////////////////////////////////////
// CAoTest1View

IMPLEMENT_DYNCREATE(CAoTest1View, CFormView)

BEGIN_MESSAGE_MAP(CAoTest1View, CFormView)
	//{{AFX_MSG_MAP(CAoTest1View)
	ON_WM_SIZE()
	ON_COMMAND(ID_MAP_ARROW, OnMapArrow)
	ON_COMMAND(ID_ZOOMIN, OnZoomin)
	ON_COMMAND(ID_MAP_ZOOMOUT, OnMapZoomout)
	ON_COMMAND(ID_MAP_PAN, OnMapPan)
	ON_COMMAND(ID_MAP_FULLEXTENT, OnMapFullextent)
	ON_UPDATE_COMMAND_UI(ID_MAP_ARROW, OnUpdateMapArrow)
	ON_UPDATE_COMMAND_UI(ID_MAP_PAN, OnUpdateMapPan)
	ON_UPDATE_COMMAND_UI(ID_MAP_ZOOMOUT, OnUpdateMapZoomout)
	ON_UPDATE_COMMAND_UI(ID_ZOOMIN, OnUpdateZoomin)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAoTest1View construction/destruction

CAoTest1View::CAoTest1View()
	: CFormView(CAoTest1View::IDD)
{
	//{{AFX_DATA_INIT(CAoTest1View)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CAoTest1View::~CAoTest1View()
{
}

void CAoTest1View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAoTest1View)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CAoTest1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CAoTest1View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	// 获取MapControl指针
	m_ipMapControl =  GetDlgItem(IDC_MAPCONTROL1)->GetControlUnknown();

}

/////////////////////////////////////////////////////////////////////////////
// CAoTest1View printing

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

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

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

void CAoTest1View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CAoTest1View diagnostics

#ifdef _DEBUG
void CAoTest1View::AssertValid() const
{
	CFormView::AssertValid();
}

void CAoTest1View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CAoTest1View message handlers

void CAoTest1View::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	// View窗口改变大小时改变MapControl大小
	if(m_ipMapControl!=NULL)
	{
		CRect rc;
		GetClientRect(rc);
		GetDlgItem(IDC_MAPCONTROL1)->MoveWindow(rc);
	}
 
}
// 选择状态
void CAoTest1View::OnMapArrow() 
{
	// TODO: Add your command handler code here
	m_ARTool = esriARToolNoneSelected;
	m_ipMapControl->put_MousePointer(esriPointerDefault);
}
//地图放大
void CAoTest1View::OnZoomin() 
{
	// TODO: Add your command handler code here
	m_ARTool = esriARToolMapZoomIn;
	m_ipMapControl->put_MousePointer(esriPointerZoomIn);

}
//地图缩小
void CAoTest1View::OnMapZoomout() 
{
	// TODO: Add your command handler code here
	m_ARTool = esriARToolMapZoomOut;
	m_ipMapControl->put_MousePointer(esriPointerZoomOut);
}
//地图平移
void CAoTest1View::OnMapPan() 
{
	// TODO: Add your command handler code here
	m_ARTool = esriARToolMapPan;
	m_ipMapControl->put_MousePointer(esriPointerPan);
}
//显示全图
void CAoTest1View::OnMapFullextent() 
{
	// TODO: Add your command handler code here
	 IActiveViewPtr ipActiveView;
	 HRESULT hr=m_ipMapControl->get_ActiveView(&ipActiveView);

	 if (FAILED(hr)) return;
	 IEnvelopePtr ipEnvelope;
	 hr =ipActiveView->get_FullExtent(&ipEnvelope);
	 if(FAILED(hr)) return;
	 ipActiveView->put_Extent (ipEnvelope);
	 ipActiveView->Refresh ();
}

BEGIN_EVENTSINK_MAP(CAoTest1View, CFormView)
    //{{AFX_EVENTSINK_MAP(CAoTest1View)
	ON_EVENT(CAoTest1View, IDC_MAPCONTROL1, 1 /* OnMouseDown */, OnOnMouseDownMapcontrol1, VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_R8 VTS_R8)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CAoTest1View::OnOnMouseDownMapcontrol1(long button, long shift, long x, long y, double mapX, double mapY) 
{
	// TODO: Add your control notification handler code here
	if(1 == button) // 鼠标左键按下
	{
		IActiveViewPtr ipActiveView;
		HRESULT hr=m_ipMapControl->get_ActiveView(&ipActiveView);
		if (ipActiveView == NULL) return ;
		switch(m_ARTool)
		{
		case esriARToolNoneSelected:
			{
				break;
			}
		case esriARToolMapZoomIn: // 地图放大
			{				
				IEnvelopePtr ipEnvelope;				
				hr = ipActiveView->get_Extent(&ipEnvelope);				
				if (FAILED(hr)) return ;				
				hr = m_ipMapControl->TrackRectangle(&ipEnvelope);				
				if (FAILED(hr)) return ;				
				if(ipEnvelope == NULL) return ;				
				double XMin,YMin,XMax,YMax;				
				XMin=YMin=XMax=YMax=0.0;				
				if(S_OK !=ipEnvelope->get_XMin(&XMin)) XMin = 0.0;				
				if(S_OK !=ipEnvelope->get_YMin(&YMin)) YMin = 0.0;				
				if(S_OK !=ipEnvelope->get_XMax(&XMax)) XMax = 0.0;				
				if(S_OK !=ipEnvelope->get_YMax(&YMax)) YMax = 0.0;				
				if(XMin!=XMax&&YMin!=YMax)           // 用户拉框选择区域放大				
				{					
					ipActiveView->put_Extent(ipEnvelope);					
					ipActiveView->Refresh();					
				}				
				else       // 用户点击进行放大					
				{					
					ipActiveView->get_Extent(&ipEnvelope);					
					if (FAILED(hr)) return ;					
					IPointPtr ipPoint(CLSID_Point);					
					ipPoint->put_X(mapX);					
					ipPoint->put_Y(mapY);					
					// 设置中心点为用户点击为主					
					ipEnvelope->CenterAt(ipPoint);					
					ipEnvelope->Expand(0.5, 0.5, true);					
					ipActiveView->put_Extent(ipEnvelope);					
					ipActiveView->Refresh();					
				}				
			}			
			break;			
		case esriARToolMapZoomOut: // 地图缩小			
			{				
				IEnvelopePtr ipEnvelope;				
				hr = ipActiveView->get_Extent(&ipEnvelope);				
				if (FAILED(hr)) return ;				
				IPointPtr ipPoint(CLSID_Point);				
				ipPoint->put_X(mapX);				
				ipPoint->put_Y(mapY);
				ipEnvelope->CenterAt(ipPoint);				
				// 设置中心点为用户点击为主				
				ipEnvelope->Expand(2, 2, true);				
				ipActiveView->put_Extent(ipEnvelope);				
				ipActiveView->Refresh();				
			}			
			break;			
		case esriARToolMapPan: // 地图平移			
			{				
				m_ipMapControl->Pan();				
			}			
			break;			
		default:			
			break;			
               }
			   
     }   
	 
}

// 更新工具栏

void CAoTest1View::OnUpdateMapArrow(CCmdUI* pCmdUI) 
{	
	pCmdUI->SetCheck(m_ARTool == esriARToolNoneSelected);
} 
void CAoTest1View::OnUpdateMapPan(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_ARTool == esriARToolMapPan);	
} 
void CAoTest1View::OnUpdateZoomin(CCmdUI* pCmdUI) 
{	
	pCmdUI->SetCheck(m_ARTool == esriARToolMapZoomIn);	
} 
void CAoTest1View::OnUpdateMapZoomout(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_ARTool == esriARToolMapZoomOut);	
}

⌨️ 快捷键说明

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