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

article/2025/10/15 15:07:53

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右键菜单功能效果展示

点击删除图层:
删除图层后效果:
点击缩放至图层:
缩放至图层效果:
点击显示所有图层:
显示所有图层效果:
点击关闭所有图层:
关闭所有图层效果:

http://chatgpt.dhexx.cn/article/rvL82fp0.shtml

相关文章

arcgis python实例_arcgis二次开发_arcgis二次开发python_arcgis二次开发实例

[1.rar] - QQ连连看的源码.单消秒杀挂机等功能喜欢的朋友请拿去研究 [qqCHAR.rar] - qq 验证码识别程序 可以叫准确的识别出qq登陆前的验证码 [1.rar] - 本书以Visualc作为开发语言&#xff0c;结合大量实例&#xff0c;详细介绍了利用Arcobjects组件进行GIS二次开发的方法和…

【ArcGIS二次开发】Engine界面搭建

1、新建窗体项目Windows Appplication(Engine) 2、添加menuStrip、statusStrip和ToolbarControl控件&#xff0c;并设置相应的Dock属性为Top和Right 3、用SplitContainer控件把显示区域分成三部分&#xff0c;并设置splitContatiner1的Orientation属性为Horizontal 4、添加TabC…

ArcGIS二次开发基础教程(02):地图导航和鹰眼

ArcGIS二次开发基础教程(02)&#xff1a;地图导航和鹰眼 地图导航&#xff08;主要是调用命令和工具&#xff09; 地图的放缩和漫游 if(axMapControl1.CurrentTool null) {ICommand icc;//地图放大ITool tool new ControlsMapZoomInToolClass();//地图缩小//ITool tool n…

arcgis java 二次开发_arcgis二次开发_cad二次开发_java arcgis二次开发

属性查询是GIS应用不可缺少的重要功能&#xff0c;尤其是在各种业务系统中&#xff0c;根据用户输入相应的查询条件&#xff0c;从属性要素中快速定位到用户感兴趣的要素&#xff0c;为业务应用提供了便利。本文就来聊一聊QGis二次开发中如何实现属性查询功能。 其实这个功能我…

