mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
[RegistryPreview]Fix showing in the background on start (#28452)
* [RegistryPreview] Fix showing in the background on start * Use existing window handle
This commit is contained in:
@@ -26,5 +26,17 @@ namespace ManagedCommon
|
|||||||
|
|
||||||
[DllImport("kernel32.dll", SetLastError = true)]
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
internal static extern bool CloseHandle(IntPtr hObject);
|
internal static extern bool CloseHandle(IntPtr hObject);
|
||||||
|
|
||||||
|
[DllImport("user32.dll", SetLastError = true)]
|
||||||
|
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
internal static extern IntPtr GetForegroundWindow();
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
internal static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/common/ManagedCommon/WindowHelpers.cs
Normal file
30
src/common/ManagedCommon/WindowHelpers.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
namespace ManagedCommon
|
||||||
|
{
|
||||||
|
public class WindowHelpers
|
||||||
|
{
|
||||||
|
public static void BringToForeground(IntPtr handle)
|
||||||
|
{
|
||||||
|
var fgHandle = NativeMethods.GetForegroundWindow();
|
||||||
|
|
||||||
|
var threadId1 = NativeMethods.GetWindowThreadProcessId(handle, System.IntPtr.Zero);
|
||||||
|
var threadId2 = NativeMethods.GetWindowThreadProcessId(fgHandle, System.IntPtr.Zero);
|
||||||
|
|
||||||
|
if (threadId1 != threadId2)
|
||||||
|
{
|
||||||
|
NativeMethods.AttachThreadInput(threadId1, threadId2, true);
|
||||||
|
NativeMethods.SetForegroundWindow(handle);
|
||||||
|
NativeMethods.AttachThreadInput(threadId1, threadId2, false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NativeMethods.SetForegroundWindow(handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
// Copyright (c) Microsoft Corporation
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
namespace Hosts.Helpers
|
|
||||||
{
|
|
||||||
internal sealed class NativeMethods
|
|
||||||
{
|
|
||||||
[DllImport("user32.dll", SetLastError = true)]
|
|
||||||
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
|
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
internal static extern IntPtr GetForegroundWindow();
|
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);
|
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
internal static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -24,7 +24,8 @@ namespace Hosts
|
|||||||
Title = title;
|
Title = title;
|
||||||
AppTitleTextBlock.Text = title;
|
AppTitleTextBlock.Text = title;
|
||||||
|
|
||||||
BringToForeground();
|
var handle = this.GetWindowHandle();
|
||||||
|
ManagedCommon.WindowHelpers.BringToForeground(handle);
|
||||||
|
|
||||||
Activated += MainWindow_Activated;
|
Activated += MainWindow_Activated;
|
||||||
}
|
}
|
||||||
@@ -40,25 +41,5 @@ namespace Hosts
|
|||||||
AppTitleTextBlock.Foreground = (SolidColorBrush)App.Current.Resources["WindowCaptionForeground"];
|
AppTitleTextBlock.Foreground = (SolidColorBrush)App.Current.Resources["WindowCaptionForeground"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BringToForeground()
|
|
||||||
{
|
|
||||||
var handle = this.GetWindowHandle();
|
|
||||||
var fgHandle = NativeMethods.GetForegroundWindow();
|
|
||||||
|
|
||||||
var threadId1 = NativeMethods.GetWindowThreadProcessId(handle, System.IntPtr.Zero);
|
|
||||||
var threadId2 = NativeMethods.GetWindowThreadProcessId(fgHandle, System.IntPtr.Zero);
|
|
||||||
|
|
||||||
if (threadId1 != threadId2)
|
|
||||||
{
|
|
||||||
NativeMethods.AttachThreadInput(threadId1, threadId2, true);
|
|
||||||
NativeMethods.SetForegroundWindow(handle);
|
|
||||||
NativeMethods.AttachThreadInput(threadId1, threadId2, false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
NativeMethods.SetForegroundWindow(handle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ namespace RegistryPreview
|
|||||||
UpdateToolBarAndUI(false);
|
UpdateToolBarAndUI(false);
|
||||||
UpdateWindowTitle(resourceLoader.GetString("FileNotFound"));
|
UpdateWindowTitle(resourceLoader.GetString("FileNotFound"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ManagedCommon.WindowHelpers.BringToForeground(windowHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
|
private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
|
||||||
|
|||||||
Reference in New Issue
Block a user