ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-07-09 16:08:03  阅读:149  来源: 互联网

标签:控件 windows comboBox ArcGIS Add hookHelper null public


向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
    }
}

 

posted on 2016-10-20 20:30  gisoracle  阅读(1727)  评论(0)  编辑  收藏  举报

标签:控件,windows,comboBox,ArcGIS,Add,hookHelper,null,public
来源: https://blog.51cto.com/u_12139363/3030600

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有