Skip to content

环境部署

发表于
更新于
阅读量

1.新建c#项目(windows窗体应用程序)

2.在form中拖拽地图控件(MapControl)、目录树控件(TOCControl)、工具栏控件(ToolbarControl)

3.双击form1.cs,在Form1()中添加如下绑定代码

4.对TOCControl、ToolbarControl控件分别右击属性进行地图控件的绑定

5.点击启动或者F5,即可运行

便捷式编写

发表于
更新于
阅读量

右击ToolbarControl控件,依次选择属性-->items-->Add...,在菜单中双击添加相应的功能即可

二次开发

发表于
更新于
阅读量

在容器中添加相应的控件,双击在对应的空间出编写代码即可

在开头处引入ESRI.ARCGIS引用

c
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.Output;

01 接口、类与对象及三者之间的关系

接口是可被任何类继承和实现的属性和行为的集合,包含属性、方法、索引器和事件,但是不能定义字段

类是用来继承和实现接口,将类实例化为对象后,即可使用接口定义的方法和属性

02 打开文件功能

c
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
  ICommand cmd = new ControlsOpenDocCommandClass();//ICommand接口申明Controls open类
  cmd.OnCreate(axMapControl1.Object);//创建对象
  cmd.OnClick();
}

03 放大功能

c
private void 缩放功能ToolStripMenuItem_Click(object sender, EventArgs e)
{
  ICommand cmd = new ControlsMapZoomInToolClass(); //ICommand接口申明Controls zoom工具
  cmd.OnCreate(axMapControl1.Object);//创建对象
  axMapControl1.CurrentTool = cmd as ITool;//将cmd的工具作为map窗口的功能
}

04 目录树右击->缩放至图层

c
private IFeatureLayer pLayer = null;//首先创建要素图层
private void 缩放至图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
  if(pLayer != null)//当所选图层不为空时
  {
     (axMapControl1.Map as IActiveView).Extent = pLayer.AreaOfInterest;//首先规定视图的大小为图层的范围大小
     (axMapControl1.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);//在视图上刷新当前图层
  }
}

private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
   if (e.button == 2)//如果鼠标是右键
   {
      esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
      IBasicMap bmap = null;
      ILayer layer = null;
      object unk = null;
      object data = null;
      axTOCControl1.HitTest(e.x, e.y, ref item, ref bmap, ref layer, ref unk, ref data);//视图获取到当前的图层
      pLayer = layer as IFeatureLayer;//将获取到的图层赋值给新的图层
      if(item == esriTOCControlItem.esriTOCControlItemLayer && pLayer != null)
      {
         contextMenuStrip1.Show(Control.MousePosition);//在鼠标点击位置出现设置的右键菜单
      }
   }
}

Released under the MIT License.