liutingxiang的个人博客分享 http://blog.sciencenet.cn/u/liutingxiang

博文

ArcGIS Engine二次开发学习(7)自定义命令

已有 4269 次阅读 2018-4-17 11:04 |个人分类:地理信息系统二次开发|系统分类:教学心得

 要求:在ToolbarControl中添加一个自定义命令,点击可清除当前活动工具。



步骤:

1、创建GIS类,选择Base Command模版,Extending ArcObjects,ArcMap MapControl or PageLayoutControl command,命名为ClearCurrentActiveToolCmd.cs


2、在类中定义IToolbarControl的接口变量m_ToolbarControl,并在类的构造函数中改变所继承的基类属性,其中,base.m_bitmap,即该命令的图标可直接双击本工程中的ClearCurrentActiveToolCmd.bmp修改。


3、在OnCreate重载函数中,将Hook转换为IToolbarControl,并赋给m_ToolbarControl变量。


4、在OnClick重载函数中,将m_ToolbarControl接口变量的CurrentTool属性设置为空。


代码如下:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;

namespace WindowsFormsApplication2_1_2
{
    /// <summary>
    /// Summary description for ClearCurrentActiveToolCmd.
    /// </summary>
    [Guid("df41e943-e173-4d88-90c1-52d7230ca74a")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("WindowsFormsApplication2_1_2.ClearCurruntActiveToolCmd")]
    public sealed class ClearCurrentActiveToolCmd : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;
        private IToolbarControl m_ToolbarControl;

        public ClearCurrentActiveToolCmd()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "Custom Command"; //localizable text
            base.m_caption = "清除当前活动工具";  //localizable text
            base.m_message = "清除当前活动工具";  //localizable text 
            base.m_toolTip = "清除当前活动工具";  //localizable text 
            base.m_name = "清除当前活动工具";   //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");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            if (m_hookHelper.Hook is IToolbarControl)
            {
                m_ToolbarControl = m_hookHelper.Hook as IToolbarControl;
                m_ToolbarControl.CurrentTool = null;
            }
            // TODO: Add ClearCurruntActiveToolCmd.OnClick implementation
        }

        #endregion
    }
}


5、在Form1.cs的窗体load事件中添加代码,将该命令添加到toolstrip上。


axToolbarControl1.AddItem(new ClearCurrentActiveToolCmd(), -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);



https://blog.sciencenet.cn/blog-3373120-1109422.html

上一篇:ArcGIS Engine二次开发学习(6)属性表操作
下一篇:ArcGIS Engine二次开发学习(8)根据属性查询
收藏 IP: 121.69.12.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 11:02

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部