Fix possible Win32Exception

This commit is contained in:
Jack251970 2025-04-15 09:06:31 +08:00
parent 9035aa6fab
commit 28c7538fc3

View file

@ -365,21 +365,10 @@ namespace Flow.Launcher.Infrastructure
// No installed English layout found
if (enHKL == HKL.Null) return;
// When application is exiting, the Application.Current will be null
if (Application.Current == null) return;
// Get the FL main window
var hwnd = GetWindowHandle(Application.Current.MainWindow, true);
// Get the foreground window
var hwnd = PInvoke.GetForegroundWindow();
if (hwnd == HWND.Null) return;
// Check if the FL main window is the current foreground window
if (!IsForegroundWindow(hwnd))
{
var result = PInvoke.SetForegroundWindow(hwnd);
// If we cannot set the foreground window, we can use the foreground window and switch the layout
if (!result) hwnd = PInvoke.GetForegroundWindow();
}
// Get the current foreground window thread ID
var threadId = PInvoke.GetWindowThreadProcessId(hwnd);
if (threadId == 0) throw new Win32Exception(Marshal.GetLastWin32Error());