1、添加现有项
①右击解决方案中的项目,添加TOCControlContextMenu中的LayerSelectable、LayerVisibility、RemoveLayer、ZoomToLayer

②点击菜单栏中的项目,添加引用ESRI.ArcGIS.ADF.Local

③修改RemoveLayer中的命名空间为项目名称EngineMapTest;修改Base.m_caption为“删除图层”

namespace EngineMapTest
{public sealed class RemoveLayer : BaseCommand  {private IMapControl3 m_mapControl;public RemoveLayer(){base.m_caption = "删除图层";}public override void OnClick(){ILayer layer =  (ILayer) m_mapControl.CustomProperty;m_mapControl.Map.DeleteLayer(layer);}public override void OnCreate(object hook){m_mapControl = (IMapControl3) hook;}}
}

③修改ZoomToLayer中的命名空间为项目名称EngineMapTest;修改Base.m_caption为“放大至图层”

namespace EngineMapTest
{public sealed class ZoomToLayer : BaseCommand  {private IMapControl3 m_mapControl;public ZoomToLayer(){base.m_caption = "缩放至图层";}public override void OnClick(){ILayer layer = (ILayer) m_mapControl.CustomProperty;m_mapControl.Extent = layer.AreaOfInterest;}public override void OnCreate(object hook){m_mapControl = (IMapControl3) hook;}}
}

④修改LayerVisibility中的命名空间为项目名称EngineMapTest;修改caption内容

namespace EngineMapTest
{public sealed class LayerVisibility : BaseCommand, ICommandSubType {private IHookHelper m_hookHelper = new HookHelperClass();private long m_subType;public LayerVisibility(){}public override void OnClick(){for (int i=0; i <= m_hookHelper.FocusMap.LayerCount - 1; i++){if (m_subType == 1) m_hookHelper.FocusMap.get_Layer(i).Visible = true;if (m_subType == 2) m_hookHelper.FocusMap.get_Layer(i).Visible = false;}m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null,null);}public override void OnCreate(object hook){m_hookHelper.Hook = hook;}public int GetCount(){return 2;}public void SetSubType(int SubType){m_subType = SubType;}public override string Caption{get{if (m_subType == 1) return "显示所有图层";else  return "关闭所有图层";}}public override bool Enabled{get{bool enabled = false; int i;if (m_subType == 1) {for (i=0;i<=m_hookHelper.FocusMap.LayerCount - 1;i++){if (m_hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == false){enabled = true;break;}}}else {for (i=0;i<=m_hookHelper.FocusMap.LayerCount - 1;i++){if (m_hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == true){enabled = true;break;}}}return enabled;}}}
}

⑤修改LayerSelectable中的命名空间为项目名称EngineMapTest;修改caption内容

namespace EngineMapTest
{public sealed class LayerSelectable : BaseCommand, ICommandSubType{private IMapControl3 m_mapControl;private long m_subType;public LayerSelectable(){}public override void OnClick(){IFeatureLayer layer = (IFeatureLayer) m_mapControl.CustomProperty;if (m_subType == 1)  layer.Selectable = true;if (m_subType == 2) layer.Selectable = false;}public override void OnCreate(object hook){m_mapControl = (IMapControl3) hook;}public override bool Enabled{get{ILayer layer = (ILayer) m_mapControl.CustomProperty;if (layer is IFeatureLayer){IFeatureLayer featureLayer = (IFeatureLayer) layer;if (m_subType == 1) return !featureLayer.Selectable;else return featureLayer.Selectable;}else{return false;}}}public int GetCount(){return 2;}public void SetSubType(int SubType){m_subType = SubType;}public override string Caption{get{if (m_subType == 1) return "图层可选";else  return "图层不可选";}}}
}

2、设置MainForm_Load功能
①定义声明变量

 private ITOCControl2 m_tocControl;private IMapControl3 m_mapControl;private IToolbarMenu m_menuMap;private IToolbarMenu m_menuLayer;

