添加单实例限制、应用图标、移除托盘设置菜单、快捷键暂停/恢复功能

单实例限制:通过命名Mutex和窗口消息广播确保只能运行一个实例,再次启动时唤起已有窗口

应用图标:exe文件嵌入app.ico,托盘和窗口图标统一使用该图标文件

移除托盘右键菜单中与显示主界面重复的设置选项

快捷键暂停/恢复:托盘菜单和主界面侧边栏均添加切换按钮,通过MainViewModel.IsHotKeyEnabled双向同步
This commit is contained in:
2026-05-10 14:10:52 +08:00
parent b715904439
commit f33c89d2c4
8 changed files with 116 additions and 26 deletions

View File

@@ -1,4 +1,6 @@
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using PersonalToolBox.Services;
@@ -17,6 +19,17 @@ public partial class App : System.Windows.Application
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PersonalToolBox", "crash.log");
private const string AppMutexName = "PersonalToolBox_SingleInstance_8E2F4A1C";
private static Mutex? _mutex;
[DllImport("user32.dll")]
private static extern uint RegisterWindowMessage(string lpString);
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xFFFF);
public App()
{
// 监听未处理的异常,写入文件日志
@@ -36,6 +49,15 @@ public partial class App : System.Windows.Application
protected override async void OnStartup(StartupEventArgs e)
{
_mutex = new Mutex(true, AppMutexName, out bool createdNew);
if (!createdNew)
{
uint msg = RegisterWindowMessage("PersonalToolBox_ShowMain");
PostMessage(HWND_BROADCAST, msg, IntPtr.Zero, IntPtr.Zero);
Shutdown();
return;
}
try
{
base.OnStartup(e);