Diving into C#
Outside, it's been a chilly few days here in Denver, which has given me the opportunity to ramp up some of my skills. I'm a VB guy, but recently I dove into building some ArcObjects components in C#. The experience reminded me of a long-ago, early-morning, Adirondack scout camp, "polar bear" swim. Standing before the icy black, I shivered at the thought of jumping in... but the experience was surprisingly painless, and it was over and done before I knew it.
I started by working through the ESRI C# Walkthrough: Creating a simple command for ArcMap
Since I'm using Visual Studio 2005 (.NET 2.0) and I'm also using the Visual C# Express Edition, I found a few differences from the ESRI documented steps. I figured I'd note those differences here:
Create a new class library
Steps worked fine
Reference the ESRI object libraries
I found it useful to also reference to ESRI.ArcGIS.Utility. I used this later for component category registration.
Create a zoom-in command
Steps worked fine
Add a using directive
To define my class, I modified the default .cls. By default, you'll get these directives:
using System;
using System.Collections.Generic;
using System.Text;
I found that I only need the first, and I also added this directive:
using ESRI.ArcGIS.Utility.CATIDs;
ESRI Step 4 states "Right-click the ICommand interface, click Add, and then click Implement Interface"...I couldn't find this functionality. Perhaps its because I'm using the Express version...
So, I manually stubbed out my class...actually, I copy/pasted from another ESRI dev sample.
Adding code to the members of ICommand
IntPntr is defined in System, so you'll either need a directive (see above), or you'll have to fully qualify:
private System.IntPtr m_hBitmap;
Returning the ICommand::Bitmap property
Steps worked fine
Expose the ZoomIn class to COM
To reg. for COM interop, Goto Project > Properties > Build > Register for COM interop. Also, maybe this is obvious, but I had to add GuidGen.exe as an External Tool (again, I'm using the Express Version of Visual Studio). Location for me was: "C:\Program Files\Microsoft Visual Studio 8\Common7\Tools"
Adding COM category registration functions
I did this differently. Since we're using ESRI.ArcGIS.Utility.CATIDs, you can do this instead:
#region "Component Category Registration"
[ComRegisterFunction()]
static void Reg(string regKey)
{
MxCommands.Register(regKey);
}
[ComUnregisterFunction()]
static void Unreg(string regKey)
{
MxCommands.Unregister(regKey);
}
#endregion
Compiling the project, Use the command in ArcMap
In order to get a .tlb, I had to go to Project > Properties > Application > Assembly Info > and check 'Make Assembly COM-visible'
Stepping through the command with the debugger
I found that I wasn't able to do this with the Express Version of VS... but I was hungry for lunch and I didn't try to hard...
Talk to you soon,
-Cory

1 Comments:
Once you went through all that, did you try to add the new command in ArcMap? I'll be surprised if you actually saw the category "developer samples" in the list. FYI, the changes you made to walkthrough 1 is shown in walkthrough 2. They wanted to show you different ways of registering and unregistering the objects.
6:18 PM
Post a Comment
<< Home