修复窗口联动显示问题
This commit is contained in:
@@ -69,7 +69,6 @@ namespace SpineViewer
|
|||||||
_instanceMutex = new Mutex(true, MutexName, out var createdNew);
|
_instanceMutex = new Mutex(true, MutexName, out var createdNew);
|
||||||
if (!createdNew)
|
if (!createdNew)
|
||||||
{
|
{
|
||||||
ShowExistedInstance();
|
|
||||||
SendCommandLineArgs();
|
SendCommandLineArgs();
|
||||||
Environment.Exit(0); // 不再启动新实例
|
Environment.Exit(0); // 不再启动新实例
|
||||||
return;
|
return;
|
||||||
@@ -100,37 +99,6 @@ namespace SpineViewer
|
|||||||
LogManager.Configuration = config;
|
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()
|
private static void SendCommandLineArgs()
|
||||||
{
|
{
|
||||||
var args = Environment.GetCommandLineArgs().Skip(1).ToArray();
|
var args = Environment.GetCommandLineArgs().Skip(1).ToArray();
|
||||||
@@ -188,11 +156,29 @@ namespace SpineViewer
|
|||||||
|
|
||||||
if (args.Count > 0)
|
if (args.Count > 0)
|
||||||
{
|
{
|
||||||
Current.Dispatcher.Invoke(() =>
|
try
|
||||||
{
|
{
|
||||||
var vm = (MainWindowViewModel)((MainWindow)Current.MainWindow).DataContext;
|
Current.Dispatcher.Invoke(() =>
|
||||||
vm.SpineObjectListViewModel.AddSpineObjectFromFileList(args);
|
{
|
||||||
});
|
// 只要收到参数就可以显示窗口了
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user