[Peek]Fix foreground window setting (#26473)

This commit is contained in:
Jaime Bernardo
2023-05-30 14:58:32 +01:00
committed by GitHub
parent 1d23ed5811
commit 88b1203cd6
2 changed files with 13 additions and 14 deletions

View File

@@ -2,7 +2,9 @@
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using ManagedCommon;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Windows.Win32; using Windows.Win32;
using Windows.Win32.Foundation; using Windows.Win32.Foundation;
@@ -22,21 +24,15 @@ namespace Peek.UI.Extensions
public static void BringToForeground(this Window window) public static void BringToForeground(this Window window)
{ {
var foregroundWindowHandle = PInvoke.GetForegroundWindow(); // Ability to be able to set the Window to the Foreground is very limited. A current workaround is simulating mouse input before bringing to the foreground.
uint targetProcessId = 0;
uint windowThreadProcessId = 0;
unsafe
{
windowThreadProcessId = PInvoke.GetWindowThreadProcessId(foregroundWindowHandle, &targetProcessId);
}
var windowHandle = window.GetWindowHandle(); var windowHandle = window.GetWindowHandle();
var currentThreadId = PInvoke.GetCurrentThreadId(); Windows.Win32.UI.Input.KeyboardAndMouse.INPUT input = new Windows.Win32.UI.Input.KeyboardAndMouse.INPUT { type = Windows.Win32.UI.Input.KeyboardAndMouse.INPUT_TYPE.INPUT_MOUSE, Anonymous = { } };
PInvoke.AttachThreadInput(windowThreadProcessId, currentThreadId, true); Windows.Win32.UI.Input.KeyboardAndMouse.INPUT[] inputs = new Windows.Win32.UI.Input.KeyboardAndMouse.INPUT[] { input };
PInvoke.BringWindowToTop(new HWND(windowHandle)); _ = PInvoke.SendInput(inputs.AsSpan<Windows.Win32.UI.Input.KeyboardAndMouse.INPUT>(), Marshal.SizeOf(typeof(Windows.Win32.UI.Input.KeyboardAndMouse.INPUT)));
PInvoke.ShowWindow(new HWND(windowHandle), Windows.Win32.UI.WindowsAndMessaging.SHOW_WINDOW_CMD.SW_SHOW); if (PInvoke.SetForegroundWindow(new HWND(windowHandle)) == 0)
PInvoke.AttachThreadInput(windowThreadProcessId, currentThreadId, false); {
Logger.LogWarning("Couldn't set the Peek window as the foreground window.");
}
} }
internal static void CenterOnMonitor(this Window window, HWND hwndDesktop, double? width = null, double? height = null) internal static void CenterOnMonitor(this Window window, HWND hwndDesktop, double? width = null, double? height = null)

View File

@@ -2,6 +2,9 @@
GetMonitorInfo GetMonitorInfo
GetDpiForWindow GetDpiForWindow
GetForegroundWindow GetForegroundWindow
SetForegroundWindow
SetActiveWindow
SendInput
GetWindowThreadProcessId GetWindowThreadProcessId
GetCurrentThreadId GetCurrentThreadId
AttachThreadInput AttachThreadInput