mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
[Peek]Fix foreground window setting (#26473)
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
GetMonitorInfo
|
GetMonitorInfo
|
||||||
GetDpiForWindow
|
GetDpiForWindow
|
||||||
GetForegroundWindow
|
GetForegroundWindow
|
||||||
|
SetForegroundWindow
|
||||||
|
SetActiveWindow
|
||||||
|
SendInput
|
||||||
GetWindowThreadProcessId
|
GetWindowThreadProcessId
|
||||||
GetCurrentThreadId
|
GetCurrentThreadId
|
||||||
AttachThreadInput
|
AttachThreadInput
|
||||||
|
|||||||
Reference in New Issue
Block a user