TOCControl右键菜单

来源:互联网 发布:mac chrome加载flash 编辑:程序博客网 时间:2024/06/05 06:27

两种方式:(一种是原模式,一种是装饰模式)

1、原模式

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


            m_tocControl = (ITOCControl2)axTOCControl1.Object;
            m_mapControl = (IMapControl3)axMapControl1.Object;

            //Add custom commands to the map menu
            m_menuMap = new ToolbarMenuClass();
            m_menuMap.AddItem(new LayerVisibility(), 1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);//添加
            m_menuMap.AddItem(new LayerVisibility(), 2, 1, false, esriCommandStyles.esriCommandStyleTextOnly);
            //Add pre-defined menu to the map menu as a sub menu 
            m_menuMap.AddSubMenu("esriControls.ControlsFeatureSelectionMenu", 2, true);
            //Add custom commands to the map menu
            m_menuLayer = new ToolbarMenuClass();
            m_menuLayer.AddItem(new  RemoveLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ScaleThresholds(), 1, 1, true, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ScaleThresholds(), 2, 2, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ScaleThresholds(), 3, 3, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new LayerSelectable(), 1, 4, true, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new LayerSelectable(), 2, 5, false, esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ZoomToLayer(), -1, 6, true, esriCommandStyles.esriCommandStyleTextOnly);


            //Set the hook of each menu
            m_menuLayer.SetHook(m_mapControl);
            m_menuMap.SetHook(m_mapControl);

 private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button != 2) return;

            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;//选择item的类型,有Map,Layer,Legend
            IBasicMap map = null; ILayer layer = null;
            object other = null; object index = null;

            //Determine what kind of item is selected
            m_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);//设置SelectItem,通过GetSelectItem获取。
            else
                m_tocControl.SelectItem(layer, null);
            
            //Set the layer into the CustomProperty (this is used by the custom layer commands)
            m_mapControl.CustomProperty = layer;//设置CustomProperty属性,这个属性在ICommand接口的Hook中获取,进行对该Layer操作。

            //Popup the correct context menu
            if (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);
        }

2、装饰模式

在项目中添加新项【Arcgis】-【Context Menu(AE)】,用此模板完成。

EngineContextMenu1 menucontext = new EngineContextMenu1();
            menucontext.SetHook(axMapControl1.Object);

menucontext.PopupMenu(e.x, e.y, m_tocControl.hWnd);

EngineContextMenu1 类有SetHook(),PopupMenu()和IToolbarMenu2 ContextMenu属性。



0 0