首先新建一个c#空窗体程序,尤其注意引用要添加。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
namespace AO
{
public partial class MainForm : Form
{
private IEnvelope m_pEnvelope = null;
private GISTool m_curTool = GISTool.Default;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
axTOCControl1.SetBuddyControl(mainMapControl);
axTOCControl1.EnableLayerDragDrop = true;
}
private void tbnAddData_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.DefaultExt = "shp";
openFileDialog.Filter = "shp 文件|*.shp";
if (DialogResult.OK != openFileDialog.ShowDialog()) return;
// 得到Shapefile文件所在的目录.
string strDir = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
// 得到Shapefile文件的名字,.shp后缀.
string strShpFile = System.IO.Path.GetFileNameWithoutExtension(openFileDialog.FileName);
IWorkspaceFactory pWSF = new ShapefileWorkspaceFactory();
IPropertySet pPropertySet = new PropertySetClass();
pPropertySet.SetProperty("DATABASE", strDir);
try
{
IWorkspace pWorkspace = pWSF.Open(pPropertySet, 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShpFile);
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureClass;
pFeatureLayer.Name = pFeatureClass.AliasName;
mainMapControl.AddLayer(pFeatureLayer as ILayer);
overviewMapControl.AddLayer(pFeatureLayer as ILayer);
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void mnuAddData_Click(object sender, EventArgs e)
{
tbnAddData_Click(sender, e);
}
private void mainMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
overviewMapControl.ActiveView.Refresh();
m_pEnvelope = e.newEnvelope as IEnvelope;
}
private void mainMapControl_OnAfterDraw(object sender, IMapControlEvents2_OnAfterDrawEvent e)
{
// 绘制红色鹰眼框
object pSym = null;
SimpleLineSymbol pOutlineSym;
IRgbColor pColor;
pColor = new RgbColorClass();
pColor.Red = 255;
pColor.Blue = 0;
pColor.Green = 0;
pSym = new SimpleFillSymbolClass();
ISimpleFillSymbol pSym1 = pSym as ISimpleFillSymbol;
pSym1.Style = esriSimpleFillStyle.esriSFSHollow;
pOutlineSym = new SimpleLineSymbolClass();
pOutlineSym.Width = 1;
pOutlineSym.Style = esriSimpleLineStyle.esriSLSSolid;
pOutlineSym.Color = pColor;
pSym1.Outline = pOutlineSym;
if (m_pEnvelope == null) return;
overviewMapControl.DrawShape(m_pEnvelope as IGeometry, ref pSym);
}
private void overviewMapControl_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
ESRI.ArcGIS.Geometry.Point pPoint = new ESRI.ArcGIS.Geometry.PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
mainMapControl.CenterAt(pPoint);
}
private void mainMapControl_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
coordinateLabel.Text = "坐标 X = " + e.mapX.ToString("F6") + " Y = " + e.mapY.ToString("F6");
}
private void tbnDefault_Click(object sender, EventArgs e)
{
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
m_curTool = GISTool.Default;
}
private void tbnPan_Click(object sender, EventArgs e)
{
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerPan;
m_curTool = GISTool.Pan;
}
private void tbnZoomIn_Click(object sender, EventArgs e)
{
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerZoomIn;
m_curTool = GISTool.ZoomIn;
}
private void tbnZoomOut_Click(object sender, EventArgs e)
{
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerZoomOut;
m_curTool = GISTool.ZoomOut;
}
private void tbnFixedZoomIn_Click(object sender, EventArgs e)
{
IEnvelope pEnvelope = mainMapControl.ActiveView.Extent;
pEnvelope.Expand(0.5, 0.5, true);
mainMapControl.ActiveView.Extent = pEnvelope;
mainMapControl.ActiveView.Refresh();
}
private void tbnFixedZoomOut_Click(object sender, EventArgs e)
{
IEnvelope pEnvelope = mainMapControl.ActiveView.Extent;
pEnvelope.Expand(1.5, 1.5, true);
mainMapControl.ActiveView.Extent = pEnvelope;
mainMapControl.ActiveView.Refresh();
}
private void tbnFullExtent_Click(object sender, EventArgs e)
{
IEnvelope pEnvelope = mainMapControl.ActiveView.FullExtent;
mainMapControl.ActiveView.Extent = pEnvelope;
mainMapControl.ActiveView.Refresh();
}
private void mainMapControl_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IEnvelope pEnvelope = null;
switch (m_curTool)
{
case GISTool.Default:
break;
case GISTool.ZoomIn:
pEnvelope = mainMapControl.TrackRectangle();
mainMapControl.ActiveView.Extent = pEnvelope;
mainMapControl.ActiveView.Refresh();
break;
case GISTool.ZoomOut:
pEnvelope = mainMapControl.TrackRectangle();
IMap pMap = mainMapControl.Map;
IActiveView pActiveView = pMap as IActiveView;
double newWidth = pActiveView.Extent.Width * (pActiveView.Extent.Width / pEnvelope.Width);
double newHeight = pActiveView.Extent.Height * (pActiveView.Extent.Height / pEnvelope.Height);
IEnvelope pEnvelope1 = new EnvelopeClass();
pEnvelope1.PutCoords(pActiveView.Extent.XMin - ((pEnvelope.XMin - pActiveView.Extent.XMin) * (pActiveView.Extent.Width / pEnvelope.Width)),
pActiveView.Extent.YMin - ((pEnvelope.YMin - pActiveView.Extent.YMin) * (pActiveView.Extent.Height / pEnvelope.Height)),
(pActiveView.Extent.XMin - ((pEnvelope.XMin - pActiveView.Extent.XMin) * (pActiveView.Extent.Width / pEnvelope.Width))) + newWidth,
(pActiveView.Extent.YMin - ((pEnvelope.YMin - pActiveView.Extent.YMin) * (pActiveView.Extent.Height / pEnvelope.Height))) + newHeight);
pActiveView.Extent = pEnvelope1;
pActiveView.Refresh();
break;
case GISTool.Pan:
mainMapControl.Pan();
break;
}
}
}
}