AE学习笔记之添加ToolbarControl控件命令

        在ArcGIS Engine 实际开发中有时经常会需要自定义一些命令或工具,因此在此记录一下AE中添加自定义命令的流程。

下面以添加打开文件的命令为例:首先在项目文件下添加一个类,选择ArcGIS-BascCommand。如下图:

       

        添加一个新的基类后,Arcgis会提示选择的 创建command的类型。我也不知道不同类型的区别,按照名字意思我选择了第二项。


         新类创建成功后,C#中会预先定义好一部分代码。开发人员需要自己重写OnCreate 和 OnClick 两种方法代码。其中OnCreate 中会自动定义hook,即钩子,用于传递控件的信息。而在OnClick函数中则需要写单击按钮控件时所触发的动作。本例是打开地图文档,代码如下:

OpenNewDocument 构造函数:

  public OpenNewMapDocument(AxMapControl mainMapCtl)
        {
            //
            // TODO: Define values for the public properties
            //相关属性设置
            base.m_category = "Generic"; //localizable text
            base.m_caption = "Open";  //localizable text 
            base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl";  //localizable text
            base.m_toolTip = "Open";  //localizable text
            base.m_name = "Generic_Open";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }

            //初始化
            m_MainMapControl = mainMapCtl;
        }


        其中控件的名称在类的构造函数中设置。

  // TODO: Define values for the public properties
            //相关属性设置
            base.m_category = "Generic"; //自定义控件类型
            base.m_caption = "Open";  //自定义控件文字,本例子是将控件上的文字设置为OPen
            base.m_message = "This should work in ArcMap/MapControl/PageLayoutControl";  //自定义控件的提示信息
            base.m_toolTip = "Open";  //自定义鼠标停留的提示信息
            base.m_name = "Generic_Open";   //自定义控件的名称(如 "MyCategory_MyCommand")

        由于加载地图文档需要axMapControl 对象,因此在构造函数的参数中传入项目中的mapControl,并将其赋值给类中定义的m_MainMapControl 。其需要在类中提前定义,如下:

AxMapControl m_MainMapControl = null;   //作为全局变量

然后就是OnClick函数的代码:

 public override void OnClick()
        {
            // TODO: Add OpenNewMapDocument.OnClick implementation
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Map Documents (*.mxd)|*.mxd";
            dlg.Multiselect = false;
            dlg.Title = "Open Map Document";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string docName = dlg.FileName;
                IMapDocument mapDoc = new MapDocumentClass();
                if (mapDoc.get_IsPresent(docName) && !mapDoc.get_IsPasswordProtected(docName))
                {
                    mapDoc.Open(docName, string.Empty);
                    IMap map = mapDoc.get_Map(0);
                  //  m_controlsSynchronizer.ReplaceMap(map);
                    m_MainMapControl.Map = map;
                    mapDoc.Close();
                }
            }
        }

        完成以后需要在项目的load函数中加载该类,因此在form1的load函数中加入如下代码:

 // 添加打开命令按钮到工具条
            OpenNewMapDocument openMapDoc = new OpenNewMapDocument(mainMapControl);
            axToolbarControl1.AddItem(openMapDoc, -1, 0, false, -1, esriCommandStyles.esriCommandStyleIconAndText);

    就是把命令添加到工具条,其中esriCommandStyles.esriCommandStyleIconAndText 是定义控件的样式,该形式是Icon加文字。还可以选择只有位图或只有文字等不同样式。其中位图是新建类时自动创建的一个位图,不需要再自己导入位图。

此时,自定义的空间就已经完成了。结果如下图:

-------------------------------------------------------------------------------end---------------------------------------------------------------------------------


  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值