AE ICommand工具以及右键菜单ToolStripMenuItem用法

1、Icommand接口 位于systemUI命名空间下,封装常用的操作工具。例如(1)地图交互操作:放大、缩小、选择、查询等(2)直接操作工具,即不用与地图进行交互的工具:另存、全图、添加数据等。

对于第一种工具使用方法:

 private IMapControlDefault mapCtl;  //需声明全局变量         
 private void 缩小_Click(object sender, EventArgs e)
    {
        ICommand commandzoomout = new ControlsMapZoomOutToolClass();
        commandzoomout.OnCreate(axMapControl1.Object);
        mapCtl.CurrentTool = commandzoomout as ITool;
    } 

对于第二种工具使用方法:

    private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ICommand commandzoomsave = new ControlsSaveAsDocCommandClass();
        commandzoomsave.OnCreate(axMapControl1.Object);
        commandzoomsave.OnClick();
    } 
    OnCreate()方法中传递的hook参数(当前对象引用)成为绑定到这个控件的交互对象,通过OnClick()方法运行。

2、右键菜单ToolStripMenuItem
普通的控件设置右键菜单,只需在其属性中直接绑定就可以,但是对于AE的axTOCControl控件不适用。在添加了contextMenuStrip控件后,以 删除图层 和 移动图层 为例记录绑定方法。
(1)删除图层:

  private ILayer m_Layer = null;private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        axMapControl1.Map.DeleteLayer(m_Layer);
    }      
    private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
    {
        esriTOCControlItem Item = esriTOCControlItem.esriTOCControlItemNone;
        IBasicMap pBasicMap = null;
        ILayer pLayer = null;
        object other = null;
        object index = null;
        axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index); 图层赋值
        m_Layer = pLayer;
        if (e.button == 2 &&  Item == esriTOCControlItem.esriTOCControlItemLayer)
           contextMenuStrip1.Show(axTOCControl1,new Point(e.x,e.y));        
           }

( 2)移动图层(可以直接在属性中设置axTOCControl的EnableLayerDragDrop属性为true实现)
思路:当鼠标点击时即在OnMouseDown事件中,获取需要调整的图层,方法同上,当鼠标弹起时即OnMouseUp事件中,获得图层移到位置的图层索引号,使用IMap接口提供MoveLayer方法,将需要调整显示顺序的图层移动目标位置;使用TOCControl的Update()方法,更新TOCControl控件中的图层顺序。
声明全局变量:

    private ILayer m_Layer = null;
    esriTOCControlItem Item = esriTOCControlItem.esriTOCControlItemNone;
    IBasicMap pBasicMap = null;
    ILayer pLayer = null;
    object other = null;
    object index = null;
    private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
    {                      
        axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index);
        m_Layer = pLayer;           
    }       
     private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
    {
        int ToIndex=0;
        if (e.button == 1 && Item == esriTOCControlItem.esriTOCControlItemLayer)
        {
            esriTOCControlItem Itemnew = esriTOCControlItem.esriTOCControlItemNone;
            ILayer moveLayer = null;
            axTOCControl1.HitTest(e.x, e.y, ref Itemnew, ref pBasicMap, ref moveLayer, ref other, ref index)                
            for (int i = 0; i < axTOCControl1.ActiveView.FocusMap.LayerCount; i++)
            {
                if (moveLayer == axTOCControl1.ActiveView.FocusMap.get_Layer(i))
                {
                    ToIndex = i;
                    break;
                }
            }
        }
        axTOCControl1.ActiveView.FocusMap.MoveLayer(m_Layer, ToIndex);
        axTOCControl1.Update();       
         }

ITOCControl接口的HitTest()方法可以返回TOCControl中点击坐标点处的对象类型ItemType。 esriTOCControlItem 有如下几种:
在这里插入图片描述
另外,其他参数的含义为:X,Y:鼠标点击坐标;Unk:TOCControl 的对象;Data:class在对象中的index.对于legendclass(图例样式设计),使用方法如下:声明同上的全局变量后:

        axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index);
        if (e.button == 1)
        {
            if (Item == esriTOCControlItem.esriTOCControlItemLegendClass)
            {
                ILegendClass pLC = new LegendClassClass();
                pLC = ((ILegendGroup)other).get_Class((int)index);                  
                ISymbol pSym = pLC.Symbol;
                ISymbolSelector pSS = new ESRI.ArcGIS.DisplayUI.SymbolSelectorClass(); / /创建符号选择器 
                bool a = false;
                pSS.AddSymbol(pSym);
                a = pSS.SelectSymbol(0);//打开设计器
                if (a)
                {
                    pLC.Symbol = pSS.GetSymbolAt(0);
                }
                this.axMapControl1.ActiveView.Refresh();
                this.axTOCControl1.Refresh();            
                    }
        }

如果出现“检索 COM 类工厂中 CLSID 为 {D20B08DF-4FF4-11D2-AB02-00C04FA334B3} 的组件失败”错误,只需将把Program.cs中的 ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop) 改成 ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)。

猜你喜欢

转载自blog.csdn.net/qq_39967296/article/details/89441267
AE