diff --git a/Ink Canvas/Helpers/MultiPPTInkManager.cs b/Ink Canvas/Helpers/MultiPPTInkManager.cs deleted file mode 100644 index 02d70da2..00000000 --- a/Ink Canvas/Helpers/MultiPPTInkManager.cs +++ /dev/null @@ -1,813 +0,0 @@ -using Microsoft.Office.Interop.PowerPoint; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Security.Cryptography; -using System.Text; -using System.Windows.Ink; - -namespace Ink_Canvas.Helpers -{ - /// - /// 多PPT墨迹管理器 - 支持多个PPT窗口分别管理墨迹 - /// - public class MultiPPTInkManager : IDisposable - { - #region Properties - public bool IsAutoSaveEnabled { get; set; } = true; - public string AutoSaveLocation { get; set; } = ""; - public PPTManager PPTManager { get; set; } - #endregion - - #region Private Fields - private readonly Dictionary _presentationManagers; - private readonly Dictionary _presentationInfos; - private readonly object _lockObject = new object(); - private bool _disposed; - private string _currentActivePresentationId = ""; - - // 墨迹备份机制 - private readonly Dictionary> _strokeBackups; - private DateTime _lastBackupTime = DateTime.MinValue; - private const int BackupIntervalMinutes = 2; // 每2分钟备份一次 - #endregion - - #region Constructor - public MultiPPTInkManager() - { - _presentationManagers = new Dictionary(); - _presentationInfos = new Dictionary(); - _strokeBackups = new Dictionary>(); - } - #endregion - - #region Public Methods - /// - /// 初始化新的演示文稿 - /// - public void InitializePresentation(Presentation presentation) - { - if (presentation == null) return; - - lock (_lockObject) - { - try - { - var presentationId = GeneratePresentationId(presentation); - - // 如果已存在该演示文稿的管理器,先清理 - if (_presentationManagers.ContainsKey(presentationId)) - { - _presentationManagers[presentationId].Dispose(); - _presentationManagers.Remove(presentationId); - } - - // 创建新的墨迹管理器 - var inkManager = new PPTInkManager(); - inkManager.IsAutoSaveEnabled = IsAutoSaveEnabled; - inkManager.AutoSaveLocation = AutoSaveLocation; - inkManager.InitializePresentation(presentation); - - // 保存管理器和演示文稿信息 - _presentationManagers[presentationId] = inkManager; - _presentationInfos[presentationId] = new PresentationInfo - { - Id = presentationId, - Name = presentation.Name, - FullName = presentation.FullName, - SlideCount = presentation.Slides.Count, - CreatedTime = DateTime.Now, - LastAccessTime = DateTime.Now - }; - - // 设置为当前活跃的演示文稿 - _currentActivePresentationId = presentationId; - - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"初始化多PPT墨迹管理失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 切换到指定的演示文稿 - /// - public bool SwitchToPresentation(Presentation presentation) - { - if (presentation == null) return false; - - lock (_lockObject) - { - try - { - var presentationId = GeneratePresentationId(presentation); - - if (_presentationManagers.ContainsKey(presentationId)) - { - // 如果切换的是不同的演示文稿,先保存当前活跃演示文稿的墨迹 - if (!string.IsNullOrEmpty(_currentActivePresentationId) && - _currentActivePresentationId != presentationId) - { - var currentManager = GetCurrentManager(); - if (currentManager != null) - { - // 获取当前活跃的演示文稿并保存墨迹 - var currentPresentation = GetCurrentActivePresentation(); - if (currentPresentation != null) - { - try - { - currentManager.SaveAllStrokesToFile(currentPresentation); - LogHelper.WriteLogToFile($"已保存当前演示文稿墨迹: {currentPresentation.Name}", LogHelper.LogType.Trace); - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"保存当前演示文稿墨迹失败: {ex}", LogHelper.LogType.Error); - } - } - } - } - - _currentActivePresentationId = presentationId; - - // 更新最后访问时间 - if (_presentationInfos.ContainsKey(presentationId)) - { - _presentationInfos[presentationId].LastAccessTime = DateTime.Now; - } - - if (_currentActivePresentationId != presentationId) - { - LogHelper.WriteLogToFile($"已切换到演示文稿: {presentation.Name}", LogHelper.LogType.Trace); - } - return true; - } - else - { - // 如果不存在,尝试初始化 - InitializePresentation(presentation); - return true; - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"切换到演示文稿失败: {ex}", LogHelper.LogType.Error); - return false; - } - } - } - - /// - /// 保存当前页面的墨迹 - /// - public void SaveCurrentSlideStrokes(int slideIndex, StrokeCollection strokes) - { - if (slideIndex <= 0 || strokes == null) return; - - lock (_lockObject) - { - try - { - var manager = GetCurrentManager(); - if (manager != null) - { - // 保存到管理器 - manager.SaveCurrentSlideStrokes(slideIndex, strokes); - - // 只有在保存成功后才创建备份 - if (!string.IsNullOrEmpty(_currentActivePresentationId)) - { - CreateStrokeBackup(_currentActivePresentationId, slideIndex, strokes); - } - - // 检查是否需要执行定期备份 - CheckAndPerformBackup(); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"保存当前页面墨迹失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 强制保存指定页面的墨迹(忽略锁定状态) - /// - 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); - } - } - } - - /// - /// 加载指定页面的墨迹 - /// - public StrokeCollection LoadSlideStrokes(int slideIndex) - { - if (slideIndex <= 0) return new StrokeCollection(); - - lock (_lockObject) - { - try - { - var manager = GetCurrentManager(); - if (manager != null) - { - var strokes = manager.LoadSlideStrokes(slideIndex); - - // 如果从管理器加载失败,尝试从备份恢复 - if (strokes == null || strokes.Count == 0) - { - if (!string.IsNullOrEmpty(_currentActivePresentationId)) - { - strokes = RestoreStrokeFromBackup(_currentActivePresentationId, slideIndex); - } - } - - return strokes ?? new StrokeCollection(); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"加载页面墨迹失败: {ex}", LogHelper.LogType.Error); - - // 尝试从备份恢复 - if (!string.IsNullOrEmpty(_currentActivePresentationId)) - { - return RestoreStrokeFromBackup(_currentActivePresentationId, slideIndex); - } - } - } - - return new StrokeCollection(); - } - - /// - /// 切换到指定页面并加载墨迹 - /// - public StrokeCollection SwitchToSlide(int slideIndex, StrokeCollection currentStrokes = null) - { - lock (_lockObject) - { - try - { - var manager = GetCurrentManager(); - if (manager != null) - { - return manager.SwitchToSlide(slideIndex, currentStrokes); - } - else - { - LogHelper.WriteLogToFile($"无法获取当前墨迹管理器,页面切换失败: {slideIndex}", LogHelper.LogType.Warning); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"切换页面墨迹失败: {ex}", LogHelper.LogType.Error); - } - } - - return new StrokeCollection(); - } - - /// - /// 保存所有墨迹到文件 - /// - public void SaveAllStrokesToFile(Presentation presentation) - { - if (!IsAutoSaveEnabled || string.IsNullOrEmpty(AutoSaveLocation) || presentation == null) return; - - lock (_lockObject) - { - try - { - var presentationId = GeneratePresentationId(presentation); - if (_presentationManagers.ContainsKey(presentationId)) - { - _presentationManagers[presentationId].SaveAllStrokesToFile(presentation); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"保存所有墨迹到文件失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 从文件加载已保存的墨迹 - /// - public void LoadSavedStrokes(Presentation presentation) - { - if (!IsAutoSaveEnabled || string.IsNullOrEmpty(AutoSaveLocation) || presentation == null) return; - - lock (_lockObject) - { - try - { - var presentationId = GeneratePresentationId(presentation); - if (_presentationManagers.ContainsKey(presentationId)) - { - _presentationManagers[presentationId].LoadSavedStrokes(); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"从文件加载墨迹失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 清除指定演示文稿的所有墨迹 - /// - public void ClearPresentationStrokes(Presentation presentation) - { - if (presentation == null) return; - - lock (_lockObject) - { - try - { - var presentationId = GeneratePresentationId(presentation); - if (_presentationManagers.ContainsKey(presentationId)) - { - _presentationManagers[presentationId].ClearAllStrokes(); - LogHelper.WriteLogToFile($"已清除演示文稿墨迹: {presentation.Name}", LogHelper.LogType.Trace); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"清除演示文稿墨迹失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 清除所有演示文稿的墨迹 - /// - public void ClearAllStrokes() - { - lock (_lockObject) - { - try - { - foreach (var manager in _presentationManagers.Values) - { - manager?.ClearAllStrokes(); - } - LogHelper.WriteLogToFile("已清除所有演示文稿墨迹", LogHelper.LogType.Trace); - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"清除所有墨迹失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 翻页后锁定墨迹写入 - /// - public void LockInkForSlide(int slideIndex) - { - lock (_lockObject) - { - try - { - var manager = GetCurrentManager(); - if (manager != null) - { - manager.LockInkForSlide(slideIndex); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"锁定墨迹写入失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 检查是否可以写入墨迹 - /// - public bool CanWriteInk(int currentSlideIndex) - { - lock (_lockObject) - { - try - { - var manager = GetCurrentManager(); - if (manager != null) - { - return manager.CanWriteInk(currentSlideIndex); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"检查墨迹写入权限失败: {ex}", LogHelper.LogType.Error); - } - } - - return false; - } - - /// - /// 重置当前演示文稿的墨迹锁定状态 - /// - public void ResetCurrentPresentationLockState() - { - lock (_lockObject) - { - try - { - var manager = GetCurrentManager(); - if (manager != null) - { - manager.ResetLockState(); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"重置墨迹锁定状态失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 移除演示文稿管理器 - /// - public void RemovePresentation(Presentation presentation) - { - if (presentation == null) return; - - lock (_lockObject) - { - try - { - var presentationId = GeneratePresentationId(presentation); - - if (_presentationManagers.ContainsKey(presentationId)) - { - // 保存墨迹到文件 - _presentationManagers[presentationId].SaveAllStrokesToFile(presentation); - - // 释放资源 - _presentationManagers[presentationId].Dispose(); - _presentationManagers.Remove(presentationId); - } - - if (_presentationInfos.ContainsKey(presentationId)) - { - _presentationInfos.Remove(presentationId); - } - - // 如果移除的是当前活跃的演示文稿,重置活跃ID - if (_currentActivePresentationId == presentationId) - { - _currentActivePresentationId = ""; - } - - } - catch (COMException comEx) - { - var hr = (uint)comEx.HResult; - if (hr == 0x8001010E || hr == 0x80004005 || hr == 0x800706BA || hr == 0x800706BE || hr == 0x80048010) - { - } - } - catch (Exception) - { - } - } - } - - /// - /// 获取当前管理的演示文稿数量 - /// - public int GetPresentationCount() - { - lock (_lockObject) - { - return _presentationManagers.Count; - } - } - - /// - /// 获取所有演示文稿信息 - /// - public List GetAllPresentationInfos() - { - lock (_lockObject) - { - return _presentationInfos.Values.ToList(); - } - } - - /// - /// 清理长时间未访问的演示文稿管理器 - /// - public void CleanupInactivePresentations(TimeSpan inactiveThreshold) - { - lock (_lockObject) - { - try - { - var inactiveIds = new List(); - var cutoffTime = DateTime.Now - inactiveThreshold; - - foreach (var info in _presentationInfos.Values) - { - if (info.LastAccessTime < cutoffTime && info.Id != _currentActivePresentationId) - { - inactiveIds.Add(info.Id); - } - } - - foreach (var id in inactiveIds) - { - if (_presentationManagers.ContainsKey(id)) - { - _presentationManagers[id].Dispose(); - _presentationManagers.Remove(id); - } - _presentationInfos.Remove(id); - - // 清理备份数据 - if (_strokeBackups.ContainsKey(id)) - { - _strokeBackups.Remove(id); - } - - LogHelper.WriteLogToFile($"已清理非活跃演示文稿: {id}", LogHelper.LogType.Trace); - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"清理非活跃演示文稿失败: {ex}", LogHelper.LogType.Error); - } - } - } - - /// - /// 创建墨迹备份 - /// - private void CreateStrokeBackup(string presentationId, int slideIndex, StrokeCollection strokes) - { - try - { - if (strokes == null || strokes.Count == 0) return; - - if (!_strokeBackups.ContainsKey(presentationId)) - { - _strokeBackups[presentationId] = new Dictionary(); - } - - // 释放旧的备份 - if (_strokeBackups[presentationId].ContainsKey(slideIndex)) - { - _strokeBackups[presentationId][slideIndex] = null; - } - - // 创建新的备份 - _strokeBackups[presentationId][slideIndex] = strokes.Clone(); - - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"创建墨迹备份失败: {ex}", LogHelper.LogType.Error); - } - } - - /// - /// 从备份恢复墨迹 - /// - private StrokeCollection RestoreStrokeFromBackup(string presentationId, int slideIndex) - { - try - { - if (_strokeBackups.ContainsKey(presentationId) && - _strokeBackups[presentationId].ContainsKey(slideIndex)) - { - var backup = _strokeBackups[presentationId][slideIndex]; - if (backup != null) - { - LogHelper.WriteLogToFile($"从备份恢复第{slideIndex}页墨迹", LogHelper.LogType.Trace); - return backup.Clone(); - } - } - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"从备份恢复墨迹失败: {ex}", LogHelper.LogType.Error); - } - - return new StrokeCollection(); - } - - /// - /// 检查并执行定期备份 - /// - private void CheckAndPerformBackup() - { - try - { - var now = DateTime.Now; - - // 检查是否需要执行备份 - if (now - _lastBackupTime < TimeSpan.FromMinutes(BackupIntervalMinutes)) - { - return; - } - - // 备份当前活跃演示文稿的所有墨迹 - if (!string.IsNullOrEmpty(_currentActivePresentationId) && - _presentationManagers.ContainsKey(_currentActivePresentationId)) - { - var manager = _presentationManagers[_currentActivePresentationId]; - if (manager != null) - { - // 这里可以添加更详细的备份逻辑 - } - } - - _lastBackupTime = now; - } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"定期备份检查失败: {ex}", LogHelper.LogType.Error); - } - } - #endregion - - #region Private Methods - private PPTInkManager GetCurrentManager() - { - if (string.IsNullOrEmpty(_currentActivePresentationId) || - !_presentationManagers.ContainsKey(_currentActivePresentationId)) - { - return null; - } - - 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 - { - // 检查COM对象是否仍然有效 - if (presentation == null) - { - return $"invalid_{DateTime.Now.Ticks}"; - } - - var presentationPath = presentation.FullName; - var fileHash = GetFileHash(presentationPath); - var processId = GetProcessId(presentation); - return $"{presentation.Name}_{presentation.Slides.Count}_{fileHash}_{processId}"; - } - catch (COMException comEx) - { - var hr = (uint)comEx.HResult; - if (hr == 0x8001010E || hr == 0x80004005 || hr == 0x800706BA || hr == 0x800706BE || hr == 0x80048010) - { - return $"disconnected_{DateTime.Now.Ticks}"; - } - return $"error_{DateTime.Now.Ticks}"; - } - catch (Exception) - { - return $"unknown_{DateTime.Now.Ticks}"; - } - } - - private string GetFileHash(string filePath) - { - try - { - if (string.IsNullOrEmpty(filePath)) return "unknown"; - - using (var md5 = MD5.Create()) - { - byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(filePath)); - return BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, 8); - } - } - catch (Exception) - { - // 所有异常都静默处理,避免日志噪音 - return "error"; - } - } - - private string GetProcessId(Presentation presentation) - { - try - { - // 尝试获取PowerPoint应用程序的进程ID - if (presentation.Application != null) - { - // 通过COM对象获取进程信息 - var hwnd = presentation.Application.HWND; - if (hwnd != 0) - { - return hwnd.ToString(); - } - } - return "unknown"; - } - catch (COMException comEx) - { - // COM对象已失效,这是正常情况,完全静默处理 - var hr = (uint)comEx.HResult; - if (hr == 0x8001010E || hr == 0x80004005 || hr == 0x800706BA || hr == 0x800706BE || hr == 0x80048010) - { - return "disconnected"; - } - return "error"; - } - catch (Exception) - { - return "error"; - } - } - #endregion - - #region Dispose - public void Dispose() - { - if (!_disposed) - { - lock (_lockObject) - { - // 释放所有管理器 - foreach (var manager in _presentationManagers.Values) - { - manager?.Dispose(); - } - _presentationManagers.Clear(); - _presentationInfos.Clear(); - - // 清理备份数据 - foreach (var backupDict in _strokeBackups.Values) - { - foreach (var backup in backupDict.Values) - { - backup?.Clear(); - } - backupDict.Clear(); - } - _strokeBackups.Clear(); - } - _disposed = true; - } - } - #endregion - } - - /// - /// 演示文稿信息 - /// - public class PresentationInfo - { - public string Id { get; set; } - public string Name { get; set; } - public string FullName { get; set; } - public int SlideCount { get; set; } - public DateTime CreatedTime { get; set; } - public DateTime LastAccessTime { get; set; } - } -} diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index bd52a3b6..b08d613f 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -325,13 +325,11 @@ namespace Ink_Canvas { if (gest.ApplicationGesture == ApplicationGesture.Left) { - // 直接发送翻页请求到PPT放映软件 - SendKeyToPPTSlideShow(false); // 下一页 + BtnPPTSlidesDown_Click(null, null); // 下一页 } if (gest.ApplicationGesture == ApplicationGesture.Right) { - // 直接发送翻页请求到PPT放映软件 - SendKeyToPPTSlideShow(true); // 上一页 + BtnPPTSlidesUp_Click(null, null); // 上一页 } } } @@ -2346,47 +2344,6 @@ namespace Ink_Canvas } #endregion - #region PPT翻页直接传递 - /// - /// 直接发送翻页请求到PPT放映软件,让PPT软件处理翻页 - /// - /// 是否为上一页 - private void SendKeyToPPTSlideShow(bool isPrevious) - { - try - { - // 查找PPT放映窗口并发送按键 - var pptWindows = Process.GetProcessesByName("POWERPNT"); - var wpsWindows = Process.GetProcessesByName("wpp"); - - foreach (var process in pptWindows.Concat(wpsWindows)) - { - if (process.MainWindowHandle != IntPtr.Zero) - { - // 激活PPT窗口 - SetForegroundWindow(process.MainWindowHandle); - - // 发送翻页按键消息 - int keyCode = isPrevious ? 0x21 : 0x22; // VK_PRIOR : VK_NEXT - - // 发送按键按下和释放消息 - PostMessage(process.MainWindowHandle, 0x0100, (IntPtr)keyCode, IntPtr.Zero); // WM_KEYDOWN - PostMessage(process.MainWindowHandle, 0x0101, (IntPtr)keyCode, IntPtr.Zero); // WM_KEYUP - - break; - } - } - } - catch (Exception) - { - // 如果直接发送失败,回退到原来的方法 - if (isPrevious) - BtnPPTSlidesUp_Click(BtnPPTSlidesUp, null); - else - BtnPPTSlidesDown_Click(BtnPPTSlidesDown, null); - } - } - #endregion /// /// 初始化文件关联状态显示 diff --git a/Ink Canvas/MainWindow_cs/MW_Hotkeys.cs b/Ink Canvas/MainWindow_cs/MW_Hotkeys.cs index 0d585128..e14f5e92 100644 --- a/Ink Canvas/MainWindow_cs/MW_Hotkeys.cs +++ b/Ink Canvas/MainWindow_cs/MW_Hotkeys.cs @@ -7,49 +7,31 @@ namespace Ink_Canvas { private void Window_MouseWheel(object sender, MouseWheelEventArgs e) { - // 只有在PPT放映模式下才响应鼠标滚轮翻页 - if (StackPanelPPTControls.Visibility != Visibility.Visible || - currentMode != 0 || - BtnPPTSlideShowEnd.Visibility != Visibility.Visible || - PPTManager?.IsInSlideShow != true) return; - - // 直接发送翻页请求到PPT放映软件,不通过软件处理 + if (BtnPPTSlideShowEnd.Visibility != Visibility.Visible || currentMode != 0) return; if (e.Delta >= 120) { - // 上一页 - 发送PageUp键到PPT放映窗口 - SendKeyToPPTSlideShow(true); + BtnPPTSlidesUp_Click(null, null); } else if (e.Delta <= -120) { - // 下一页 - 发送PageDown键到PPT放映窗口 - SendKeyToPPTSlideShow(false); + BtnPPTSlidesDown_Click(null, null); } } private void Main_Grid_PreviewKeyDown(object sender, KeyEventArgs e) { - // 只有在PPT放映模式下才响应键盘翻页快捷键 - if (StackPanelPPTControls.Visibility != Visibility.Visible || - currentMode != 0 || - BtnPPTSlideShowEnd.Visibility != Visibility.Visible || - PPTManager?.IsInSlideShow != true) return; + if (BtnPPTSlideShowEnd.Visibility != Visibility.Visible || currentMode != 0) return; - // 直接发送翻页请求到PPT放映软件,不通过软件处理 - if (e.Key == Key.Down || e.Key == Key.PageDown || e.Key == Key.Right || e.Key == Key.N || - e.Key == Key.Space) + if (e.Key == Key.Down || e.Key == Key.PageDown || e.Key == Key.Right || e.Key == Key.N || e.Key == Key.Space) { - e.Handled = true; // 阻止事件继续传播 - SendKeyToPPTSlideShow(false); // 下一页 + BtnPPTSlidesDown_Click(null, null); } - else if (e.Key == Key.Up || e.Key == Key.PageUp || e.Key == Key.Left || e.Key == Key.P) + if (e.Key == Key.Up || e.Key == Key.PageUp || e.Key == Key.Left || e.Key == Key.P) { - e.Handled = true; // 阻止事件继续传播 - SendKeyToPPTSlideShow(true); // 上一页 + BtnPPTSlidesUp_Click(null, null); } } - // 保留PPT翻页快捷键处理 - // 以下方法保留供全局快捷键调用 private void HotKey_Undo(object sender, ExecutedRoutedEventArgs e) { diff --git a/Ink Canvas/MainWindow_cs/MW_PPT.cs b/Ink Canvas/MainWindow_cs/MW_PPT.cs index d542b616..769dc3ce 100644 --- a/Ink Canvas/MainWindow_cs/MW_PPT.cs +++ b/Ink Canvas/MainWindow_cs/MW_PPT.cs @@ -104,7 +104,6 @@ namespace Ink_Canvas #region PPT Managers private PPTManager _pptManager; - private MultiPPTInkManager _multiPPTInkManager; private PPTInkManager _singlePPTInkManager; private PPTUIManager _pptUIManager; @@ -135,19 +134,9 @@ namespace Ink_Canvas _pptManager.PresentationClose += OnPPTPresentationClose; _pptManager.SlideShowStateChanged += OnPPTSlideShowStateChanged; - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager = new PPTInkManager(); - _singlePPTInkManager.IsAutoSaveEnabled = Settings.PowerPointSettings.IsAutoSaveStrokesInPowerPoint; - _singlePPTInkManager.AutoSaveLocation = Settings.Automation.AutoSavedStrokesLocation; - } - else - { - _multiPPTInkManager = new MultiPPTInkManager(); - _multiPPTInkManager.IsAutoSaveEnabled = Settings.PowerPointSettings.IsAutoSaveStrokesInPowerPoint; - _multiPPTInkManager.AutoSaveLocation = Settings.Automation.AutoSavedStrokesLocation; - _multiPPTInkManager.PPTManager = _pptManager; - } + _singlePPTInkManager = new PPTInkManager(); + _singlePPTInkManager.IsAutoSaveEnabled = Settings.PowerPointSettings.IsAutoSaveStrokesInPowerPoint; + _singlePPTInkManager.AutoSaveLocation = Settings.Automation.AutoSavedStrokesLocation; // 初始化UI管理器 _pptUIManager = new PPTUIManager(this); @@ -430,12 +419,10 @@ namespace Ink_Canvas try { _pptManager?.Dispose(); - _multiPPTInkManager?.Dispose(); _singlePPTInkManager?.Dispose(); _longPressTimer?.Stop(); _longPressTimer = null; _pptManager = null; - _multiPPTInkManager = null; _singlePPTInkManager = null; _pptUIManager = null; @@ -521,14 +508,7 @@ namespace Ink_Canvas else { LogHelper.WriteLogToFile("PPT连接已断开", LogHelper.LogType.Event); - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.ClearAllStrokes(); - } - else - { - _multiPPTInkManager?.ClearAllStrokes(); - } + _singlePPTInkManager?.ClearAllStrokes(); } }); } @@ -553,14 +533,7 @@ namespace Ink_Canvas TimeMachineHistories[0] = null; } - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.InitializePresentation(pres); - } - else - { - _multiPPTInkManager?.InitializePresentation(pres); - } + _singlePPTInkManager?.InitializePresentation(pres); // 处理跳转到首页或上次播放页的逻辑 HandlePresentationOpenNavigation(pres); @@ -594,15 +567,7 @@ namespace Ink_Canvas { Application.Current.Dispatcher.InvokeAsync(() => { - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.SaveAllStrokesToFile(pres); - } - else - { - _multiPPTInkManager?.SaveAllStrokesToFile(pres); - _multiPPTInkManager?.RemovePresentation(pres); - } + _singlePPTInkManager?.SaveAllStrokesToFile(pres); _pptUIManager?.UpdateConnectionStatus(false); }); @@ -686,12 +651,15 @@ namespace Ink_Canvas if (activePresentation != null) { - if (Settings.PowerPointSettings.IsSupportWPS) + if (_singlePPTInkManager != null) { - } - else - { - _multiPPTInkManager?.SwitchToPresentation(activePresentation); + try + { + _singlePPTInkManager.InitializePresentation(activePresentation); + } + catch (Exception) + { + } } } @@ -754,6 +722,7 @@ namespace Ink_Canvas if (Settings.PowerPointSettings.IsShowCanvasAtNewSlideShow && !Settings.Automation.IsAutoFoldInPPTSlideShow) { + await Task.Delay(300); // 先进入批注模式,这会显示调色盘 PenIcon_Click(null, null); // 然后设置颜色 @@ -827,11 +796,6 @@ namespace Ink_Canvas var activePresentation = wn.Presentation; var totalSlides = activePresentation.Slides.Count; - if (!Settings.PowerPointSettings.IsSupportWPS) - { - _multiPPTInkManager?.SwitchToPresentation(activePresentation); - } - // 使用防抖机制处理页面切换 HandleSlideSwitchWithDebounce(currentSlide, totalSlides); @@ -879,14 +843,7 @@ namespace Ink_Canvas if (isEnteredSlideShowEndEvent) return; isEnteredSlideShowEndEvent = true; - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.SaveAllStrokesToFile(pres); - } - else - { - _multiPPTInkManager?.SaveAllStrokesToFile(pres); - } + _singlePPTInkManager?.SaveAllStrokesToFile(pres); await Application.Current.Dispatcher.InvokeAsync(() => { @@ -1148,15 +1105,7 @@ namespace Ink_Canvas ClearStrokes(true); timeMachine.ClearStrokeHistory(); - StrokeCollection strokes = null; - if (Settings.PowerPointSettings.IsSupportWPS) - { - strokes = _singlePPTInkManager?.LoadSlideStrokes(slideIndex); - } - else - { - strokes = _multiPPTInkManager?.LoadSlideStrokes(slideIndex); - } + StrokeCollection strokes = _singlePPTInkManager?.LoadSlideStrokes(slideIndex); if (strokes != null && strokes.Count > 0) { @@ -1176,19 +1125,7 @@ namespace Ink_Canvas { try { - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.ResetLockState(); - } - else - { - var activePresentation = _pptManager?.GetCurrentActivePresentation(); - if (activePresentation != null) - { - _multiPPTInkManager?.SwitchToPresentation(activePresentation); - _multiPPTInkManager?.ResetCurrentPresentationLockState(); - } - } + _singlePPTInkManager?.ResetLockState(); } catch (Exception ex) { @@ -1300,40 +1237,17 @@ namespace Ink_Canvas // 如果有当前墨迹且不是第一次切换,先保存到当前页面 if (inkCanvas.Strokes.Count > 0 && currentSlideIndex > 0 && currentSlideIndex != newSlideIndex) { - bool canWrite = false; - if (Settings.PowerPointSettings.IsSupportWPS) - { - canWrite = _singlePPTInkManager?.CanWriteInk(currentSlideIndex) == true; - } - else - { - canWrite = _multiPPTInkManager?.CanWriteInk(currentSlideIndex) == true; - } + bool canWrite = _singlePPTInkManager?.CanWriteInk(currentSlideIndex) == true; if (canWrite) { - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlideIndex, inkCanvas.Strokes); - } - else - { - _multiPPTInkManager?.SaveCurrentSlideStrokes(currentSlideIndex, inkCanvas.Strokes); - } + _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlideIndex, inkCanvas.Strokes); } } ClearStrokes(true); timeMachine.ClearStrokeHistory(); - StrokeCollection newStrokes = null; - if (Settings.PowerPointSettings.IsSupportWPS) - { - newStrokes = _singlePPTInkManager?.SwitchToSlide(newSlideIndex, null); - } - else - { - newStrokes = _multiPPTInkManager?.SwitchToSlide(newSlideIndex, null); - } + StrokeCollection newStrokes = _singlePPTInkManager?.SwitchToSlide(newSlideIndex, null); if (newStrokes != null && newStrokes.Count > 0) { @@ -1474,14 +1388,7 @@ namespace Ink_Canvas var currentSlide = _pptManager?.GetCurrentSlideNumber() ?? 0; if (currentSlide > 0) { - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); - } - else - { - _multiPPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); - } + _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); } // 保存截图(如果启用) @@ -1521,14 +1428,7 @@ namespace Ink_Canvas var currentSlide = _pptManager?.GetCurrentSlideNumber() ?? 0; if (currentSlide > 0) { - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); - } - else - { - _multiPPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); - } + _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); } // 保存截图(如果启用) @@ -1688,14 +1588,7 @@ namespace Ink_Canvas { Application.Current.Dispatcher.Invoke(() => { - if (Settings.PowerPointSettings.IsSupportWPS) - { - _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); - } - else - { - _multiPPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); - } + _singlePPTInkManager?.SaveCurrentSlideStrokes(currentSlide, inkCanvas.Strokes); timeMachine.ClearStrokeHistory(); }); } diff --git a/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs b/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs index dbd3bf50..84621012 100644 --- a/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs +++ b/Ink Canvas/MainWindow_cs/MW_Save&OpenStrokes.cs @@ -79,7 +79,7 @@ namespace Ink_Canvas for (int i = 1; i <= totalSlides; i++) { - var slideStrokes = _multiPPTInkManager?.LoadSlideStrokes(i); + var slideStrokes = _singlePPTInkManager?.LoadSlideStrokes(i); if (slideStrokes != null && slideStrokes.Count > 0) { allPageStrokes.Add(slideStrokes); @@ -581,7 +581,7 @@ namespace Ink_Canvas timeMachine.ClearStrokeHistory(); // 重置PPT墨迹存储 - _multiPPTInkManager?.ClearAllStrokes(); + _singlePPTInkManager?.ClearAllStrokes(); // 读取所有页面的墨迹文件 var files = Directory.GetFiles(tempDir, "page_*.icstk"); @@ -595,7 +595,7 @@ namespace Ink_Canvas var strokes = new StrokeCollection(fs); if (strokes.Count > 0) { - _multiPPTInkManager?.ForceSaveSlideStrokes(pageNumber, strokes); + _singlePPTInkManager?.ForceSaveSlideStrokes(pageNumber, strokes); } } } @@ -605,7 +605,7 @@ namespace Ink_Canvas if (_pptManager?.IsInSlideShow == true) { int currentSlide = _pptManager.GetCurrentSlideNumber(); - var currentStrokes = _multiPPTInkManager?.LoadSlideStrokes(currentSlide); + var currentStrokes = _singlePPTInkManager?.LoadSlideStrokes(currentSlide); if (currentStrokes != null && currentStrokes.Count > 0) { inkCanvas.Strokes.Add(currentStrokes);