arcgis10 arcmap10插件监控打开和保存文档
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
namespace OpenSaveLogExtensionCS
{
/// <summary>
/// Simple extension that logs message when document is opened and saved
/// </summary>
[Guid("7d868fd7-f986-4347-bc93-b79751e12def")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("OpenSaveLogExtensionCS.LogExtension")]
public class LogExtension : IExtension, IPersistVariant
{
#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);
MxExtension.Register(regKey);
GMxExtensions.Register(regKey);
SxExtensions.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);
MxExtension.Unregister(regKey);
GMxExtensions.Unregister(regKey);
SxExtensions.Unregister(regKey);
}
#endregion
#endregion
private IApplication m_application;
#region "Add Event Wiring for Open Documents"
// Event member variables
private IDocumentEvents_Event m_docEvents;
// Wiring
private void SetUpDocumentEvent(IDocument myDocument)
{
m_docEvents = myDocument as IDocumentEvents_Event;
m_docEvents.OpenDocument += new IDocumentEvents_OpenDocumentEventHandler(OnOpenDocument);
//Optional, new and close document events
m_docEvents.NewDocument += new IDocumentEvents_NewDocumentEventHandler(OnNewDocument);
m_docEvents.CloseDocument += new IDocumentEvents_CloseDocumentEventHandler(OnCloseDocument);
}
void OnOpenDocument()
{
System.Diagnostics.Debug.WriteLine("Open Document", "Sample Extension (C#)");
string logText = "Document '" + m_application.Document.Title + "'"
+ " opened by " + Environment.UserName
+ " at " + DateTime.Now.ToLongTimeString();
LogMessage(logText);
}
void OnCloseDocument()
{
System.Diagnostics.Debug.WriteLine("Close Document", "Sample Extension (C#)");
}
void OnNewDocument()
{
System.Diagnostics.Debug.WriteLine("New Document", "Sample Extension (C#)");
}
#endregion
#region "IExtension Implementations"
public string Name
{
get
{
return "OpenSaveLogExtensionCS";
}
}
public void Shutdown()
{
m_docEvents = null;
m_application = null;
}
public void Startup(ref object initializationData)
{
m_application = initializationData as IApplication;
SetUpDocumentEvent(m_application.Document);
}
#endregion
#region "IPersistVariant Implementations"
public UID ID
{
get
{
UID extUID = new UIDClass();
extUID.Value = GetType().GUID.ToString("B");
return extUID;
}
}
public void Load(IVariantStream Stream)
{
Marshal.ReleaseComObject(Stream);
}
public void Save(IVariantStream Stream)
{
System.Diagnostics.Debug.WriteLine("Save Document", "Sample Extension (C#)");
LogMessage("Document '" + m_application.Document.Title + "'"
+ " saved by " + Environment.UserName
+ " at " + DateTime.Now.ToLongTimeString());
Marshal.ReleaseComObject(Stream);
}
#endregion
private void LogMessage(string message)
{
m_application.StatusBar.set_Message(0, message);
}
}
}
【推荐】国内首个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编程运行原理