feat: restore from another instance

This commit is contained in:
ema 2024-11-04 03:45:08 +08:00
parent 00447d3e78
commit a960f76920
3 changed files with 31 additions and 10 deletions

View File

@ -405,7 +405,7 @@ public partial class AutoWoodTask : ISoloTask
private void PressZ(WoodTaskParam taskParam)
{
// IMPORTANT: MUST try focus before press Z
SystemControl.Focus(TaskContext.Instance().GameHandle);
SystemControl.FocusWindow(TaskContext.Instance().GameHandle);
if (_first)
{
@ -457,7 +457,7 @@ public partial class AutoWoodTask : ISoloTask
private void PressEsc(WoodTaskParam taskParam)
{
SystemControl.Focus(TaskContext.Instance().GameHandle);
SystemControl.FocusWindow(TaskContext.Instance().GameHandle);
Simulation.SendInput.Keyboard.KeyPress(VK.VK_ESCAPE);
// if (TaskContext.Instance().Config.AutoWoodConfig.PressTwoEscEnabled)
// {

View File

@ -178,18 +178,37 @@ public class SystemControl
ActivateWindow(TaskContext.Instance().GameHandle);
}
public static void Focus(nint hWnd)
public static void FocusWindow(nint hWnd)
{
if (User32.IsWindow(hWnd))
{
_ = User32.SendMessage(hWnd, User32.WindowMessage.WM_SYSCOMMAND, User32.SysCommand.SC_RESTORE, 0);
_ = User32.SetForegroundWindow(hWnd);
while (User32.IsIconic(hWnd))
{
continue;
}
_ = User32.BringWindowToTop(hWnd);
_ = User32.SetActiveWindow(hWnd);
}
}
public static void RestoreWindow(nint hWnd)
{
if (User32.IsWindow(hWnd))
{
_ = User32.SendMessage(hWnd, User32.WindowMessage.WM_SYSCOMMAND, User32.SysCommand.SC_RESTORE, 0);
_ = User32.SetForegroundWindow(hWnd);
if (User32.IsIconic(hWnd))
{
_ = User32.ShowWindow(hWnd, ShowWindowCommand.SW_RESTORE);
}
_ = User32.BringWindowToTop(hWnd);
_ = User32.SetActiveWindow(hWnd);
}
}

View File

@ -1,4 +1,5 @@
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.GameTask;
using Microsoft.Extensions.Hosting;
using System;
using System.ComponentModel;
@ -9,6 +10,8 @@ using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
namespace BetterGenshinImpact.Helpers;
@ -116,19 +119,18 @@ internal static class RuntimeHelper
handle = new EventWaitHandle(false, EventResetMode.AutoReset, instanceName);
}
Task.Factory.StartNew(() =>
_ = Task.Factory.StartNew(() =>
{
while (handle.WaitOne())
{
#if false
UIDispatcherHelper.BeginInvoke(main =>
Application.Current.Dispatcher?.BeginInvoke(() =>
{
main?.Activate();
main?.Show();
Application.Current.MainWindow?.Show();
Application.Current.MainWindow?.Activate();
SystemControl.RestoreWindow(new WindowInteropHelper(Application.Current.MainWindow).Handle);
});
#endif
}
}, TaskCreationOptions.LongRunning);
}, TaskCreationOptions.LongRunning).ConfigureAwait(false);
}
public static void CheckIntegration()