From 88b1203cd64f88fe238a0712c1c7d9ee8bdf9ee7 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Tue, 30 May 2023 14:58:32 +0100 Subject: [PATCH] [Peek]Fix foreground window setting (#26473) --- .../Peek.UI/Extensions/WindowExtensions.cs | 24 ++++++++----------- src/modules/peek/Peek.UI/NativeMethods.txt | 3 +++ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs b/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs index 71dbd6ca07..7deb604d1a 100644 --- a/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs +++ b/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs @@ -2,7 +2,9 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Runtime.InteropServices; +using ManagedCommon; using Microsoft.UI.Xaml; using Windows.Win32; using Windows.Win32.Foundation; @@ -22,21 +24,15 @@ namespace Peek.UI.Extensions public static void BringToForeground(this Window window) { - var foregroundWindowHandle = PInvoke.GetForegroundWindow(); - - uint targetProcessId = 0; - uint windowThreadProcessId = 0; - unsafe - { - windowThreadProcessId = PInvoke.GetWindowThreadProcessId(foregroundWindowHandle, &targetProcessId); - } - + // 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. var windowHandle = window.GetWindowHandle(); - var currentThreadId = PInvoke.GetCurrentThreadId(); - PInvoke.AttachThreadInput(windowThreadProcessId, currentThreadId, true); - PInvoke.BringWindowToTop(new HWND(windowHandle)); - PInvoke.ShowWindow(new HWND(windowHandle), Windows.Win32.UI.WindowsAndMessaging.SHOW_WINDOW_CMD.SW_SHOW); - PInvoke.AttachThreadInput(windowThreadProcessId, currentThreadId, false); + 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 = { } }; + Windows.Win32.UI.Input.KeyboardAndMouse.INPUT[] inputs = new Windows.Win32.UI.Input.KeyboardAndMouse.INPUT[] { input }; + _ = PInvoke.SendInput(inputs.AsSpan(), Marshal.SizeOf(typeof(Windows.Win32.UI.Input.KeyboardAndMouse.INPUT))); + if (PInvoke.SetForegroundWindow(new HWND(windowHandle)) == 0) + { + 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) diff --git a/src/modules/peek/Peek.UI/NativeMethods.txt b/src/modules/peek/Peek.UI/NativeMethods.txt index 7cc3a51db2..3f9cc3e8f7 100644 --- a/src/modules/peek/Peek.UI/NativeMethods.txt +++ b/src/modules/peek/Peek.UI/NativeMethods.txt @@ -2,6 +2,9 @@ GetMonitorInfo GetDpiForWindow GetForegroundWindow +SetForegroundWindow +SetActiveWindow +SendInput GetWindowThreadProcessId GetCurrentThreadId AttachThreadInput