121 lines
3.8 KiB
C#
121 lines
3.8 KiB
C#
using System;
|
||
using System.Runtime.InteropServices;
|
||
using Extensibility;
|
||
using Microsoft.Office.Core;
|
||
using Word = Microsoft.Office.Interop.Word;
|
||
using System.Windows.Forms;
|
||
|
||
namespace WordAddIn
|
||
{
|
||
/// <summary>
|
||
/// WPS Office 兼容入口类。
|
||
/// 实现标准的 COM Add-in 接口 (IDTExtensibility2),以便 WPS 可以加载此插件。
|
||
/// 同时继承自 AiRibbon,复用 Ribbon UI 和业务逻辑。
|
||
/// </summary>
|
||
[ComVisible(true)]
|
||
[Guid("D8E52004-9D68-4509-8A11-66774D4F0523")] // 固定的 COM GUID
|
||
[ProgId("WordAddIn.WpsAddIn")] // 注册表中的 ProgID
|
||
public class WpsAddIn : AiRibbon, IDTExtensibility2, IRibbonExtensibility
|
||
{
|
||
private Word.Application _application;
|
||
private object _addInInstance;
|
||
|
||
private void Log(string message)
|
||
{
|
||
try
|
||
{
|
||
string logPath = @"C:\Temp\WpsAddIn.log";
|
||
string dir = System.IO.Path.GetDirectoryName(logPath);
|
||
if (!System.IO.Directory.Exists(dir))
|
||
{
|
||
System.IO.Directory.CreateDirectory(dir);
|
||
}
|
||
System.IO.File.AppendAllText(logPath, $"{DateTime.Now}: {message}{Environment.NewLine}");
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
#region IDTExtensibility2 成员
|
||
|
||
/// <summary>
|
||
/// 插件加载时调用
|
||
/// </summary>
|
||
public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
|
||
{
|
||
Log("OnConnection called");
|
||
try
|
||
{
|
||
_application = Application as Word.Application;
|
||
_addInInstance = AddInInst;
|
||
|
||
if (_application == null)
|
||
{
|
||
Log("Application is null in OnConnection");
|
||
}
|
||
else
|
||
{
|
||
Log("Application obtained successfully");
|
||
}
|
||
|
||
// 初始化全局上下文,确保 AiRibbon 中的功能可以正常获取 Application 对象
|
||
AddInContext.CurrentApplication = _application;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log($"Error in OnConnection: {ex.Message}");
|
||
MessageBox.Show("WPS 插件加载失败: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 插件卸载时调用
|
||
/// </summary>
|
||
public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
|
||
{
|
||
Log("OnDisconnection called");
|
||
_application = null;
|
||
_addInInstance = null;
|
||
// 清理上下文
|
||
if (AddInContext.CurrentApplication == _application)
|
||
{
|
||
AddInContext.CurrentApplication = null;
|
||
}
|
||
}
|
||
|
||
public void OnAddInsUpdate(ref Array custom)
|
||
{
|
||
Log("OnAddInsUpdate called");
|
||
}
|
||
|
||
public void OnStartupComplete(ref Array custom)
|
||
{
|
||
Log("OnStartupComplete called");
|
||
}
|
||
|
||
public void OnBeginShutdown(ref Array custom)
|
||
{
|
||
Log("OnBeginShutdown called");
|
||
}
|
||
|
||
#endregion
|
||
|
||
// 显式实现 IRibbonExtensibility,虽然基类实现了,但为了确保 COM 可见性和调试,这里显式重写/覆盖
|
||
public new string GetCustomUI(string RibbonID)
|
||
{
|
||
Log($"GetCustomUI called for RibbonID: {RibbonID}");
|
||
try
|
||
{
|
||
// 调用基类的方法获取 XML
|
||
string xml = base.GetCustomUI(RibbonID);
|
||
Log("GetCustomUI XML retrieved successfully");
|
||
return xml;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log($"Error in GetCustomUI: {ex.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
}
|