首页 > 代码库 > TOCControl右键菜单

TOCControl右键菜单

       新接触TOCControl空间的自定义菜单,其功能强大令人叹为观止。可用AddItem添加自定义Command、menu或者esriControls下的Command、menu;了解使用esriControls下的Command、menu,可以为开发者省下大量的时间。举例说明如下:自定义了一个添加图层的命令(在其中特别注意CustomProperty这个属性,它可以灵活附加传递多种数据)。

   class AddLayer : BaseCommand
    {
        private IMapControl3 m_mapControl;

        public AddLayer()
        {
            base.m_caption = "添加地图文档";
        }

        public override void OnClick()
        {
            ILayer layer = (ILayer)m_mapControl.CustomProperty;
            m_mapControl.Map.DeleteLayer(layer);
            OpenFileDialog openFileDialog1 = (OpenFileDialog)m_mapControl.CustomProperty;
            openFileDialog1.Title = "打开地图文档";
            openFileDialog1.Filter = "map documents(*.mxd)|*.mxd";
            openFileDialog1.ShowDialog();
            string filePath = openFileDialog1.FileName;
            if (m_mapControl.CheckMxFile(filePath))
            {
                m_mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                m_mapControl.LoadMxFile(filePath);
                m_mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
            }
            else
            {
                MessageBox.Show("地图文档无效!");
            }
        }

        public override void OnCreate(object hook)
        {
            m_mapControl = (IMapControl3)hook;
        }
    }

   toolbarMenu.AddItem(new AddLayer(), -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);

   但其实足够了解esriControls下的现成命令的话,上述代码可以浓缩为:

     toolbarMenu.AddItem("esriControls.ControlsOpenDocCommand", -1, 0, false,esriCommandStyles.esriCommandStyleIconOnly);

   就这样。

TOCControl右键菜单