优化代码
This commit is contained in:
@@ -3,6 +3,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Windows.Ink;
|
using System.Windows.Ink;
|
||||||
@@ -78,7 +79,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
// 设置为当前活跃的演示文稿
|
// 设置为当前活跃的演示文稿
|
||||||
_currentActivePresentationId = presentationId;
|
_currentActivePresentationId = presentationId;
|
||||||
|
|
||||||
LogHelper.WriteLogToFile($"已初始化多PPT墨迹管理: {presentation.Name} (ID: {presentationId})", LogHelper.LogType.Event);
|
LogHelper.WriteLogToFile($"已初始化多PPT墨迹管理: {presentation.Name}, 幻灯片数量: {presentation.Slides.Count}", LogHelper.LogType.Event);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -127,7 +128,10 @@ namespace Ink_Canvas.Helpers
|
|||||||
_presentationInfos[presentationId].LastAccessTime = DateTime.Now;
|
_presentationInfos[presentationId].LastAccessTime = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogHelper.WriteLogToFile($"已切换到演示文稿: {presentation.Name} (ID: {presentationId})", LogHelper.LogType.Trace);
|
if (_currentActivePresentationId != presentationId)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"已切换到演示文稿: {presentation.Name}", LogHelper.LogType.Trace);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -445,11 +449,17 @@ namespace Ink_Canvas.Helpers
|
|||||||
_currentActivePresentationId = "";
|
_currentActivePresentationId = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
LogHelper.WriteLogToFile($"已移除演示文稿管理器: {presentation.Name}", LogHelper.LogType.Trace);
|
LogHelper.WriteLogToFile($"已移除演示文稿管理器: {presentationId}", LogHelper.LogType.Trace);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (COMException comEx)
|
||||||
|
{
|
||||||
|
var hr = (uint)comEx.HResult;
|
||||||
|
if (hr == 0x8001010E || hr == 0x80004005 || hr == 0x800706BA || hr == 0x800706BE || hr == 0x80048010)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"移除演示文稿管理器失败: {ex}", LogHelper.LogType.Error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -546,14 +556,28 @@ namespace Ink_Canvas.Helpers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// 检查COM对象是否仍然有效
|
||||||
|
if (presentation == null)
|
||||||
|
{
|
||||||
|
return $"invalid_{DateTime.Now.Ticks}";
|
||||||
|
}
|
||||||
|
|
||||||
var presentationPath = presentation.FullName;
|
var presentationPath = presentation.FullName;
|
||||||
var fileHash = GetFileHash(presentationPath);
|
var fileHash = GetFileHash(presentationPath);
|
||||||
var processId = GetProcessId(presentation);
|
var processId = GetProcessId(presentation);
|
||||||
return $"{presentation.Name}_{presentation.Slides.Count}_{fileHash}_{processId}";
|
return $"{presentation.Name}_{presentation.Slides.Count}_{fileHash}_{processId}";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"生成演示文稿ID失败: {ex}", LogHelper.LogType.Error);
|
|
||||||
return $"unknown_{DateTime.Now.Ticks}";
|
return $"unknown_{DateTime.Now.Ticks}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -570,9 +594,9 @@ namespace Ink_Canvas.Helpers
|
|||||||
return BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, 8);
|
return BitConverter.ToString(hashBytes).Replace("-", "").Substring(0, 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"计算文件哈希值失败: {ex}", LogHelper.LogType.Error);
|
// 所有异常都静默处理,避免日志噪音
|
||||||
return "error";
|
return "error";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -593,9 +617,18 @@ namespace Ink_Canvas.Helpers
|
|||||||
}
|
}
|
||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"获取进程ID失败: {ex}", LogHelper.LogType.Error);
|
|
||||||
return "error";
|
return "error";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,10 @@ namespace Ink_Canvas.Helpers
|
|||||||
// 检查墨迹锁定
|
// 检查墨迹锁定
|
||||||
if (!CanWriteInk(slideIndex))
|
if (!CanWriteInk(slideIndex))
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"墨迹写入被锁定,当前页:{slideIndex},锁定页:{_lockedSlideIndex}", LogHelper.LogType.Warning);
|
if (DateTime.Now < _inkLockUntil)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"墨迹写入被锁定,当前页:{slideIndex},锁定页:{_lockedSlideIndex}", LogHelper.LogType.Warning);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +120,10 @@ namespace Ink_Canvas.Helpers
|
|||||||
_memoryStreams[slideIndex]?.Dispose();
|
_memoryStreams[slideIndex]?.Dispose();
|
||||||
_memoryStreams[slideIndex] = ms;
|
_memoryStreams[slideIndex] = ms;
|
||||||
|
|
||||||
LogHelper.WriteLogToFile($"已保存第{slideIndex}页墨迹,大小: {ms.Length} bytes", LogHelper.LogType.Trace);
|
if (ms.Length > 0)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"已保存第{slideIndex}页墨迹,大小: {ms.Length} bytes", LogHelper.LogType.Trace);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -215,7 +221,10 @@ namespace Ink_Canvas.Helpers
|
|||||||
_lastSwitchTime = now;
|
_lastSwitchTime = now;
|
||||||
_lastSwitchSlideIndex = slideIndex;
|
_lastSwitchSlideIndex = slideIndex;
|
||||||
|
|
||||||
LogHelper.WriteLogToFile($"已切换到第{slideIndex}页,加载墨迹数量: {newStrokes.Count}", LogHelper.LogType.Trace);
|
if (newStrokes.Count > 0)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"已切换到第{slideIndex}页,加载墨迹数量: {newStrokes.Count}", LogHelper.LogType.Trace);
|
||||||
|
}
|
||||||
|
|
||||||
return newStrokes;
|
return newStrokes;
|
||||||
}
|
}
|
||||||
@@ -386,9 +395,20 @@ namespace Ink_Canvas.Helpers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool CanWriteInk(int currentSlideIndex)
|
public bool CanWriteInk(int currentSlideIndex)
|
||||||
{
|
{
|
||||||
if (DateTime.Now < _inkLockUntil) return false;
|
// 如果锁定时间已过,允许写入
|
||||||
if (currentSlideIndex != _lockedSlideIndex && _lockedSlideIndex > 0) return false;
|
if (DateTime.Now >= _inkLockUntil)
|
||||||
return true;
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果当前页面与锁定页面相同,允许写入(用户在当前页面绘制)
|
||||||
|
if (currentSlideIndex == _lockedSlideIndex)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只有在快速切换且页面不同时才锁定
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -277,16 +277,6 @@ namespace Ink_Canvas.Helpers
|
|||||||
catch (COMException ex)
|
catch (COMException ex)
|
||||||
{
|
{
|
||||||
var hr = (uint)ex.HResult;
|
var hr = (uint)ex.HResult;
|
||||||
// 忽略常见的PowerPoint连接错误:
|
|
||||||
// 0x800401E3: 操作无法使用
|
|
||||||
// 0x80004005: 未指定错误
|
|
||||||
// 0x800706B5: RPC服务器不可用
|
|
||||||
// 0x8001010E: 应用程序调用一个已为另一线程整理的接口
|
|
||||||
// 0x800401F3: 无效的类字符串(PowerPoint未安装或COM组件未注册)
|
|
||||||
if (hr != 0x800401E3 && hr != 0x80004005 && hr != 0x800706B5 && hr != 0x8001010E && hr != 0x800401F3)
|
|
||||||
{
|
|
||||||
LogHelper.WriteLogToFile($"连接PowerPoint失败: {ex}", LogHelper.LogType.Warning);
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (InvalidCastException)
|
catch (InvalidCastException)
|
||||||
@@ -294,9 +284,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
// COM对象类型转换失败
|
// COM对象类型转换失败
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"连接PowerPoint时发生意外错误: {ex}", LogHelper.LogType.Warning);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -422,12 +411,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
}
|
}
|
||||||
catch (COMException comEx)
|
catch (COMException comEx)
|
||||||
{
|
{
|
||||||
// COM对象已经被释放或在错误的线程中,忽略这些错误
|
|
||||||
var hr = (uint)comEx.HResult;
|
var hr = (uint)comEx.HResult;
|
||||||
if (hr != 0x8001010E && hr != 0x80004005 && hr != 0x800706B5)
|
|
||||||
{
|
|
||||||
LogHelper.WriteLogToFile($"取消PPT事件注册COM异常: {comEx}", LogHelper.LogType.Warning);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (InvalidCastException)
|
catch (InvalidCastException)
|
||||||
{
|
{
|
||||||
@@ -437,10 +421,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
}, DispatcherPriority.Normal, CancellationToken.None, TimeSpan.FromSeconds(1));
|
}, DispatcherPriority.Normal, CancellationToken.None, TimeSpan.FromSeconds(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
// 记录但不抛出异常,确保清理过程能够继续
|
|
||||||
LogHelper.WriteLogToFile($"取消PPT事件注册失败: {ex.GetType().Name} - {ex.Message}", LogHelper.LogType.Warning);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 释放COM对象
|
// 释放COM对象
|
||||||
@@ -451,9 +433,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
Marshal.ReleaseComObject(CurrentSlide);
|
Marshal.ReleaseComObject(CurrentSlide);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"释放CurrentSlide COM对象失败: {ex}", LogHelper.LogType.Warning);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -463,9 +444,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
Marshal.ReleaseComObject(CurrentSlides);
|
Marshal.ReleaseComObject(CurrentSlides);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"释放CurrentSlides COM对象失败: {ex}", LogHelper.LogType.Warning);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -475,9 +455,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
Marshal.ReleaseComObject(CurrentPresentation);
|
Marshal.ReleaseComObject(CurrentPresentation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"释放CurrentPresentation COM对象失败: {ex}", LogHelper.LogType.Warning);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -487,9 +466,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
Marshal.ReleaseComObject(PPTApplication);
|
Marshal.ReleaseComObject(PPTApplication);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"释放PPTApplication COM对象失败: {ex}", LogHelper.LogType.Warning);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清理引用
|
// 清理引用
|
||||||
|
|||||||
@@ -86,7 +86,6 @@ namespace Ink_Canvas.Helpers
|
|||||||
{
|
{
|
||||||
_mainWindow.PPTBtnPageNow.Text = currentSlide.ToString();
|
_mainWindow.PPTBtnPageNow.Text = currentSlide.ToString();
|
||||||
_mainWindow.PPTBtnPageTotal.Text = $"/ {totalSlides}";
|
_mainWindow.PPTBtnPageTotal.Text = $"/ {totalSlides}";
|
||||||
LogHelper.WriteLogToFile($"更新PPT页码显示: {currentSlide}/{totalSlides}", LogHelper.LogType.Trace);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -127,7 +126,6 @@ namespace Ink_Canvas.Helpers
|
|||||||
{
|
{
|
||||||
_mainWindow.PPTBtnPageNow.Text = currentSlide.ToString();
|
_mainWindow.PPTBtnPageNow.Text = currentSlide.ToString();
|
||||||
_mainWindow.PPTBtnPageTotal.Text = $"/ {totalSlides}";
|
_mainWindow.PPTBtnPageTotal.Text = $"/ {totalSlides}";
|
||||||
LogHelper.WriteLogToFile($"更新PPT页码显示: {currentSlide}/{totalSlides}", LogHelper.LogType.Trace);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -578,9 +578,16 @@ namespace Ink_Canvas
|
|||||||
_pptUIManager?.UpdateConnectionStatus(false);
|
_pptUIManager?.UpdateConnectionStatus(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (COMException comEx)
|
||||||
|
{
|
||||||
|
// COM对象已失效,这是正常情况,完全静默处理
|
||||||
|
var hr = (uint)comEx.HResult;
|
||||||
|
if (hr == 0x8001010E || hr == 0x80004005 || hr == 0x800706BA || hr == 0x800706BE || hr == 0x80048010)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"处理演示文稿关闭事件失败: {ex}", LogHelper.LogType.Error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -222,7 +222,6 @@ namespace Ink_Canvas
|
|||||||
BtnStartCover.Visibility = Visibility.Collapsed;
|
BtnStartCover.Visibility = Visibility.Collapsed;
|
||||||
BorderStopTime.Visibility = Visibility.Collapsed;
|
BorderStopTime.Visibility = Visibility.Collapsed;
|
||||||
TextBlockHour.Foreground = new SolidColorBrush(StringToColor("#FF5B5D5F"));
|
TextBlockHour.Foreground = new SolidColorBrush(StringToColor("#FF5B5D5F"));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else if (isTimerRunning && isPaused)
|
else if (isTimerRunning && isPaused)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user