From b9200695be4e2eccbd939d0db1ef31eceddbadb5 Mon Sep 17 00:00:00 2001 From: huiyadanli Date: Tue, 5 Dec 2023 22:42:13 +0800 Subject: [PATCH] fixed: #135 use FindWindow instead of MainWindowHandle --- BetterGenshinImpact/GameTask/SystemControl.cs | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/BetterGenshinImpact/GameTask/SystemControl.cs b/BetterGenshinImpact/GameTask/SystemControl.cs index cdc9f193..d00612e8 100644 --- a/BetterGenshinImpact/GameTask/SystemControl.cs +++ b/BetterGenshinImpact/GameTask/SystemControl.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.Windows; using Vanara.PInvoke; namespace BetterGenshinImpact.GameTask; @@ -12,7 +13,8 @@ public class SystemControl { public static nint FindGenshinImpactHandle() { - return FindHandleByProcessName("YuanShen", "GenshinImpact", "Genshin Impact Cloud Game"); + var handle = FindHandleByWindowName(); + return handle != 0 ? handle : FindHandleByProcessName("YuanShen", "GenshinImpact", "Genshin Impact Cloud Game"); } public static async Task StartFromLocalAsync(string path) @@ -22,15 +24,18 @@ public class SystemControl DirectStartFromYuanShen(path); await Task.Delay(10000); } + if (path.EndsWith("launcher.exe")) { StartFromLauncher(path); await Task.Delay(10000); } + if (path.EndsWith("Genshin Impact Cloud Game.exe")) { StartCloudYaunShen(path); } + return FindGenshinImpactHandle(); } @@ -65,6 +70,29 @@ public class SystemControl return 0; } + public static nint FindHandleByWindowName() + { + var handle = (nint)User32.FindWindow("UnityWndClass", "原神"); + if (handle != 0) + { + return handle; + } + + handle = (nint)User32.FindWindow("UnityWndClass", "Genshin Impact"); + if (handle != 0) + { + return handle; + } + + handle = (nint)User32.FindWindow("Qt5152QWindowIcon", "云·原神"); + if (handle != 0) + { + return handle; + } + + return 0; + } + public static string? GetActiveProcessName() { try @@ -146,6 +174,7 @@ public class SystemControl { throw new Exception("请先启动BetterGI"); } + ActivateWindow(TaskContext.Instance().GameHandle); } @@ -159,6 +188,7 @@ public class SystemControl { continue; } + _ = User32.BringWindowToTop(hWnd); } } @@ -209,4 +239,4 @@ public class SystemControl // TODO:点完之后有个15s的倒计时,好像不处理也没什么问题,直接睡个20s吧 Thread.Sleep(20000); } -} +} \ No newline at end of file