[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.
// 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<Windows.Win32.UI.Input.KeyboardAndMouse.INPUT>(), 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)

View File

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