About the Using HookActions in custom commands Sample
[C#]
hookActionsPan.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
namespace HookActions
{
[Guid("97407C39-DB5B-4e84-B21A-3CF3A0902838")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("HookActions.hookActionsPan")]
public sealed class hookActionsPan : 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);
GMxCommands.Register(regKey);
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);
GMxCommands.Unregister(regKey);
MxCommands.Unregister(regKey);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper = null;
private IGlobeHookHelper m_globeHookHelper = null;
public hookActionsPan()
{
base.m_category = "HookActions";
base.m_caption = "Pan selected features";
base.m_message = "Pan selected features";
base.m_toolTip = "Pan selected features";
base.m_name = "HookActions_Pan";
}
public override void OnCreate(object hook)
{
// Test the hook that calls this command and disable if nothing valid
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)
{
//Can be globe
try
{
m_globeHookHelper = new GlobeHookHelperClass();
m_globeHookHelper.Hook = hook;
if (m_globeHookHelper.ActiveViewer == null)
{
//Nothing valid!
m_globeHookHelper = null;
}
}
catch
{
m_globeHookHelper = null;
}
}
if (m_globeHookHelper == null && m_hookHelper == null)
base.m_enabled = false;
else
base.m_enabled = true;
}
public override bool Enabled
{
get
{
IHookActions hookActions = null;
IBasicMap basicMap = null;
//Get basic map and set hook actions
if (m_hookHelper != null)
{
basicMap = m_hookHelper.FocusMap as IBasicMap;
hookActions = m_hookHelper as IHookActions;
}
else if (m_globeHookHelper != null)
{
basicMap = m_globeHookHelper.Globe as IBasicMap;
hookActions = m_globeHookHelper as IHookActions;
}
//Disable if no features selected
IEnumFeature enumFeature = basicMap.FeatureSelection as IEnumFeature;
IFeature feature = enumFeature.Next();
if (feature == null) return false;
//Enable if action supported on first selected feature
if (hookActions.get_ActionSupported(feature.Shape, esriHookActions.esriHookActionsPan))
return true;
else
return false;
}
}
public override void OnClick()
{
IHookActions hookActions = null;
IBasicMap basicMap = null;
//Get basic map and set hook actions
if (m_hookHelper != null)
{
basicMap = m_hookHelper.FocusMap as IBasicMap;
hookActions = m_hookHelper as IHookActions;
}
else if (m_globeHookHelper != null)
{
basicMap = m_globeHookHelper.Globe as IBasicMap;
hookActions = m_globeHookHelper as IHookActions;
}
//Get feature selection
ISelection selection = basicMap.FeatureSelection;
//Get enumerator
IEnumFeature enumFeature = selection as IEnumFeature;
enumFeature.Reset();
//Set first feature
IFeature feature;
feature = enumFeature.Next();
//Loop though the features
IArray array = new ESRI.ArcGIS.esriSystem.Array();
while (feature != null)
{
//Add feature to array
array.Add(feature.Shape);
//Set next feature
feature = enumFeature.Next();
}
//If the action is supported perform the action
if (hookActions.get_ActionSupportedOnMultiple(array, esriHookActions.esriHookActionsPan))
hookActions.DoActionOnMultiple(array, esriHookActions.esriHookActionsPan);
}
}
}
[Visual Basic .NET]
hookActionsPan.vb
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geodatabase
<ComClass(hookActionsFlash.ClassId, hookActionsFlash.InterfaceId, hookActionsFlash.EventsId), _
ProgId("HookActions.hookActionsPan")> _
Public NotInheritable Class hookActionsPan
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 = "95B205CA-8E9D-4409-844F-AB777CFD2C95"
Public Const InterfaceId As String = "DB7606BA-77A0-46b2-92D0-3841AC08489E"
Public Const EventsId As String = "49482A53-B22C-4b69-84BA-3006029D5953"
#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)
ControlsCommands.Register(regKey)
MxCommands.Register(regKey)
GMxCommands.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)
ControlsCommands.Unregister(regKey)
MxCommands.Unregister(regKey)
GMxCommands.Unregister(regKey)
End Sub
#End Region
#End Region
Private m_hookHelper As IHookHelper
Private m_globeHookHelper As IGlobeHookHelper
' 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()
MyBase.m_category = "HookActions"
MyBase.m_caption = "Pan selected features"
MyBase.m_message = "Pan selected features"
MyBase.m_toolTip = "Pan selected features"
MyBase.m_name = "HookActions_Pan"
End Sub
Public Overrides Sub OnCreate(ByVal hook As Object)
' Test the hook that calls this command and disable if nothing is valid
If Not hook Is Nothing Then
Try
m_hookHelper = New HookHelperClass
m_hookHelper.Hook = hook
If m_hookHelper.ActiveView Is Nothing Then m_hookHelper = Nothing
Catch
m_hookHelper = Nothing
End Try
If m_hookHelper Is Nothing Then
'Can be globe
Try
m_globeHookHelper = New GlobeHookHelperClass
m_globeHookHelper.Hook = hook
If m_globeHookHelper.ActiveViewer Is Nothing Then m_globeHookHelper = Nothing
Catch
m_globeHookHelper = Nothing
End Try
End If
End If
If m_globeHookHelper Is Nothing And m_hookHelper Is Nothing Then
MyBase.m_enabled = False
Else
MyBase.m_enabled = True
End If
End Sub
Public Overrides ReadOnly Property Enabled() As Boolean
Get
Dim hookActions As IHookActions = Nothing
Dim basicMap As IBasicMap = Nothing
'Get basic map and set hook actions
If Not m_hookHelper Is Nothing Then
basicMap = m_hookHelper.FocusMap
hookActions = m_hookHelper
ElseIf Not m_globeHookHelper Is Nothing Then
basicMap = CType(m_globeHookHelper.Globe, IBasicMap)
hookActions = m_globeHookHelper
End If
'Disable if no features selected
Dim enumFeature As IEnumFeature = basicMap.FeatureSelection
Dim feature As IFeature = enumFeature.Next
If feature Is Nothing Then Return False
'Enable if action supported on first selected feature
If hookActions.ActionSupported(feature.Shape, esriHookActions.esriHookActionsPan) Then
Return True
Else
Return False
End If
End Get
End Property
Public Overrides Sub OnClick()
Dim hookActions As IHookActions = Nothing
Dim basicMap As IBasicMap = Nothing
'Get basic map and set hook actions
If Not m_hookHelper Is Nothing Then
basicMap = m_hookHelper.FocusMap
hookActions = m_hookHelper
ElseIf Not m_globeHookHelper Is Nothing Then
basicMap = CType(m_globeHookHelper.Globe, IBasicMap)
hookActions = m_globeHookHelper
End If
'Get feature selection
Dim selection As ISelection
selection = basicMap.FeatureSelection
'Get enumerator
Dim enumFeature As IEnumFeature, feature As IFeature
enumFeature = selection
enumFeature.Reset()
'Set first feature
feature = enumFeature.Next
'Loop though the features
Dim array As IArray = New Array()
Do Until feature Is Nothing
'Add feature to array
array.Add(feature.Shape)
'Set next feature
feature = enumFeature.Next
Loop
'If the action is supported perform the action
If hookActions.ActionSupportedOnMultiple(array, esriHookActions.esriHookActionsPan) Then
hookActions.DoActionOnMultiple(array, esriHookActions.esriHookActionsPan)
End If
End Sub
End Class