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
+119 -80
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
{ {
SafeReleaseComObject(activePresentation); if (activePresentation != null && (_pptActivePresentation == null || !PPTROTConnectionHelper.AreComObjectsEqual(_pptActivePresentation, activePresentation)))
{
SafeReleaseComObject(activePresentation);
}
} }
bool isSlideShowActive = false; bool isSlideShowActive = false;
try try
{ {
dynamic app = PPTApplication; dynamic app = PPTApplication;
activePresentation = app.ActivePresentation; if (activePresentation == null)
{
try
{
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))
@@ -380,8 +393,11 @@ namespace Ink_Canvas.Helpers
} }
finally finally
{ {
SafeReleaseComObject(activePresentation); if (activePresentation != null && (_pptActivePresentation == null || !PPTROTConnectionHelper.AreComObjectsEqual(_pptActivePresentation, activePresentation)))
if (!PPTROTConnectionHelper.AreComObjectsEqual(_pptSlideShowWindow, slideShowWindow)) {
SafeReleaseComObject(activePresentation);
}
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,95 +845,115 @@ namespace Ink_Canvas.Helpers
{ {
if (_pptActivePresentation != null) if (_pptActivePresentation != null)
{ {
CurrentPresentation = _pptActivePresentation;
CurrentSlides = CurrentPresentation.Slides;
try try
{ {
var slideCount = CurrentSlides.Count; CurrentPresentation = _pptActivePresentation as Presentation;
if (slideCount > 0) if (CurrentPresentation != null)
{ {
SlidesCount = slideCount; CurrentSlides = CurrentPresentation.Slides;
} }
else else
{ {
SlidesCount = 0; dynamic pres = _pptActivePresentation;
LogHelper.WriteLogToFile("PPT演示文稿页数为0,可能为空演示文稿", LogHelper.LogType.Warning); CurrentSlides = pres.Slides as Slides;
} }
} }
catch (COMException comEx) catch
{ {
var hr = (uint)comEx.HResult; dynamic pres = _pptActivePresentation;
SlidesCount = 0; CurrentSlides = pres.Slides as Slides;
LogHelper.WriteLogToFile($"读取PPT页数失败: {comEx.Message} (HR: 0x{hr:X8})", LogHelper.LogType.Warning);
} }
try if (CurrentSlides != null)
{ {
if (IsInSlideShow && _pptSlideShowWindow != null) try
{ {
try var slideCount = CurrentSlides.Count;
if (slideCount > 0)
{ {
view = _pptSlideShowWindow.View; SlidesCount = slideCount;
if (view != null)
{
dynamic viewObj = view;
CurrentSlide = viewObj.Slide as Slide;
}
} }
catch { } else
}
else
{
dynamic app = PPTApplication;
activeWindow = app.ActiveWindow;
if (activeWindow != null)
{ {
dynamic aw = activeWindow; SlidesCount = 0;
selection = aw.Selection; LogHelper.WriteLogToFile("PPT演示文稿页数为0,可能为空演示文稿", LogHelper.LogType.Warning);
if (selection != null) }
}
catch (COMException comEx)
{
var hr = (uint)comEx.HResult;
SlidesCount = 0;
LogHelper.WriteLogToFile($"读取PPT页数失败: {comEx.Message} (HR: 0x{hr:X8})", LogHelper.LogType.Warning);
}
try
{
if (IsInSlideShow && _pptSlideShowWindow != null)
{
try
{ {
dynamic sel = selection; dynamic ssw = _pptSlideShowWindow;
slideRange = sel.SlideRange; view = ssw.View;
if (slideRange != null) if (view != null)
{ {
dynamic sr = slideRange; dynamic viewObj = view;
int slideNumber = sr.SlideNumber; CurrentSlide = viewObj.Slide as Slide;
if (slideNumber > 0 && slideNumber <= SlidesCount) }
}
catch { }
}
else
{
dynamic app = PPTApplication;
activeWindow = app.ActiveWindow;
if (activeWindow != null)
{
dynamic aw = activeWindow;
selection = aw.Selection;
if (selection != null)
{
dynamic sel = selection;
slideRange = sel.SlideRange;
if (slideRange != null)
{ {
CurrentSlide = CurrentSlides[slideNumber]; dynamic sr = slideRange;
int slideNumber = sr.SlideNumber;
if (slideNumber > 0 && slideNumber <= SlidesCount)
{
CurrentSlide = CurrentSlides[slideNumber];
}
} }
} }
} }
if (CurrentSlide == null && SlidesCount > 0)
{
CurrentSlide = CurrentSlides[1];
}
} }
}
if (CurrentSlide == null && SlidesCount > 0) catch (COMException comEx)
{
var hr = (uint)comEx.HResult;
if (hr != 0x8001010E && hr != 0x80004005)
{
LogHelper.WriteLogToFile($"获取当前幻灯片失败: {comEx.Message}", LogHelper.LogType.Warning);
}
if (SlidesCount > 0)
{ {
CurrentSlide = CurrentSlides[1]; CurrentSlide = CurrentSlides[1];
} }
} }
} }
catch (COMException comEx) else
{ {
var hr = (uint)comEx.HResult; CurrentPresentation = null;
if (hr != 0x8001010E && hr != 0x80004005) CurrentSlides = null;
{ CurrentSlide = null;
LogHelper.WriteLogToFile($"获取当前幻灯片失败: {comEx.Message}", LogHelper.LogType.Warning); SlidesCount = 0;
}
if (SlidesCount > 0)
{
CurrentSlide = CurrentSlides[1];
}
} }
} }
else
{
CurrentPresentation = null;
CurrentSlides = null;
CurrentSlide = null;
SlidesCount = 0;
}
} }
catch (COMException comEx) catch (COMException comEx)
{ {
@@ -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;
// 初始化当前播放页码跟踪 // 初始化当前播放页码跟踪