diff --git a/Ink Canvas/App.xaml.cs b/Ink Canvas/App.xaml.cs index 6268a1d6..396a012e 100644 --- a/Ink Canvas/App.xaml.cs +++ b/Ink Canvas/App.xaml.cs @@ -39,6 +39,8 @@ namespace Ink_Canvas // 新增:标记是否通过--board参数启动 public static bool StartWithBoardMode = false; + // 新增:标记是否通过--show参数启动 + public static bool StartWithShowMode = false; // 新增:保存看门狗进程对象 private static Process watchdogProcess; // 新增:标记是否为软件内主动退出 @@ -617,6 +619,14 @@ namespace Ink_Canvas LogHelper.WriteLogToFile("App | 检测到--board参数,将直接进入白板模式"); } + // 检查是否通过--show参数启动 + bool hasShowArg = e.Args.Contains("--show"); + if (hasShowArg) + { + StartWithShowMode = true; + LogHelper.WriteLogToFile("App | 检测到--show参数,将退出收纳模式并恢复浮动栏"); + } + // 记录最终应用启动状态 if (isFinalApp) { @@ -819,6 +829,21 @@ namespace Ink_Canvas LogHelper.WriteLogToFile("通过IPC发送白板模式命令失败", LogHelper.LogType.Warning); } } + // 检查是否有--show参数 + else if (hasShowArg) + { + LogHelper.WriteLogToFile("检测到已运行实例且有--show参数,尝试通过IPC发送展开浮动栏命令", LogHelper.LogType.Event); + + // 尝试通过IPC发送展开浮动栏命令给已运行实例 + if (FileAssociationManager.TrySendShowModeCommandToExistingInstance()) + { + LogHelper.WriteLogToFile("展开浮动栏命令已通过IPC发送给已运行实例", LogHelper.LogType.Event); + } + else + { + LogHelper.WriteLogToFile("通过IPC发送展开浮动栏命令失败", LogHelper.LogType.Warning); + } + } else { LogHelper.WriteLogToFile("检测到已运行实例,但无文件参数", LogHelper.LogType.Event); diff --git a/Ink Canvas/Helpers/FileAssociationManager.cs b/Ink Canvas/Helpers/FileAssociationManager.cs index cc5460ad..89525e93 100644 --- a/Ink Canvas/Helpers/FileAssociationManager.cs +++ b/Ink Canvas/Helpers/FileAssociationManager.cs @@ -25,6 +25,7 @@ namespace Ink_Canvas.Helpers private const string IpcEventName = "InkCanvasFileAssociationEvent"; private const string IpcFilePrefix = "InkCanvasFileAssociation_"; private const string IpcBoardModePrefix = "InkCanvasBoardMode_"; + private const string IpcShowModePrefix = "InkCanvasShowMode_"; private const int IpcTimeout = 5000; // 5秒超时 /// @@ -310,6 +311,56 @@ namespace Ink_Canvas.Helpers } } + /// + /// 尝试通过IPC将展开浮动栏命令发送给已运行的实例 + /// + /// 是否成功发送 + public static bool TrySendShowModeCommandToExistingInstance() + { + try + { + LogHelper.WriteLogToFile("尝试通过IPC发送展开浮动栏命令给已运行实例", LogHelper.LogType.Event); + + // 创建IPC文件 + string tempDir = Path.GetTempPath(); + string ipcFileName = IpcShowModePrefix + Guid.NewGuid().ToString("N") + ".tmp"; + string ipcFilePath = Path.Combine(tempDir, ipcFileName); + + // 写入展开浮动栏命令到IPC文件 + File.WriteAllText(ipcFilePath, "SHOW_MODE", Encoding.UTF8); + + // 创建事件通知已运行实例 + using (EventWaitHandle ipcEvent = new EventWaitHandle(false, EventResetMode.ManualReset, IpcEventName)) + { + ipcEvent.Set(); + } + + // 等待一段时间让已运行实例处理命令 + Thread.Sleep(1000); + + // 清理IPC文件 + try + { + if (File.Exists(ipcFilePath)) + { + File.Delete(ipcFilePath); + } + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"清理IPC文件失败: {ex.Message}", LogHelper.LogType.Warning); + } + + LogHelper.WriteLogToFile("IPC展开浮动栏命令发送完成", LogHelper.LogType.Event); + return true; + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"通过IPC发送展开浮动栏命令失败: {ex.Message}", LogHelper.LogType.Error); + return false; + } + } + /// /// 启动IPC监听器,等待其他实例发送文件路径 /// @@ -470,6 +521,61 @@ namespace Ink_Canvas.Helpers catch { } } } + + // 处理展开浮动栏命令IPC文件 + string[] showModeFiles = Directory.GetFiles(tempDir, IpcShowModePrefix + "*.tmp"); + foreach (string ipcFile in showModeFiles) + { + try + { + // 读取命令内容 + string command = File.ReadAllText(ipcFile, Encoding.UTF8); + + if (command == "SHOW_MODE") + { + LogHelper.WriteLogToFile("IPC接收到展开浮动栏命令", LogHelper.LogType.Event); + + // 在UI线程中处理展开浮动栏 + Application.Current.Dispatcher.BeginInvoke(new Action(async () => + { + try + { + // 获取主窗口并展开浮动栏 + if (Application.Current.MainWindow is MainWindow mainWindow) + { + // 如果当前处于收纳模式,则展开浮动栏 + if (mainWindow.isFloatingBarFolded) + { + await mainWindow.UnFoldFloatingBar(new object()); + } + mainWindow.ShowNotification("已退出收纳模式并恢复浮动栏"); + } + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"IPC处理展开浮动栏失败: {ex.Message}", LogHelper.LogType.Error); + } + })); + } + + // 删除IPC文件 + File.Delete(ipcFile); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"处理展开浮动栏IPC文件失败: {ex.Message}", LogHelper.LogType.Warning); + + // 尝试删除损坏的IPC文件 + try + { + if (File.Exists(ipcFile)) + { + File.Delete(ipcFile); + } + } + catch { } + } + } } catch (Exception ex) { diff --git a/Ink Canvas/MainWindow.xaml.cs b/Ink Canvas/MainWindow.xaml.cs index 84bc8f17..7857e8bb 100644 --- a/Ink Canvas/MainWindow.xaml.cs +++ b/Ink Canvas/MainWindow.xaml.cs @@ -511,8 +511,8 @@ namespace Ink_Canvas } SystemEvents.DisplaySettingsChanged += SystemEventsOnDisplaySettingsChanged; - // 自动收纳到侧边栏(若通过 --board 进入白板模式则跳过收纳) - if (Settings.Startup.IsFoldAtStartup && !App.StartWithBoardMode) + // 自动收纳到侧边栏(若通过 --board 进入白板模式或 --show 参数则跳过收纳) + if (Settings.Startup.IsFoldAtStartup && !App.StartWithBoardMode && !App.StartWithShowMode) { FoldFloatingBar_MouseUp(new object(), null); } @@ -523,8 +523,6 @@ namespace Ink_Canvas else RadioCrashNoAction.IsChecked = true; - - // 如果当前不是黑板模式,则切换到黑板模式 if (currentMode == 0) { @@ -584,6 +582,21 @@ namespace Ink_Canvas SwitchToBoardMode(); }), DispatcherPriority.Loaded); } + + // 检查是否通过--show参数启动,如果是则确保退出收纳模式并恢复浮动栏 + if (App.StartWithShowMode) + { + LogHelper.WriteLogToFile("检测到--show参数,退出收纳模式并恢复浮动栏", LogHelper.LogType.Event); + // 延迟执行,确保UI已完全加载 + Dispatcher.BeginInvoke(new Action(async () => + { + // 如果当前处于收纳模式,则展开浮动栏 + if (isFloatingBarFolded) + { + await UnFoldFloatingBar(new object()); + } + }), DispatcherPriority.Loaded); + } } private void SystemEventsOnDisplaySettingsChanged(object sender, EventArgs e)