About the Create a command by inheriting from BaseCommand Sample
[C#]
ZoomToLayer.cs
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
namespace CommandInheritingBaseCommand
{
/// <summary>
/// Summary description for ZoomToLayer.
/// </summary>
[Guid("46bd0933-acac-4c33-8669-1255db03ca5c")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CommandInheritingBaseCommand.ZoomToLayer")]
public sealed class ZoomToLayer : 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);
}
/// <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);
}
#endregion
#endregion
private IApplication m_application;
public ZoomToLayer()
{
//
// TODO: Define values for the public properties
//
base.m_category = "Developer Samples"; //localizable text
base.m_caption = "Zoom To Layer CSharp"; //localizable text
base.m_message = "Zoom to the extent of the active layer in the TOC"; //localizable text
base.m_toolTip = "Zoom To Layer CSharp"; //localizable text
base.m_name = "DeveloperSamples_ZoomToLayerCSharp"; //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
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 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;
m_application = hook as IApplication;
//Disable if it is not ArcMap
if (hook is IMxApplication)
base.m_enabled = true;
else
base.m_enabled = false;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
IMxDocument mxDocument = GetMxDocument(m_application);
ZoomToLayerInTOC(mxDocument);
}
#endregion
#region "Zoom to Active Layer in TOC"
// ArcGIS Snippet Title:
// Zoom to Active Layer in TOC
//
// Add the following references to the project:
// ESRI.ArcGIS.ArcMapUI
// ESRI.ArcGIS.Carto
// ESRI.ArcGIS.Geometry
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Desktop
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
// Use the following XML documentation comments to use this snippet:
/// <summary>Zooms to the selected layer in the TOC associated with the active view.</summary>
///
/// <param name="mxDocument">An IMxDocument interface</param>
///
/// <remarks></remarks>
public void ZoomToLayerInTOC(ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument)
{
if (mxDocument == null)
{
return;
}
ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView;
// Get the TOC
ESRI.ArcGIS.ArcMapUI.IContentsView IContentsView = mxDocument.CurrentContentsView;
// Get the selected layer
System.Object selectedItem = IContentsView.SelectedItem;
if (!(selectedItem is ESRI.ArcGIS.Carto.ILayer))
{
return;
}
ESRI.ArcGIS.Carto.ILayer layer = selectedItem as ESRI.ArcGIS.Carto.ILayer;
// Zoom to the extent of the layer and refresh the map
activeView.Extent = layer.AreaOfInterest;
activeView.Refresh();
}
#endregion
#region "Get MxDocument from ArcMap"
// ArcGIS Snippet Title:
// Get MxDocument from ArcMap
//
// Add the following references to the project:
// ESRI.ArcGIS.ArcMapUI
// ESRI.ArcGIS.Framework
// ESRI.ArcGIS.System
//
// Intended ArcGIS Products for this snippet:
// ArcGIS Desktop
//
// Required ArcGIS Extensions:
// (NONE)
//
// Notes:
// This snippet is intended to be inserted at the base level of a Class.
// It is not intended to be nested within an existing Method.
//
// Use the following XML documentation comments to use this snippet:
/// <summary>Get MxDocument from ArcMap.</summary>
///
/// <param name="application">An IApplication interface that is the ArcMap application.</param>
///
/// <returns>An IMxDocument interface.</returns>
///
/// <remarks></remarks>
public ESRI.ArcGIS.ArcMapUI.IMxDocument GetMxDocument(ESRI.ArcGIS.Framework.IApplication application)
{
if (application == null)
{
return null;
}
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ((ESRI.ArcGIS.ArcMapUI.IMxDocument)(application.Document)); // Explicit Cast
return mxDocument;
}
#endregion
}
}
[Visual Basic .NET]
ZoomToLayer.vb
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
<ComClass(ZoomToLayer.ClassId, ZoomToLayer.InterfaceId, ZoomToLayer.EventsId), _
ProgId("CommandInheritingBaseCommand.ZoomToLayer")> _
Public NotInheritable Class ZoomToLayer
Inherits BaseCommand
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "92a9664b-e3e3-43b9-a758-a3157981868a"
Public Const InterfaceId As String = "4d2eda59-1721-4575-bec1-43c9f0163959"
Public Const EventsId As String = "cdfcbab2-2be5-4ee0-adf5-8cd069c9fd2b"
#End Region
#Region "COM Registration Function(s)"
<ComRegisterFunction(), ComVisibleAttribute(False)> _
Public Shared Sub RegisterFunction(ByVal registerType As Type)
' Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType)
'Add any COM registration code after the ArcGISCategoryRegistration() call
End Sub
<ComUnregisterFunction(), ComVisibleAttribute(False)> _
Public Shared Sub UnregisterFunction(ByVal registerType As Type)
' Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType)
'Add any COM unregistration code after the ArcGISCategoryUnregistration() call
End Sub
#Region "ArcGIS Component Category Registrar generated code"
Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
MxCommands.Register(regKey)
End Sub
Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
MxCommands.Unregister(regKey)
End Sub
#End Region
#End Region
Private m_application As IApplication
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
' TODO: Define values for the public properties
MyBase.m_category = "Developer Samples" 'localizable text
MyBase.m_caption = "Zoom To Layer VBNet" 'localizable text
MyBase.m_message = "Zoom to the extent of the active layer in the TOC" 'localizable text
MyBase.m_toolTip = "Zoom To Layer VBNet" 'localizable text
MyBase.m_name = "DeveloperSamples_ZoomToLayerVBNet" 'unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
Try
'TODO: change bitmap name if necessary
Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
Catch ex As Exception
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
End Try
End Sub
Public Overrides Sub OnCreate(ByVal hook As Object)
If Not hook Is Nothing Then
m_application = CType(hook, IApplication)
'Disable if it is not ArcMap
If TypeOf hook Is IMxApplication Then
MyBase.m_enabled = True
Else
MyBase.m_enabled = False
End If
End If
' TODO: Add other initialization code
End Sub
Public Overrides Sub OnClick()
Dim mxDocument As IMxDocument = GetMxDocument(m_application)
ZoomToLayerInTOC(mxDocument)
End Sub
#Region "Zoom to Active Layer in TOC"
' ArcGIS Snippet Title:
' Zoom to Active Layer in TOC
'
' Add the following references to the project:
' ESRI.ArcGIS.ArcMapUI
' ESRI.ArcGIS.Carto
' ESRI.ArcGIS.Geometry
'
' Intended ArcGIS Products for this snippet:
' ArcGIS Desktop
'
' Required ArcGIS Extensions:
' (NONE)
'
' Notes:
' This snippet is intended to be inserted at the base level of a Class.
' It is not intended to be nested within an existing Sub or Function.
'
' Use the following XML documentation comments to use this snippet:
''' <summary>Zooms to the selected layer in the TOC associated with the active view.</summary>
'''
''' <param name="mxDocument">An IMxDocument interface</param>
'''
''' <remarks></remarks>
Public Sub ZoomToLayerInTOC(ByVal mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument)
If mxDocument Is Nothing Then
Return
End If
' Get the map
Dim activeView As ESRI.ArcGIS.Carto.IActiveView = mxDocument.ActiveView
' Get the TOC
Dim contentsView As ESRI.ArcGIS.ArcMapUI.IContentsView = mxDocument.CurrentContentsView
' Get the selected layer
Dim selectedItem As System.Object = contentsView.SelectedItem
If Not (TypeOf selectedItem Is ESRI.ArcGIS.Carto.ILayer) Then
Return
End If
Dim layer As ESRI.ArcGIS.Carto.ILayer = TryCast(selectedItem, ESRI.ArcGIS.Carto.ILayer) ' Dynamic Cast
' Zoom to the extent of the layer and refresh the map
activeView.Extent = layer.AreaOfInterest
activeView.Refresh()
End Sub
#End Region
#Region "Get MxDocument from ArcMap"
' ArcGIS Snippet Title:
' Get MxDocument from ArcMap
'
' Add the following references to the project:
' ESRI.ArcGIS.ArcMapUI
' ESRI.ArcGIS.Framework
' ESRI.ArcGIS.System
'
' Intended ArcGIS Products for this snippet:
' ArcGIS Desktop
'
' Required ArcGIS Extensions:
' (NONE)
'
' Notes:
' This snippet is intended to be inserted at the base level of a Class.
' It is not intended to be nested within an existing Sub or Function.
'
' Use the following XML documentation comments to use this snippet:
''' <summary>Get MxDocument from ArcMap.</summary>
'''
''' <param name="application">An IApplication interface that is the ArcMap application.</param>
'''
''' <returns>An IMxDocument interface.</returns>
'''
''' <remarks></remarks>
Public Function GetMxDocument(ByVal application As ESRI.ArcGIS.Framework.IApplication) As ESRI.ArcGIS.ArcMapUI.IMxDocument
If application Is Nothing Then
Return Nothing
End If
Dim mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument = (CType(application.Document, ESRI.ArcGIS.ArcMapUI.IMxDocument)) ' Explicit Cast
Return mxDocument
End Function
#End Region
End Class