improve:PPT联动

This commit is contained in:
2026-01-18 00:58:58 +08:00
parent ed03d40ff3
commit 60c341927f
+18 -23
View File
@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@@ -258,18 +259,9 @@ namespace Ink_Canvas.Helpers
try try
{ {
Microsoft.Office.Interop.PowerPoint.Application pptApp = bestApp as Microsoft.Office.Interop.PowerPoint.Application; LogHelper.WriteLogToFile("使用dynamic类型连接", LogHelper.LogType.Trace);
if (pptApp != null) PPTApplication = bestApp;
{ ConnectToPPT(null);
LogHelper.WriteLogToFile("成功转换为强类型Application,开始连接", LogHelper.LogType.Trace);
ConnectToPPT(pptApp);
}
else
{
LogHelper.WriteLogToFile("无法转换为强类型Application,使用dynamic类型连接", LogHelper.LogType.Trace);
PPTApplication = bestApp;
ConnectToPPT(null);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -528,7 +520,7 @@ namespace Ink_Canvas.Helpers
} }
} }
private void ConnectToPPT(Microsoft.Office.Interop.PowerPoint.Application pptApp) private void ConnectToPPT(object pptApp)
{ {
try try
{ {
@@ -627,7 +619,7 @@ namespace Ink_Canvas.Helpers
{ {
_bindingEvents = false; _bindingEvents = false;
_forcePolling = true; _forcePolling = true;
LogHelper.WriteLogToFile("转换Application接口失败,无法注册事件", LogHelper.LogType.Warning); LogHelper.WriteLogToFile("无法转换为强类型Application,使用轮询模式", LogHelper.LogType.Trace);
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -637,9 +629,6 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile($"无法注册事件: {ex.Message}", LogHelper.LogType.Warning); LogHelper.WriteLogToFile($"无法注册事件: {ex.Message}", LogHelper.LogType.Warning);
} }
_bindingEvents = false;
_forcePolling = true;
if (_pptActivePresentation != null) if (_pptActivePresentation != null)
{ {
UpdateCurrentPresentationInfo(); UpdateCurrentPresentationInfo();
@@ -2095,13 +2084,19 @@ namespace Ink_Canvas.Helpers
} }
// 第三步:释放 pptApp 对象(PPTApplication // 第三步:释放 pptApp 对象(PPTApplication
Microsoft.Office.Interop.PowerPoint.Application pptApp = PPTApplication as Microsoft.Office.Interop.PowerPoint.Application; if (PPTApplication != null)
if (pptApp != null)
{ {
Marshal.ReleaseComObject(pptApp); try
pptApp = null; {
PPTApplication = null; Marshal.ReleaseComObject(PPTApplication);
LogHelper.WriteLogToFile("已释放pptApp对象", LogHelper.LogType.Trace); PPTApplication = null;
LogHelper.WriteLogToFile("已释放pptApp对象", LogHelper.LogType.Trace);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放pptApp对象失败: {ex.Message}", LogHelper.LogType.Trace);
PPTApplication = null;
}
} }
// 第四步:强制垃圾回收及等待终结器执行 // 第四步:强制垃圾回收及等待终结器执行