Welcome to second walkthrough on adding custom ArcGIS component (command) to standard tool bar in ArcMap. We have successfully created custom command using C#; adding this command to ArcMap standard toolbar through ESRI IExtension.
Tools & Language: Visual Studio 2005, C#.NET, ArcGIS 9.2
Manually we can add the command using ArcMap Tools–>Customize–>Click Command Tab, Choose the category and drag the command to toolbar. If we want to save this command permanently in ArcMap; use ‘Save In’ option to ‘Normal.mxt’ . The command will be saved on template.
The purpose of this walkthrough is to add the custom command to ESRI toolbar programmatically. ESRI has provided option to develop Application Extension inheriting ESRI IExtension. By inheriting IExtension class we can create our own extension and add the extension in categories manager. Applications loads extension AUTOMATICALLY on Startup. Life cycle of loading extension is a) Application starts b) Extension are loaded c) Document is loaded d) Document is closed e) Extensions are unloaded .
There are two types of extension they are Visible and Invisible (present walkthrough). Visible extension inherits IExtensionConfig and it will be added in Extension Manager Window. Looks like other ESRI extension along with its description.
Once you added this class, new class ‘ApplicationExtension1.cs’ will create by inheriting base class IExtension. This class contains functions to Register COM components (Register and UnRegister) and IExtension members like Extension name (Property-String) , Startup, Shutdown methods.
Our objective is to add the developed custom command to existing standard toolbar, i.e. whenever we open Map Document is opened, and command should be added in the toolbar. For achieving this, we have add delegate (IDocumentEvent_NewDocumentEventHandler) and pass a method to be fired on startup.
public void Startup(ref object initializationData)
{
//m_application is ArcMap Application
m_application = initializationData as IApplication;
if (m_application == null)
return;
IMxDocument pMxDoc;
// pMxDoc is ArcMap Map Document
pMxDoc = (IMxDocument)m_application.Document;
// Creating Event Handler with a Method ’onNewOpenDoc’
((IDocumentEvents_Event)pMxDoc).NewDocument += new IDocumentEvents_NewDocumentEventHandler(OnNewOpenDoc);
}
private void OnNewOpenDoc()
{
ICommandBar pCmdBar;
//Create two new UID objects from UID class
UID pUID = new UID();
UID pCommandUID=new UID();
//pUID value is ArcMap Standard ToolBar UID Value taken from here
pUID.Value = “{5DEB1DB8-C2A9-11D1-B9A2-080009EE4E51}”;
pCmdBar= ICommandBar)m_application.Document.CommandBars.Find(pUID, false, false);
// pCommandUID value is custom command UID value found in the class //created
pCommandUID.Value = “{a657d37d-5cdd-4315-8866-87e389f2ffb8}”;
object index=0;
// Note object called index is set to ‘0′; command will be added as first //command in toolbar
pCmdBar.Add(pCommandUID,ref index);
}
In ‘OnNewOpenDoc’ method we have to write code for adding the custom command to toolbar. This method gets fired on opening every new ArcMap Instance.
Build and run the project. Now you can notice your custom command appeared as first item in a standard toolbar (make visible, if not) of ArcMap.
I hope that this tutorial will be of much help to beginner who starts programming in ArcObjects. I will add some useful external links to this post.
Attachment: extending-arcgisnet PDF

