向ArcGIS的ToolBarControl中添加任意的windows控件的方法


概要:在使用ArcEngine開發中,給ToolbarControl添加按鈕形式的命令項相信大家都很熟悉了,因為網上的例子很多。但這種使用click調用功能的方式只能滿足大部分用戶在體驗方面的需求,除此之外用戶很可能要求你在工具條中增加類似文本框,單選框、選擇面板,combobox等windows控件,今天有個同事問我這個問題就在這里做一個實例。供大家參考。

具體實現:

  1 知識整備

 (1 )其實要實現這個效果很簡單,只要大家了解Arcgis中的IToolControl接口的使用方法,就不難實現。

   IToolControl 這個接口有只有簡單的三個方法:

 
hwnd:是個只讀屬性,用於給調用者返回控件 句柄。
OnDrop:是一個方法,用於調用者驗證當前控件是否可拖動。
OnFocus:是一個方法,用於調用通知當前項,你已經聚焦。
(2)除了了解IToolCotrol接口之外,大家還必須具備知道如何創建Arcgis 命令插件的知識,以及如何調用插件的方法。
 
2 實現
   以一個簡單Combobox為例:
    public sealed class Command1 : BaseCommand, IToolControl
    {
 

        private int _handle = 0;
        private ICompletionNotify _CompNotify;
        private System.Windows.Forms.ComboBox comboBox = new System.Windows.Forms.ComboBox();
        private IHookHelper m_hookHelper = null;
        public Command1()
        {
            this._handle = comboBox.Handle.ToInt32();
            comboBox.Items.Add("大家好才是真的好1");
            comboBox.Items.Add("大家好才是真的好1");
            comboBox.Items.Add("大家好才是真的好1");
        }

        #region Overriden 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;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add Command1.OnClick implementation
        }

        #endregion

        #region IToolControl 成員

        public bool OnDrop(esriCmdBarType barType)
        {
            if (barType == esriCmdBarType.esriCmdBarTypeToolbar)
            {
                return true;
            }
            else return false;
        }

        public void OnFocus(ICompletionNotify complete)
        {
            _CompNotify = complete;
        }

        public int hWnd
        {
            get
            {
                return _handle;
            }
        }

        #endregion
    }

3 實現效果
 
照着這個實例可以添加,其它的windows控件
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;

namespace AddCustomControlToToolbar
{
    /// <summary>
    /// Command that works in ArcMap/Map/PageLayout
    /// </summary>
    [Guid("7e8238b9-b38c-417c-894f-34ca9d99b634")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("AddCustomControlToToolbar.Command1")]
    public sealed class Command1 : BaseCommand, IToolControl
    {
        #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);
            MxCommands.Register(regKey);
            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);
            MxCommands.Unregister(regKey);
            ControlsCommands.Unregister(regKey);
        }

        #endregion
        #endregion

        private int _handle = 0;
        private ICompletionNotify _CompNotify;
        private System.Windows.Forms.ComboBox comboBox = new System.Windows.Forms.ComboBox();
        private IHookHelper m_hookHelper = null;
        public Command1()
        {
            this._handle = comboBox.Handle.ToInt32();
            comboBox.Items.Add("大家好才是真的好1");
            comboBox.Items.Add("大家好才是真的好1");
            comboBox.Items.Add("大家好才是真的好1");
        }

        #region Overriden 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;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add Command1.OnClick implementation
        }

        #endregion

        #region IToolControl 成員

        public bool OnDrop(esriCmdBarType barType)
        {
            if (barType == esriCmdBarType.esriCmdBarTypeToolbar)
            {
                return true;
            }
            else return false;
        }

        public void OnFocus(ICompletionNotify complete)
        {
            _CompNotify = complete;
        }

        public int hWnd
        {
            get
            {
                return _handle;
            }
        }

        #endregion
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM