1.在项目中添加基于Base Command等的类,可改变命名空间名称。
2.引用:命名空间.类名称;
实例:
using System;
using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Resources;using System.Reflection;using System.Drawing;
using ESRI.ArcGIS.ADF.BaseClasses;using ESRI.ArcGIS.ADF.CATIDs;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Display;namespace CSharpMyFullExtent
{ /// <summary> /// Command that works in ArcMap/Map/PageLayout /// </summary> [Guid("4f4e73b1-40c3-44a5-8729-d403b7b3d779")] [ClassInterface(ClassInterfaceType.None)] [ProgId("CSharpMyFullExtent.MyFullExtentCommand")] public sealed class MyFullExtentCommand : 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
#endregionprivate IHookHelper m_hookHelper = null;
public MyFullExtentCommand() { // // TODO: Define values for the public properties // base.m_category = "CustomCommands"; //localizable text base.m_caption = "My Full Extent"; //localizable text base.m_message = "My Full Extent"; //localizable text base.m_toolTip = "My Full Extent"; //localizable text base.m_name = "MyCommands_MyFullExtent"; //unique id, non-localizable (e.g. "MyCategory_MyCommand")//As part of your deployment strategy you must register the name of the WinHelp file (*.hlp)
//as a new string value in: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Help //or provide the full path name. base.m_helpFile = "MyHelpFile.hlp";//The Context Help ID as defined when compiling the RTF file into the HelpFile
base.m_helpID = 1234;ResourceManager rm = new ResourceManager("CSharpMyFullExtent.Resources", Assembly.GetExecutingAssembly());
base.m_bitmap = (System.Drawing.Bitmap)rm.GetObject("CommandImage"); }#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;}
/// <summary>
/// Occurs when this command is clicked /// </summary> public override void OnClick() { //Get IActiveView interface IActiveView pActiveView = m_hookHelper.FocusMap as IActiveView;//Set the extent to the full extent
pActiveView.Extent = pActiveView.FullExtent;//Refresh the active view
pActiveView.Refresh(); }#endregion
}}
引用:
axToolbarControl1.AddItem("CSharpMyFullExtent.MyFullExtentCommand", 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly);