修复窗口联动显示问题

This commit is contained in:
ww-rm
2025-10-04 16:56:32 +08:00
parent 092fa76124
commit f19f172e7c

View File

@@ -69,7 +69,6 @@ namespace SpineViewer
_instanceMutex = new Mutex(true, MutexName, out var createdNew);
if (!createdNew)
{
ShowExistedInstance();
SendCommandLineArgs();
Environment.Exit(0); // 不再启动新实例
return;
@@ -100,37 +99,6 @@ namespace SpineViewer
LogManager.Configuration = config;
}
private static void ShowExistedInstance()
{
try
{
// 遍历同名进程
var processes = Process.GetProcessesByName(ProcessName);
foreach (var p in processes)
{
// 跳过当前进程
if (p.Id == Process.GetCurrentProcess().Id)
continue;
IntPtr hWnd = p.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
// 3. 显示并置顶窗口
if (User32.IsIconic(hWnd))
{
User32.ShowWindow(hWnd, User32.SW_RESTORE);
}
User32.SetForegroundWindow(hWnd);
break; // 找到一个就可以退出
}
}
}
catch
{
// 忽略异常,不影响当前进程退出
}
}
private static void SendCommandLineArgs()
{
var args = Environment.GetCommandLineArgs().Skip(1).ToArray();
@@ -187,13 +155,31 @@ namespace SpineViewer
args.Add(line);
if (args.Count > 0)
{
try
{
Current.Dispatcher.Invoke(() =>
{
var vm = (MainWindowViewModel)((MainWindow)Current.MainWindow).DataContext;
// 只要收到参数就可以显示窗口了
var window = (MainWindow)Current.MainWindow;
window.Show();
if (window.WindowState == WindowState.Minimized)
{
window.WindowState = WindowState.Normal;
}
window.Activate();
// 尝试加载参数内容
var vm = (MainWindowViewModel)window.DataContext;
vm.SpineObjectListViewModel.AddSpineObjectFromFileList(args);
});
}
catch (Exception ex)
{
_logger.Trace(ex.ToString());
_logger.Error("Failed to process arguments, {0}", ex.Message);
}
}
}
}
}