AE+ArcGIS二次开发课程设计(基于C#)

AEArcGIS二次开发课程设计&#xff08;基于C#&#xff09; 1.工作内容2.程序功能介绍3.功能模块介绍3.1 实现【创建TIN】说明3.1.1 功能说明3.1.2 代码实现&#xff08;包含了所有主要的代码&#xff0c;库引用自行导入&#xff09; 3.2 实现【TIN坡度坡向分析】说明3.2.1 功能…

ArcGIS二次开发前言

ArcGIS二次开发前言 前言环境常见bug解决方案 前言 自毕业成为GIS开发工程师已有一年多的时间&#xff0c;时间很短&#xff0c;短到不过人一生中工作时限的3.75%&#xff0c;时间很长&#xff0c;长到收藏夹已经从零攒到了一千四百多条记录&#xff0c;OneNote上也记录了几十…

ArcGIS Engine二次开发

目录 1 前言 2 准备工作 2.1 License的加入 2.2 ToolStrip控件 2.3 MenuStrip控件 2.4 帮助文档的查看 3 数据加载 3.1 矢量数据的加载 3.2 栅格数据的加载 4 地图浏览功能 1 前言 这是一份关于ArcGIS Engine二次开发的一份报告总结&#xff0c;在这份报告中包含了简单的…

ArcGIS二次开发知识点总结

空间分析定义&#xff1a;空间分析是指分析具有空间坐标或相对位置的数据和过程的理论和方法&#xff0c;是对地理空间现象的定量研究&#xff0c;其目的在于提取并传输空间数据中隐含的空间信息。 叠置分析定义&#xff1a;是指将同一坐标系统下不同信息表达的两组或多组专题…

【ArcGIS Pro二次开发】(31):ArcGIS Pro中的多线程

ArcGIS Pro与旧的ArcGIS桌面应用程序的显著不同之处在于&#xff0c;它采用多线程架构&#xff0c;可以有效的发挥多核CPU的优势。这使得二次开发工具的性能变得更好&#xff0c;但也对开发工作带来了更多的难点和挑战。 一、多线程需要注意的问题 一般情况下&#xff0c;为了…

GIS二次开发:实验一 ArcGIS Engine 开发初步

实验一 ArcGIS Engine 开发初步 一、实验目的 掌握ArcGIS Engine的安装&#xff1b;熟悉ArcGIS Engine中几个常用的控件&#xff1b;搭建第一个简单的ArcGIS Engine 程序&#xff1b;通过ICommand接口添加地图浏览工具。 二、实验仪器与设备 计算机、visual studio 软件、A…

Arcgis二次开发软件安装(Arcgis10.2、VS2012、AE10.2)

目录 一、序言 二、Arcgis10.2安装 &#xff08;一&#xff09;安装ArcGIS License Manager 1.1 ArcGIS License Manager安装 1.2 ArcGIS License Manager配置 &#xff08;二&#xff09;安装ArcGIS Desktop 1.1ArcGIS Desktop安装 1.2.ArcGIS文件替换 1.3中文显示与…

StackPanel 实现从上往下+从右往左 排列+RenderTransform特效实例分析

StackPanel:将子元素排列到可沿水平或垂直放置的行。 参考资料&#xff1a; 1. StackPanel类 2. Silverlight学习笔记&#xff08;九&#xff09;——RenderTransform特效【五种基本变换】及【矩阵变换MatrixTransform】 3. MatrixTransform矩阵变换 stack表明StackPane…

控件篇 - 子控件在StackPanel里的居中问题

如下面代码&#xff1a; <StackPanel Width"200" Height"80" Background"Tomato"><TextBlock HorizontalAlignment"Center" VerticalAlignment"Center" Text"ABCD"/></StackPanel> 原意是想通…

Docker Stack

大规模场景下的多服务部署和管理是一件很难的事情。Docker Stack 为解决该问题而生&#xff0c;Docker Stack 通过提供期望状态、滚动升级、简单易用、扩缩容、健康检查等特性简化了应用的管理&#xff0c;这些功能都封装在一个完美的声明式模型当中。 Stack 能够在单个声明文…

StackPanel布局

StackPanel可以把内部元素在纵向或横向上紧凑排列&#xff0c;形成栈式布局。 示例代码&#xff1a; <Grid><GroupBox Header"请选择没有错别字的成语" BorderBrush"Black" Margin"5"><StackPanel Margin"5" Heig…

2.12 Stack

2.12 Stack Stack也是List接口的实现类之一&#xff0c;和Vector一样&#xff0c;因为性能原因&#xff0c;更主要在开发过程中很少用到栈这种数据结构&#xff0c;不过栈在计算机底层是一种非常重要的数据结构&#xff0c;下边将探讨下Java中Stack。 2.12.1 Stack的继承关系 …

wpf之StackPanel布局

注意两个参数&#xff1a; Orientation &#xff1a;控制排列是水平方向&#xff0c;还是垂直方向&#xff08;Horizontal 水平方向 Vertical垂直方向&#xff09; FlowDirection&#xff1a;控制控件排序是从右往左还是从左往右&#xff0c;写两个简单的demo如下&#xff…

四、StackPanel控件

StackPanel可以把内部控件在纵向或横向上紧凑排列、形成栈式布局&#xff0c;当上层控件不被删除时&#xff0c;剩余的控件会前移&#xff0c;填充空白 特点&#xff1a; 同类控件紧凑排列移除其中…

WPF 控制StackPanel用法

StackPanel是非常相似的WrapPanel&#xff0c;但至少有一个重要的区别&#xff1a;StackPanel的不换行的内容。相反&#xff0c;它将内容向一个方向拉伸&#xff0c;允许您将一项一项一项地堆叠在一起。让我们首先尝试一个非常简单的示例&#xff0c;就像我们对 WrapPanel 所做…

2021-08-16 WPF控件专题 StackPanel 控件详解

1.StackPanel 控件介绍 堆栈面板 —布局控件 —Panel 子元素排列成一行或一列 水平 垂直 FlowDirection 子元素的流动方向 Orientation&#xff1a;Horizontal 一行 一般设置VerticalAlignment&#xff1a;Top Bottom Center Stretch Vertical 一列 HorizontalAlignment Left…