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右键菜单功能效果展示
点击删除图层:
删除图层后效果:
点击缩放至图层:
缩放至图层效果:
点击显示所有图层:
显示所有图层效果:
点击关闭所有图层:
关闭所有图层效果:







