TOCControl右键菜单

1.类中定义菜单变量

  private IToolbarMenu mapMenu;//toc控件右键地图菜单
  private IToolbarMenu layerMenu;//toc控件右键图层菜单

  并在菜单中初始化

    private void MainForm_Load(object sender, EventArgs e)
        {
            //get the MapControl
            m_mapControl = (IMapControl3)axMapControl1.Object;
            tocControl = axTOCControl1.Object as ITOCControl2;
            //disable the Save menu (since there is no document yet)
            //menuSaveDoc.Enabled = false;

            //构造地图右键菜单
            mapMenu = new ToolbarMenuClass();
            mapMenu.AddItem(new LayerVisibility(), 1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
            mapMenu.AddItem(new LayerVisibility(), 2, 1, false, esriCommandStyles.esriCommandStyleIconAndText);

            //构造图层右键菜单
            layerMenu = new ToolbarMenuClass();
            layerMenu.AddItem(new LayerVisibility(), 1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
            layerMenu.AddItem(new LayerVisibility(), 2, 1, true, esriCommandStyles.esriCommandStyleTextOnly);

            //右键菜单绑定
            mapMenu.SetHook(m_mapControl);
            layerMenu.SetHook(m_mapControl);
        }

2.自定义LayerVisibility类

    public sealed class LayerVisibility : BaseCommand, ICommandSubType
    {
        private IHookHelper hookHelper;
        private long subType;

        public LayerVisibility()
        {

        }

        public override void OnCreate(object hook)
        {
            hookHelper = new HookHelperClass();
            hookHelper.Hook = hook;
        }

        public override void OnClick()
        {
            for (int i = 0; i <= hookHelper.FocusMap.LayerCount - 1; i++)
            {
                if (((hookHelper.FocusMap.get_Layer(i) as IFeatureLayer) as IFeatureSelection) != null)
                {
                    string t = hookHelper.FocusMap.get_Layer(i).Name;
                    //((hookHelper.FocusMap.get_Layer(i) as IFeatureLayer) as IFeatureSelection).Clear();
                }
                if (subType == 1) hookHelper.FocusMap.get_Layer(i).Visible = true;
                if (subType == 2) hookHelper.FocusMap.get_Layer(i).Visible = false;
            }
            hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            hookHelper.ActiveView.Refresh();
        }

        public override string Caption
        {
            get
            {
                if (subType == 1) return "显示所有图层";
                else return "隐藏所有图层";
            }
        }

        public override bool Enabled
        {
            get
            {
                bool enabled = false; int i;
                if (subType == 1)
                {
                    for (i = 0; i <= hookHelper.FocusMap.LayerCount - 1; i++)
                    {
                        if (hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == false)
                        {
                            enabled = true;
                            break;
                        }
                    }
                }
                else
                {
                    for (i = 0; i <= hookHelper.FocusMap.LayerCount - 1; i++)
                    {
                        if (hookHelper.ActiveView.FocusMap.get_Layer(i).Visible == true)
                        {
                            enabled = true;
                            break;
                        }
                    }
                }
                return enabled;
            }
        }

        #region ICommandSubType 成员

        public int GetCount()
        {
            return 2;
        }

        public void SetSubType(int SubType)
        {
            subType = SubType;
        }

        #endregion
    }

3.TOCControl中单击事件

               esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
                IBasicMap map = null;
                ILayer layer = null;
                object other = null;
                object index = null;
                tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
                if (e.button == 2)//右键
                {
                    m_mapControl.CustomProperty = layer;

                    if (item == esriTOCControlItem.esriTOCControlItemMap)//点击的是地图
                    {
                        tocControl.SelectItem(map, null);
                        mapMenu.PopupMenu(e.x, e.y, tocControl.hWnd);
                    }
                  
                    if (item == esriTOCControlItem.esriTOCControlItemLayer)//点击的是图层
                    {
                        tocControl.SelectItem(layer, null);
                        //setSecAndEdit(layer.Name);
                        layerMenu.PopupMenu(e.x, e.y, tocControl.hWnd);
                    }
                }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值