The ArcGIS Visual Studio IDE Integration Framework includes item templates for the ArcGIS Base Command and Base Tool. Using these item templates allows you to create commands and tools more efficiently than directly implementing the ICommand and ITool interfaces. The following are the advantages of using the item templates:
The ArcGIS Base Command and Base Tool class inherits from the BaseCommand and BaseTool abstract classes. Instead of implementing the Bitmap, Caption, Category, Name, Message, and ToolTipproperties individually, you can set the values that should be returned from these read-only properties in the class constructor and rely on the BaseCommand or BaseTool class to provide the implementation for these methods. The other members will be left to return the default values as implemented by the BaseCommand or BaseTool class.
Abstract classes cannot be instantiated and frequently contain only partial implementation code or no implementation at all. They are closely related to interfaces. However, they differ significantly from interfaces in that a class may implement any number of interfaces, but it can inherit from only one abstract class.
The ArcGIS Base Command and Base Tool override the OnCreate method that is passed a handle or hook to the ArcGIS Engine control or ArcGIS Desktop application that the command works with (for example, MapControl, PageLayoutControl, ToolbarControl, or ArcMap). Rather than adding code to an OnCreate method to determine the type of hook that is passed to the command, the HookHelper object handles this. The HookHelper object is used to hold on to the hook, and the IHookHelper interface it implements can return the ActiveView, PageLayout, and Map regardless of the type of hook that is passed.
ArcGIS Engine expects a custom command or tool to be a Component Object Model (COM) class. The ArcGIS Base Command and Base Tool provide the attributes and globally unique identifiers (GUIDs) required by COM in the class.
Visual Studio provides the ability to specify functions that execute when an assembly exposed for COM is registered and unregistered on a system. This allows the class to be registered in a component category that a Customize dialog box looks for. The ArcGIS Base Command and Base Tool provide these COM component category registration functions.
Creating a Base Command or Base Tool
To add an ArcGIS Base Tool or Base Command to your Microsoft Visual Studio project, do one of the following:
Select the project name in the Solution Explorer and choose Add New Item from the Project menu.
Alternatively, right-click the project name in the Solution Explorer and choose Add, then New Item. Or press Ctrl+Shift+A.
Select Base Command or Base Tool from the Add New Item dialog box. See the following screen shot:
After you click Add, the ArcGIS New Item Wizard Options dialog box appears. Select the type of command or tool you want to create and click OK. The type of command or tool you choose depends on the ArcGIS Engine control or ArcGIS Desktop application you want the command or tool to work with. See the following screen shots:
A Universal Command or Universal Tool works with the ArcGIS Engine MapControl, PageLayoutControl, GlobeControl, and SceneControl, as well as the ArcGIS Desktop ArcMap, ArcGlobe, and ArcScene applications. A Blank Command or Blank Tool does not contain any component category registration code or any implementation within the OnCreate method; you need to provide this.
Add custom functionality to the command or tool. For example, provide Bitmap, Caption, Category, Name, Message, and ToolTip values for the command or tool in the class constructor. In the case of a command, provide implementation in the OnClick method. In the case of a tool, provide implementation in the OnMouseDown, OnMouseMove, or OnMouseUp methods.
Using a Base Command or Base Tool in an application
There are several ways a custom ArcGIS Base Command or Base Tool can be used in an application once it has been fully implemented and built as discussed next. For further information, see the following:
In custom ArcGIS Engine applications in which you use the ToolbarControl in conjunction with a buddy control (MapControl, PageLayoutControl, GlobeControl, or SceneControl), the command or tool can be added to the ToolbarControl in one of the following ways:
Using the ToolbarControl property pages at design time
Once a command or tool has been added to the ToolbarControl, the ToolbarControl passes the hook to the OnCreatemethod.
In this case, the hook that is passed is the actual ToolbarControl. If the command or tool uses a HookHelper, GlobeHookHelper, or SceneHookHelper object, they internally return properties from the ToolbarControl Buddy (MapControl, PageLayoutControl, GlobeControl, or SceneControl).
Using the ArcGIS Engine controls without the ToolbarControl
In custom ArcGIS Engine applications where the ToolbarControl is not required, the command or tool must work directly with the MapControl, PageLayoutControl, GlobeControl, or SceneControl. As a developer, you must programmatically perform the following tasks:
Create a new instance of the command or tool.
Pass the individual ArcGIS Engine control to the OnCreate method. When passing the control, use the Object property (for example, AxGlobeControl.Object when using the host wrapper) to ensure the real control is passed to the command or tool. This is done because .NET contains the real control inside a wrapper object or host.
The ArcGIS Engine controls are contained in a Controls library. For the .NET application programming interface (API), there are two primary interop assemblies provided by ESRI for the ArcGIS Engine controls. The first, ESRI.ArcGIS.AxControl, inherits from the .NET AxHost class and enables the controls to be embedded into a .NET container, such as a form. The second, ESRI.ArcGIS.Control, is a wrapper around the actual type library.
In the case of a command, call the OnClick method at the appropriate time to perform the specific action. See the following code example:
[C#]
//Create a command instance.
ICommand command = new myCommandClass();
//Pass the MapControl to the OnCreate method.
command.OnCreate(axMapControl1.Object);
//Call OnClick.
command.OnClick();
[VB.NET]
'Create a command instance.Dim command As ICommand = New myCommandClass()
'Pass the MapControl to the OnCreate method.
command.OnCreate(AxMapControl1.Object)
'Call OnClick.
command.OnClick()
In the case of a tool, set the CurrentTool of the ArcGIS Engine control. The ArcGIS Engine control sends any keyboard and mouse event to the tool. See the following code example:
[C#]
//Create a tool instance.
ICommand command = new myToolClass();
//Pass the MapControl to the OnCreate method.
command.OnCreate(axMapControl1.Object);
//Set the MapControl's current tool.
axMapControl1.CurrentTool = command as ITool;
[VB.NET]
'Create a tool instance.Dim command As ICommand = New myToolClass()
'Pass the MapControl to the OnCreate method.
command.OnCreate(AxMapControl1.Object)
'Set the MapControl's current tool.
AxMapControl1.CurrentTool = command
The Enabled, Caption, and Bitmap properties of the command or tool can be read and incorporated into properties of a command button supplied by the development environment; for example, to build up the user interface of the application.
Using an ArcGIS Desktop application
A custom command or tool built with the ArcGIS Engine can be added to an ArcGIS Desktop ArcMap, ArcGlobe, or ArcScene application using, for example, the Customize dialog box supplied by these applications. This is because the libraries and objects available in ArcGIS Engine are also available in ArcGIS Desktop.