[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:
Stefan Markovic
2023-09-12 15:50:02 +02:00
committed by GitHub
parent 67a2dc0d6f
commit 43549eba77
5 changed files with 46 additions and 45 deletions

View File

@@ -26,5 +26,17 @@ namespace ManagedCommon
[DllImport("kernel32.dll", SetLastError = true)]
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);
}
}

View 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);
}
}
}
}

View File

@@ -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);
}
}

View File

@@ -24,7 +24,8 @@ namespace Hosts
Title = title;
AppTitleTextBlock.Text = title;
BringToForeground();
var handle = this.GetWindowHandle();
ManagedCommon.WindowHelpers.BringToForeground(handle);
Activated += MainWindow_Activated;
}
@@ -40,25 +41,5 @@ namespace Hosts
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);
}
}
}
}

View File

@@ -97,6 +97,8 @@ namespace RegistryPreview
UpdateToolBarAndUI(false);
UpdateWindowTitle(resourceLoader.GetString("FileNotFound"));
}
ManagedCommon.WindowHelpers.BringToForeground(windowHandle);
}
private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)