使用.Net开发ArcGIS9扩展组件的注册到ArcMAP中
最近碰到一个需求:要求用vs2005平台C#语言开发一个功能,注册到ArcMap中使用,由于最开始自己接触的都是AE接口,对于AO不是很熟悉,对于其扩展也不是很熟悉,所以最近查阅了大量的资料,大概了解了这种需求的解决方案。
以前对于ArcMAP功能扩展就只知道使用VBA宏进行扩展,而且一般使用VBA也只是使用其来测试不熟悉的几个接口而已。
使用C#开发注册到ArcMAP中COM组件需要注意以下几个关键点:
一:对于输出的类库需要选择可以注册到COM组件,其中的几个设置,在属性生成中选择“位COM Interop注册”,在AssemblyInfo.cs中修改ComVisiable位true;
二:对于所输出的类需要添加ArcGIS提供的两个方法,分别为MxCommands.Register(regKey);
MxCommands.Unregister(regKey);
三:将自定义的工具编译好之后,会在当前编译的环境在中自动注册到ArcMAP中,但是对于需要部署到客户机器上的组件来说,这种方法还是有缺陷的;但是好的是.NET提供了注册的方法即RegistrationServices regSrv = new RegistrationServices()下的RegisterAssembly方法和regSrv.UnregisterAssembly(AS)
以下提供一个工具示例,注册示例,卸载示例:
// Copyright 2006 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See use restrictions at /arcgis/developerkit/userestrictions.
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;
using System.Windows.Forms;
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 C#"; //localizable text
base.m_message = "Zoom to the extent of the active layer in the TOC"; //localizable text
base.m_toolTip = "Zoom To Layer C#"; //localizable text
base.m_name = "Developer Samples_Zoom To Layer C#"; //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);
ZoomToLayer2(mxDocument);
//MessageBox.Show("");
}
#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 ZoomToLayer2(ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument)
{
// Get the map
ESRI.ArcGIS.Carto.IMap map = mxDocument.FocusMap;
ESRI.ArcGIS.Carto.IActiveView activeView = (ESRI.ArcGIS.Carto.IActiveView)map; // Explicit Cast
// Get the TOC
ESRI.ArcGIS.ArcMapUI.IContentsView IContentsView = mxDocument.CurrentContentsView;
// Get the selected layer
ESRI.ArcGIS.Carto.ILayer layer = (ESRI.ArcGIS.Carto.ILayer)IContentsView.SelectedItem; // Explicit Cast
// If the selected layer is not an ILayer then quit
if (!(layer is ESRI.ArcGIS.Carto.ILayer))
{
return;
}
// 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)
{
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ((ESRI.ArcGIS.ArcMapUI.IMxDocument)(application.Document)); // Explicit Cast
return mxDocument;
}
#endregion
}
}
注册:
System.IO.DirectoryInfo directInfo;
directInfo = new System.IO.DirectoryInfo(System.Environment.CurrentDirectory);
bool bSuccessed = true;
foreach (System.IO.FileInfo fileInfo in directInfo.GetFiles("*.dll"))
{
System.Reflection.Assembly AS = null;
AS = System.Reflection.Assembly.LoadFrom(fileInfo.FullName);
if (null == AS)
{
continue;
}
try
{
RegistrationServices regSrv = new RegistrationServices();
bool b = regSrv.RegisterAssembly(AS, AssemblyRegistrationFlags.SetCodeBase);
if (b)
{
//MessageBox.Show("成功");
}
else
{
Console.WriteLine(fileInfo.FullName + "注册失败");
bSuccessed = false;
//Console.Read();
//MessageBox.Show("注册失败");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
bSuccessed = false;
}
}
if (!bSuccessed)
{
Console.Read();
}
}
卸载:
System.IO.DirectoryInfo directInfo;
directInfo = new System.IO.DirectoryInfo(System.Environment.CurrentDirectory);
bool bSuccessed = true;
foreach (System.IO.FileInfo fileInfo in directInfo.GetFiles("*.dll"))
{
System.Reflection.Assembly AS = null;
AS = System.Reflection.Assembly.LoadFrom(fileInfo.FullName);
if (null == AS)
{
continue;
}
RegistrationServices regSrv = new RegistrationServices();
try
{
regSrv.UnregisterAssembly(AS);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
bSuccessed = false;
}
}
if (!bSuccessed)
{
Console.Read();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2009-08-19 oracle把一个大表分成几个小表