1. 程式人生 > >ArcGIS Engine 開發學習-------基礎(一):自定義命令

ArcGIS Engine 開發學習-------基礎(一):自定義命令

 要求:在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);



轉載本文請聯絡原作者獲取授權,同時請註明本文來自劉廷祥科學網部落格。
連結地址:http://blog.sciencenet.cn/blog-3373120-1109422.html