撤销至c760211

This commit is contained in:
CJK_mkp
2026-03-07 12:15:14 +08:00
committed by GitHub
parent 134c90508d
commit 4f99f11a33
+25 -90
View File
@@ -66,8 +66,7 @@ namespace Ink_Canvas.Helpers
private volatile bool _cachedIsInSlideShow; private volatile bool _cachedIsInSlideShow;
private readonly object _lockObject = new object(); private readonly object _lockObject = new object();
private bool _disposed; private bool _disposed;
private static bool IsPptBusyHResult(uint hr) => hr == 0x80010001 || hr == 0x8001010A; private static bool IsPptBusyHResult(uint hr) => hr == 0x80010001 || hr == 0x8001010A;
private static bool IsNoActivePresentationOrWindowHResult(uint hr) => hr == 0x80048240;
#endregion #endregion
#region Constructor & Initialization #region Constructor & Initialization
@@ -142,13 +141,13 @@ namespace Ink_Canvas.Helpers
private void CheckAndConnectToPPT() private void CheckAndConnectToPPT()
{ {
if (_isModuleUnloading) return; if (_isModuleUnloading) return;
lock (_lockObject) lock (_lockObject)
{ {
try try
{ {
if (_isModuleUnloading) return; if (_isModuleUnloading) return;
// 尝试连接到PowerPoint // 尝试连接到PowerPoint
var pptApp = TryConnectToPowerPoint(); var pptApp = TryConnectToPowerPoint();
if (pptApp == null && IsSupportWPS) if (pptApp == null && IsSupportWPS)
@@ -413,7 +412,7 @@ namespace Ink_Canvas.Helpers
SafeReleaseComObject(CurrentSlide, "CurrentSlide"); SafeReleaseComObject(CurrentSlide, "CurrentSlide");
SafeReleaseComObject(CurrentSlides, "CurrentSlides"); SafeReleaseComObject(CurrentSlides, "CurrentSlides");
SafeReleaseComObject(CurrentPresentation, "CurrentPresentation"); SafeReleaseComObject(CurrentPresentation, "CurrentPresentation");
if (PPTApplication != null && Marshal.IsComObject(PPTApplication)) if (PPTApplication != null && Marshal.IsComObject(PPTApplication))
{ {
try try
@@ -466,13 +465,13 @@ namespace Ink_Canvas.Helpers
_isModuleUnloading = false; _isModuleUnloading = false;
return; return;
} }
GC.Collect(); GC.Collect();
GC.WaitForPendingFinalizers(); GC.WaitForPendingFinalizers();
GC.Collect(); GC.Collect();
Thread.Sleep(1000); Thread.Sleep(1000);
_isModuleUnloading = false; _isModuleUnloading = false;
try try
@@ -486,7 +485,7 @@ namespace Ink_Canvas.Helpers
{ {
LogHelper.WriteLogToFile("PPT联动模块重载时计时器已释放,跳过重启", LogHelper.LogType.Trace); LogHelper.WriteLogToFile("PPT联动模块重载时计时器已释放,跳过重启", LogHelper.LogType.Trace);
} }
LogHelper.WriteLogToFile("PPT联动模块已重新加载", LogHelper.LogType.Trace); LogHelper.WriteLogToFile("PPT联动模块已重新加载", LogHelper.LogType.Trace);
} }
catch (Exception ex) catch (Exception ex)
@@ -538,7 +537,7 @@ namespace Ink_Canvas.Helpers
} }
} }
private void UpdateCurrentPresentationInfo(bool preferSlideShowWindow = false, SlideShowWindow knownSlideShowWindow = null) private void UpdateCurrentPresentationInfo()
{ {
object activePresentation = null; object activePresentation = null;
object slideShowWindows = null; object slideShowWindows = null;
@@ -554,7 +553,7 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
activePresentation = TryGetActivePresentation(knownSlideShowWindow); activePresentation = PPTApplication.ActivePresentation;
if (activePresentation != null) if (activePresentation != null)
{ {
SafeReleaseComObject(CurrentPresentation, "CurrentPresentation"); SafeReleaseComObject(CurrentPresentation, "CurrentPresentation");
@@ -584,30 +583,21 @@ namespace Ink_Canvas.Helpers
try try
{ {
slideShowWindows = PPTApplication.SlideShowWindows; slideShowWindows = PPTApplication.SlideShowWindows;
var shouldUseSlideShowWindow = preferSlideShowWindow || IsInSlideShow; if (IsInSlideShow && slideShowWindows != null)
if (shouldUseSlideShowWindow && slideShowWindows != null)
{ {
if (knownSlideShowWindow != null) dynamic ssw = slideShowWindows;
if (ssw.Count > 0)
{ {
slideShowWindow = knownSlideShowWindow; slideShowWindow = ssw[1];
} if (slideShowWindow != null)
else
{
dynamic ssw = slideShowWindows;
if (ssw.Count > 0)
{ {
slideShowWindow = ssw[1]; dynamic sswObj = slideShowWindow;
} view = sswObj.View;
} if (view != null)
{
if (slideShowWindow != null) dynamic viewObj = view;
{
dynamic sswObj = slideShowWindow;
view = sswObj.View;
if (view != null)
{
dynamic viewObj = view;
CurrentSlide = viewObj.Slide as Slide; CurrentSlide = viewObj.Slide as Slide;
}
} }
} }
} }
@@ -643,7 +633,7 @@ namespace Ink_Canvas.Helpers
catch (COMException comEx) catch (COMException comEx)
{ {
var hr = (uint)comEx.HResult; var hr = (uint)comEx.HResult;
if (hr != 0x8001010E && hr != 0x80004005 && !IsNoActivePresentationOrWindowHResult(hr)) if (hr != 0x8001010E && hr != 0x80004005)
{ {
LogHelper.WriteLogToFile($"获取当前幻灯片失败: {comEx.Message}", LogHelper.LogType.Warning); LogHelper.WriteLogToFile($"获取当前幻灯片失败: {comEx.Message}", LogHelper.LogType.Warning);
} }
@@ -665,7 +655,7 @@ namespace Ink_Canvas.Helpers
catch (COMException comEx) catch (COMException comEx)
{ {
var hr = (uint)comEx.HResult; var hr = (uint)comEx.HResult;
if (hr == 0x8001010E || hr == 0x80004005 || IsNoActivePresentationOrWindowHResult(hr)) if (hr == 0x8001010E || hr == 0x80004005)
{ {
CurrentPresentation = null; CurrentPresentation = null;
CurrentSlides = null; CurrentSlides = null;
@@ -708,61 +698,6 @@ namespace Ink_Canvas.Helpers
} }
} }
} }
private object TryGetActivePresentation(SlideShowWindow knownSlideShowWindow)
{
try
{
return PPTApplication.ActivePresentation;
}
catch (COMException comEx)
{
var hr = (uint)comEx.HResult;
if (!IsNoActivePresentationOrWindowHResult(hr))
{
throw;
}
}
try
{
if (knownSlideShowWindow != null)
{
return knownSlideShowWindow.Presentation;
}
var slideShowWindows = PPTApplication.SlideShowWindows;
if (slideShowWindows == null)
{
return null;
}
dynamic ssw = slideShowWindows;
if (ssw.Count == 0)
{
return null;
}
var slideShowWindow = ssw[1];
if (slideShowWindow == null)
{
return null;
}
dynamic sswObj = slideShowWindow;
return sswObj.Presentation;
}
catch (COMException comEx)
{
var hr = (uint)comEx.HResult;
if (!IsNoActivePresentationOrWindowHResult(hr))
{
throw;
}
}
return null;
}
#endregion #endregion
#region Event Handlers #region Event Handlers
@@ -799,7 +734,7 @@ namespace Ink_Canvas.Helpers
_cachedIsInSlideShow = true; _cachedIsInSlideShow = true;
try try
{ {
UpdateCurrentPresentationInfo(preferSlideShowWindow: true, knownSlideShowWindow: wn); UpdateCurrentPresentationInfo();
SlideShowBegin?.Invoke(wn); SlideShowBegin?.Invoke(wn);
} }
catch (Exception ex) catch (Exception ex)
@@ -812,7 +747,7 @@ namespace Ink_Canvas.Helpers
{ {
try try
{ {
UpdateCurrentPresentationInfo(preferSlideShowWindow: true, knownSlideShowWindow: wn); UpdateCurrentPresentationInfo();
SlideShowNextSlide?.Invoke(wn); SlideShowNextSlide?.Invoke(wn);
} }
catch (Exception ex) catch (Exception ex)