ArcEngine——移除當前圖層

/***************************
 Write by Chn gzGISer Tst
 CSDN blog Ricardo.M.Tan
 **************************/
using System;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;

namespace Chn.SpatialDataProcessing.Command {
    /// <summary>
    /// 移除圖層
    /// </summary>
    [Guid("91dc2719-f181-4c3f-bc16-5486c88057fa")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Chn.SpatialDataProcessing.Command.RemoveLayerCmd")]
    public sealed class RemoveLayerCmd : 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);
            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 IHookHelper m_hookHelper = null;
        private IMapControl3 m_mapcontrol = null;
        private ILayer currentLayer = null;
        private IActiveView m_activeView = null;
        private IMap m_map = null;
        /// <summary>
        /// 構造函數
        /// </summary>
        public RemoveLayerCmd() {
            m_category = "通用工具";
            m_caption = "移除當前圖層";
            m_message = "移除當前圖層";
            m_toolTip = "移除當前圖層";
            m_name = "RemoveLayerCmd";

            try {
                m_bitmap = Properties.Resources.RemoveLayerCmd;
            } catch (Exception ex) {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// 移除命令被創建時
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook) {
            if (hook == null)
                return;
            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();
            m_hookHelper.Hook = hook;
        }

        /// <summary>
        /// 移除命令被創建時
        /// </summary>
        public override void OnClick() {
            if (m_hookHelper.Hook is IMapControl3) {
                m_mapcontrol = m_hookHelper.Hook as IMapControl3;
                currentLayer = m_mapcontrol.CustomProperty as ILayer;
                m_map = m_mapcontrol.Map;
                m_activeView = m_map as IActiveView;
            }

            if (m_map == null || currentLayer == null) return;
            m_map.DeleteLayer(currentLayer);
            m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_activeView.Extent);
        }
        #endregion
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章