improve:PPT联动

This commit is contained in:
2026-01-18 00:10:47 +08:00
parent 89c9c0d9ef
commit 84f026f1dd
2 changed files with 120 additions and 81 deletions
+60 -21
View File
@@ -143,8 +143,8 @@ namespace Ink_Canvas.Helpers
private readonly object _lockObject = new object(); private readonly object _lockObject = new object();
private bool _disposed; private bool _disposed;
private Presentation _pptActivePresentation; private dynamic _pptActivePresentation;
private SlideShowWindow _pptSlideShowWindow; private dynamic _pptSlideShowWindow;
private int _polling = 0; private int _polling = 0;
private bool _forcePolling = false; private bool _forcePolling = false;
private bool _bindingEvents = false; private bool _bindingEvents = false;
@@ -307,15 +307,15 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
Presentation activePresentation = null; dynamic activePresentation = null;
SlideShowWindow slideShowWindow = null; dynamic slideShowWindow = null;
try try
{ {
dynamic app = PPTApplication; dynamic app = PPTApplication;
activePresentation = app.ActivePresentation; activePresentation = app.ActivePresentation;
if (activePresentation != null && !PPTROTConnectionHelper.AreComObjectsEqual(_pptActivePresentation, activePresentation)) if (activePresentation != null && _pptActivePresentation != null && !PPTROTConnectionHelper.AreComObjectsEqual(_pptActivePresentation, activePresentation))
{ {
DisconnectFromPPT(); DisconnectFromPPT();
return; return;
@@ -328,20 +328,32 @@ namespace Ink_Canvas.Helpers
} }
catch (Exception ex) catch (Exception ex)
{ {
LogHelper.WriteLogToFile($"检查演示文稿状态失败: {ex}", LogHelper.LogType.Warning); LogHelper.WriteLogToFile($"检查演示文稿状态失败: {ex.Message},继续使用轮询模式", LogHelper.LogType.Warning);
DisconnectFromPPT(); activePresentation = null;
return;
} }
finally finally
{
if (activePresentation != null && (_pptActivePresentation == null || !PPTROTConnectionHelper.AreComObjectsEqual(_pptActivePresentation, activePresentation)))
{ {
SafeReleaseComObject(activePresentation); SafeReleaseComObject(activePresentation);
} }
}
bool isSlideShowActive = false; bool isSlideShowActive = false;
try try
{ {
dynamic app = PPTApplication; dynamic app = PPTApplication;
if (activePresentation == null)
{
try
{
activePresentation = app.ActivePresentation; activePresentation = app.ActivePresentation;
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"访问ActivePresentation失败: {ex.Message},继续使用轮询模式", LogHelper.LogType.Warning);
}
}
if (activePresentation != null) if (activePresentation != null)
{ {
@@ -356,9 +368,10 @@ namespace Ink_Canvas.Helpers
catch { } catch { }
} }
if (isSlideShowActive) if (isSlideShowActive && activePresentation != null)
{ {
slideShowWindow = activePresentation.SlideShowWindow; dynamic pres = activePresentation;
slideShowWindow = pres.SlideShowWindow;
if (_pptSlideShowWindow == null || !PPTROTConnectionHelper.IsValidSlideShowWindow(_pptSlideShowWindow)) if (_pptSlideShowWindow == null || !PPTROTConnectionHelper.IsValidSlideShowWindow(_pptSlideShowWindow))
{ {
if (!PPTROTConnectionHelper.AreComObjectsEqual(_pptSlideShowWindow, slideShowWindow)) if (!PPTROTConnectionHelper.AreComObjectsEqual(_pptSlideShowWindow, slideShowWindow))
@@ -379,9 +392,12 @@ namespace Ink_Canvas.Helpers
LogHelper.WriteLogToFile($"发现窗口失败: {ex}", LogHelper.LogType.Warning); LogHelper.WriteLogToFile($"发现窗口失败: {ex}", LogHelper.LogType.Warning);
} }
finally finally
{
if (activePresentation != null && (_pptActivePresentation == null || !PPTROTConnectionHelper.AreComObjectsEqual(_pptActivePresentation, activePresentation)))
{ {
SafeReleaseComObject(activePresentation); SafeReleaseComObject(activePresentation);
if (!PPTROTConnectionHelper.AreComObjectsEqual(_pptSlideShowWindow, slideShowWindow)) }
if (slideShowWindow != null && !PPTROTConnectionHelper.AreComObjectsEqual(_pptSlideShowWindow, slideShowWindow))
{ {
SafeReleaseComObject(slideShowWindow); SafeReleaseComObject(slideShowWindow);
} }
@@ -393,7 +409,8 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
slideShowWindow = _pptActivePresentation.SlideShowWindow; dynamic pres = _pptActivePresentation;
slideShowWindow = pres.SlideShowWindow;
int tempTotalPage = -1; int tempTotalPage = -1;
if (slideShowWindow != null) if (slideShowWindow != null)
@@ -828,9 +845,27 @@ namespace Ink_Canvas.Helpers
{ {
if (_pptActivePresentation != null) if (_pptActivePresentation != null)
{ {
CurrentPresentation = _pptActivePresentation; try
{
CurrentPresentation = _pptActivePresentation as Presentation;
if (CurrentPresentation != null)
{
CurrentSlides = CurrentPresentation.Slides; CurrentSlides = CurrentPresentation.Slides;
}
else
{
dynamic pres = _pptActivePresentation;
CurrentSlides = pres.Slides as Slides;
}
}
catch
{
dynamic pres = _pptActivePresentation;
CurrentSlides = pres.Slides as Slides;
}
if (CurrentSlides != null)
{
try try
{ {
var slideCount = CurrentSlides.Count; var slideCount = CurrentSlides.Count;
@@ -857,7 +892,8 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
view = _pptSlideShowWindow.View; dynamic ssw = _pptSlideShowWindow;
view = ssw.View;
if (view != null) if (view != null)
{ {
dynamic viewObj = view; dynamic viewObj = view;
@@ -918,6 +954,7 @@ namespace Ink_Canvas.Helpers
SlidesCount = 0; SlidesCount = 0;
} }
} }
}
catch (COMException comEx) catch (COMException comEx)
{ {
var hr = (uint)comEx.HResult; var hr = (uint)comEx.HResult;
@@ -1412,7 +1449,7 @@ namespace Ink_Canvas.Helpers
/// <summary> /// <summary>
/// 获取当前活跃的演示文稿 /// 获取当前活跃的演示文稿
/// </summary> /// </summary>
public Presentation GetCurrentActivePresentation() public object GetCurrentActivePresentation()
{ {
object slideShowWindows = null; object slideShowWindows = null;
object slideShowWindow = null; object slideShowWindow = null;
@@ -1447,7 +1484,7 @@ namespace Ink_Canvas.Helpers
{ {
dynamic slideObj = slide; dynamic slideObj = slide;
presentation = slideObj.Parent; presentation = slideObj.Parent;
return presentation as Presentation; return presentation;
} }
} }
} }
@@ -1463,7 +1500,7 @@ namespace Ink_Canvas.Helpers
presentation = aw.Presentation; presentation = aw.Presentation;
if (presentation != null) if (presentation != null)
{ {
return presentation as Presentation; return presentation;
} }
} }
@@ -1505,7 +1542,7 @@ namespace Ink_Canvas.Helpers
/// <summary> /// <summary>
/// 获取当前幻灯片编号 /// 获取当前幻灯片编号
/// </summary> /// </summary>
private int GetCurrentSlideIndex(SlideShowWindow slideShowWindow) private int GetCurrentSlideIndex(dynamic slideShowWindow)
{ {
object view = null; object view = null;
object slide = null; object slide = null;
@@ -1513,7 +1550,8 @@ namespace Ink_Canvas.Helpers
try try
{ {
if (slideShowWindow == null) return 0; if (slideShowWindow == null) return 0;
view = slideShowWindow.View; dynamic ssw = slideShowWindow;
view = ssw.View;
if (view != null) if (view != null)
{ {
dynamic viewObj = view; dynamic viewObj = view;
@@ -1533,12 +1571,13 @@ namespace Ink_Canvas.Helpers
} }
} }
private int GetTotalSlideIndex(Presentation presentation) private int GetTotalSlideIndex(dynamic presentation)
{ {
try try
{ {
if (presentation == null) return 0; if (presentation == null) return 0;
return presentation.Slides.Count; dynamic pres = presentation;
return pres.Slides.Count;
} }
catch catch
{ {
+1 -1
View File
@@ -644,7 +644,7 @@ namespace Ink_Canvas
} }
else else
{ {
activePresentation = _pptManager?.GetCurrentActivePresentation(); activePresentation = _pptManager?.GetCurrentActivePresentation() as Presentation;
currentSlide = _pptManager?.GetCurrentSlideNumber() ?? 0; currentSlide = _pptManager?.GetCurrentSlideNumber() ?? 0;
totalSlides = _pptManager?.SlidesCount ?? 0; totalSlides = _pptManager?.SlidesCount ?? 0;
// 初始化当前播放页码跟踪 // 初始化当前播放页码跟踪