②添加MainForm_Load代码

private void MainForm_Load(object sender, EventArgs e){m_tocControl = (ITOCControl2)axTOCControl1.Object;m_mapControl = (IMapControl3)mainMapControl.Object;//Set buddy controlm_tocControl.SetBuddyControl(m_mapControl);axToolbarControl1.SetBuddyControl(m_mapControl);//Add pre-defined control commands to the ToolbarControlaxToolbarControl1.AddItem("esriControls.ControlsSelectFeaturesTool", -1, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly);axToolbarControl1.AddToolbarDef("esriControls.ControlsMapNavigationToolbar", 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly);axToolbarControl1.AddItem("esriControls.ControlsOpenDocCommand", -1, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly);//Add custom commands to the Layer menu, //TOCControl图层右键菜单m_menuLayer = new ToolbarMenuClass();m_menuLayer.AddItem(new RemoveLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.AddItem(new ZoomToLayer(), 0, -1, false, esriCommandStyles.esriCommandStyleTextOnly);m_menuLayer.SetHook(m_mapControl);//TOCControlMap右键菜单//Add custom commands to the map menum_menuMap = new ToolbarMenuClass();m_menuMap.AddItem(new LayerVisibility(), 1, 0, true, esriCommandStyles.esriCommandStyleIconAndText);m_menuMap.AddItem(new LayerVisibility(), 2, 1, false, esriCommandStyles.esriCommandStyleIconAndText);m_menuMap.AddItem(new ControlsAddDataCommandClass(), 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText);//Set the hook of each menum_menuMap.SetHook(m_mapControl);另存地图文档ToolStripMenuItem.Enabled = false; }

3、设置axTOCControl1_OnMouseDown功能

private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e){if (e.button != 2) return;esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;IBasicMap map = null; ILayer layer = null;object other = null; object index = null;//Determine what kind of item is selectedm_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);//Ensure the item gets selected if (item == esriTOCControlItem.esriTOCControlItemMap)m_tocControl.SelectItem(map, null);elsem_tocControl.SelectItem(layer, null);//Set the layer into the CustomProperty (this is used by the custom layer commands)            m_mapControl.CustomProperty = layer;//Popup the correct context menuif (item == esriTOCControlItem.esriTOCControlItemMap) m_menuMap.PopupMenu(e.x, e.y, m_tocControl.hWnd);if (item == esriTOCControlItem.esriTOCControlItemLayer) m_menuLayer.PopupMenu(e.x, e.y, m_tocControl.hWnd);}

4、TOCControl右键菜单功能效果展示

点击删除图层:

删除图层后效果:

点击缩放至图层:

缩放至图层效果:

点击显示所有图层:

显示所有图层效果:

点击关闭所有图层:

关闭所有图层效果:

【ArcGIS二次开发】TOCControl右键菜单功能实现相关推荐

  1. ArcGis二次开发ArcEngine开篇

    ArcGis二次开发ArcEngine开篇 以一款简单GIS软件截图片引入本篇内容 导读 万事开头难,如何利用ArcEngine开发一款GIS产品呢?一款简单的GIS软件基本布局如上图所示,共划分为六 ...

  2. ArcGIS二次开发基础教程(06):有关图层的基本操作

    ArcGIS二次开发基础教程(06):有关图层的基本操作 0. PageLayout和MapControl 的同步 void CopyToPage(){//对象拷贝,把mapcontrol的地图拷贝重 ...

  3. ArcGIS二次开发基础教程(00):基础界面设计

    ArcGIS二次开发基础教程(00) : 基础界面设计 (开发环境:VS2010+ArcEngine10.2+C# :鉴于学习ArcGIS二次开发的同学都有一定的WinForm开发和ArcGIS软件使 ...

  4. ArcGIS二次开发——地图居中显示

    ArcGIS二次开发--地图居中显示 一.创建 Engine 应用程序 1.启动 Visual Studio 2012,从"文件"->"新建"选中&quo ...

  5. arcgis二次开发python-ArcGIS 二次开发专题 序

    依据ArcGIS 组件式开发及应用的目录结构,将系统性的学习ArcGIS 二次开发的道路分为三个部分.这个系列包含以下三个部分: Part1 基础 1. 前言 1.1 组件式GIS 1.2 ArcOb ...

  6. ArcGIS二次开发入门 一

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 作者:朱 ...

  7. 基于ARCGIS二次开发可视化开发环境搭建(JAVA)

    这两天为了搭建这么一个基于java的ArcGIS二次开发环境可着实花了一番心血.在网上搜索各种资料,大部分都是基于C#的,关于JAVA的很少,而且很杂乱,没有一个完整的.详细的.适合新手的这么一个教程 ...

  8. ArcGIS二次开发基础教程(11):网络分析之最短路径分析

    ArcGIS二次开发基础教程(11):网络分析之最短路径分析 最短路径分析 这里直接调用了在mdb中建立好的网络数据集 //全局变量 private INetworkDataset my_networ ...

  9. Arcgis 二次开发指导 热点分析实现

    Arcgis 二次开发 热点分析 背景 具体操作 1.toolbox试验 2.查找接口 3.输入文件路径,特别注意! 4.找出错误原因 总结 背景 大三下上完了arcgis二次开发,真心觉得咱们gis ...

  10. ArcGIS二次开发基础教程(10):三维分析

    ArcGIS二次开发基础教程(10):三维分析 坡度分析 请务必学会使用帮助文档!!! //DEM数据的坡度分析 将分析结果添加到地图上 //首先获取DEM数据,方法有很多例如从个人地理数据库获取,也 ...

最新文章

  1. ssh tunnel 上网
  2. 云存储巧解三大存储难题
  3. 在adapter中startactivityforresult
  4. 系统部署常见问题汇总
  5. Pytorch实现U-net视网膜血管分割
  6. java bigdecimal赋值_Java中BigDecimal类介绍及用法(亲测)
  7. 无法查找网络工作组计算机,XP系统弹出“无法查看工作组计算机”提示怎么办?...
  8. sqllite开发安卓项目_【兼职项目】预算3万开发无线温度电流传感,2万开发直流电机打磨机控制...
  9. JavaScript执行环境 + 变量对象 + 作用域链 + 闭包
  10. 《Python地理数据处理》——导读
  11. 数据传输服务 DTS > 数据订阅 > 数据订阅(新版) > 创建RDS MySQL数据订阅通道(新版)
  12. python离散变量_python – 当涉及离散变量时,pymc3与pymc2的困难
  13. Akash Network主网现已部署Sushiswap应用
  14. 倍福---Modbus TCP Server和调试助手测试
  15. 520表白网页代码html 爱心网页制作
  16. 【教程】Spire.PDF教程:C# 添加、获取和删除 PDF 自定义文档属性
  17. C-V2X 网络层及适配层解析填充
  18. 文化财经SAR指标计算(一)
  19. 深夜给这个世界添加一点佐料
  20. 特斯拉Q4财报:底部反弹70%,为信仰打call

热门文章

  1. 软考 | 2016年下半年 软件设计师 下午试卷
  2. 朝阳过敏性鼻炎如何调理 首选祖传鼻炎药
  3. 2022年专精特新各地区补贴政策汇总
  4. 云服务器数据丢失_云上会发生数据丢失吗?
  5. WinXP优化设置~希望能对用XP的网管有帮助!
  6. android下usb框架系列文章---(2)Usb mass_storage turn on的过程
  7. 制作zmap的dns探针
  8. JSP+ssm计算机毕业设计青年旅舍租赁系统设计与实现u62wp【源码、数据库、LW、部署】
  9. 消除恼人的SD卡去不掉的写保护提示
  10. Vue组件间通信--消息订阅与发布