From a5ac282ab6bac4de149d40b9c92c66bd194d73b7 Mon Sep 17 00:00:00 2001
From: CJKmkp <2564608840@qq.com>
Date: Sat, 13 Sep 2025 18:13:31 +0800
Subject: [PATCH] add:issue #190
---
Ink Canvas/Helpers/MultiPPTInkManager.cs | 56 +++++++++++++++++++
Ink Canvas/Helpers/PPTInkManager.cs | 37 +++++++++++-
Ink Canvas/MainWindow_cs/MW_PPT.cs | 1 +
.../MainWindow_cs/MW_Save&OpenStrokes.cs | 2 +-
4 files changed, 94 insertions(+), 2 deletions(-)
diff --git a/Ink Canvas/Helpers/MultiPPTInkManager.cs b/Ink Canvas/Helpers/MultiPPTInkManager.cs
index 24e6f271..92588601 100644
--- a/Ink Canvas/Helpers/MultiPPTInkManager.cs
+++ b/Ink Canvas/Helpers/MultiPPTInkManager.cs
@@ -17,6 +17,7 @@ namespace Ink_Canvas.Helpers
#region Properties
public bool IsAutoSaveEnabled { get; set; } = true;
public string AutoSaveLocation { get; set; } = "";
+ public PPTManager PPTManager { get; set; }
#endregion
#region Private Fields
@@ -101,6 +102,23 @@ namespace Ink_Canvas.Helpers
if (_presentationManagers.ContainsKey(presentationId))
{
+ // 如果切换的是不同的演示文稿,先保存当前活跃演示文稿的墨迹
+ if (!string.IsNullOrEmpty(_currentActivePresentationId) &&
+ _currentActivePresentationId != presentationId)
+ {
+ var currentManager = GetCurrentManager();
+ if (currentManager != null)
+ {
+ // 获取当前活跃的演示文稿并保存墨迹
+ var currentPresentation = GetCurrentActivePresentation();
+ if (currentPresentation != null)
+ {
+ currentManager.SaveAllStrokesToFile(currentPresentation);
+ LogHelper.WriteLogToFile($"已保存当前演示文稿墨迹: {currentPresentation.Name}", LogHelper.LogType.Trace);
+ }
+ }
+ }
+
_currentActivePresentationId = presentationId;
// 更新最后访问时间
@@ -151,6 +169,30 @@ namespace Ink_Canvas.Helpers
}
}
+ ///
+ /// 强制保存指定页面的墨迹(忽略锁定状态)
+ ///
+ public void ForceSaveSlideStrokes(int slideIndex, StrokeCollection strokes)
+ {
+ if (slideIndex <= 0 || strokes == null) return;
+
+ lock (_lockObject)
+ {
+ try
+ {
+ var manager = GetCurrentManager();
+ if (manager != null)
+ {
+ manager.ForceSaveSlideStrokes(slideIndex, strokes);
+ }
+ }
+ catch (Exception ex)
+ {
+ LogHelper.WriteLogToFile($"强制保存页面墨迹失败: {ex}", LogHelper.LogType.Error);
+ }
+ }
+ }
+
///
/// 加载指定页面的墨迹
///
@@ -459,6 +501,20 @@ namespace Ink_Canvas.Helpers
return _presentationManagers[_currentActivePresentationId];
}
+ private Presentation GetCurrentActivePresentation()
+ {
+ try
+ {
+ // 通过PPTManager获取当前活跃的演示文稿
+ return PPTManager?.GetCurrentActivePresentation();
+ }
+ catch (Exception ex)
+ {
+ LogHelper.WriteLogToFile($"获取当前活跃演示文稿失败: {ex}", LogHelper.LogType.Error);
+ return null;
+ }
+ }
+
private string GeneratePresentationId(Presentation presentation)
{
try
diff --git a/Ink Canvas/Helpers/PPTInkManager.cs b/Ink Canvas/Helpers/PPTInkManager.cs
index 436f5d71..5e14e3f8 100644
--- a/Ink Canvas/Helpers/PPTInkManager.cs
+++ b/Ink Canvas/Helpers/PPTInkManager.cs
@@ -122,6 +122,37 @@ namespace Ink_Canvas.Helpers
}
}
+ ///
+ /// 强制保存指定页面的墨迹(忽略锁定状态)
+ ///
+ public void ForceSaveSlideStrokes(int slideIndex, StrokeCollection strokes)
+ {
+ if (slideIndex <= 0 || strokes == null) return;
+
+ lock (_lockObject)
+ {
+ try
+ {
+ if (slideIndex < _memoryStreams.Length)
+ {
+ var ms = new MemoryStream();
+ strokes.Save(ms);
+ ms.Position = 0;
+
+ // 释放旧的内存流
+ _memoryStreams[slideIndex]?.Dispose();
+ _memoryStreams[slideIndex] = ms;
+
+ LogHelper.WriteLogToFile($"已强制保存第{slideIndex}页墨迹,大小: {ms.Length} bytes", LogHelper.LogType.Trace);
+ }
+ }
+ catch (Exception ex)
+ {
+ LogHelper.WriteLogToFile($"强制保存第{slideIndex}页墨迹失败: {ex}", LogHelper.LogType.Error);
+ }
+ }
+ }
+
///
/// 加载指定页面的墨迹
///
@@ -162,7 +193,7 @@ namespace Ink_Canvas.Helpers
// 如果有当前墨迹,先保存到正确的页面
if (currentStrokes != null && currentStrokes.Count > 0)
{
- // 确定要保存的页面索引
+ // 使用当前锁定的页面索引,如果没有锁定则使用传入的页面索引
int saveToSlideIndex = _lockedSlideIndex > 0 ? _lockedSlideIndex : slideIndex;
// 确保页面索引有效
@@ -171,6 +202,10 @@ namespace Ink_Canvas.Helpers
SaveCurrentSlideStrokes(saveToSlideIndex, currentStrokes);
LogHelper.WriteLogToFile($"已保存第{saveToSlideIndex}页墨迹,墨迹数量: {currentStrokes.Count}", LogHelper.LogType.Trace);
}
+ else
+ {
+ LogHelper.WriteLogToFile($"页面索引无效,无法保存墨迹: {saveToSlideIndex}", LogHelper.LogType.Warning);
+ }
}
// 设置墨迹锁定
diff --git a/Ink Canvas/MainWindow_cs/MW_PPT.cs b/Ink Canvas/MainWindow_cs/MW_PPT.cs
index ea4a3876..8a43a49e 100644
--- a/Ink Canvas/MainWindow_cs/MW_PPT.cs
+++ b/Ink Canvas/MainWindow_cs/MW_PPT.cs
@@ -130,6 +130,7 @@ namespace Ink_Canvas
_multiPPTInkManager = new MultiPPTInkManager();
_multiPPTInkManager.IsAutoSaveEnabled = Settings.PowerPointSettings.IsAutoSaveStrokesInPowerPoint;
_multiPPTInkManager.AutoSaveLocation = Settings.Automation.AutoSavedStrokesLocation;
+ _multiPPTInkManager.PPTManager = _pptManager;
// 初始化UI管理器
_pptUIManager = new PPTUIManager(this);
diff --git a/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs b/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs
index a3e252e1..b0aae681 100644
--- a/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs
+++ b/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs
@@ -542,7 +542,7 @@ namespace Ink_Canvas
var strokes = new StrokeCollection(fs);
if (strokes.Count > 0)
{
- _multiPPTInkManager?.SaveCurrentSlideStrokes(pageNumber, strokes);
+ _multiPPTInkManager?.ForceSaveSlideStrokes(pageNumber, strokes);
}
}
}