From 69d9f3fd14d3b420dcfb0f56acb623e0025c7b83 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Thu, 9 Jan 2025 22:45:31 -0600 Subject: [PATCH] Fix window focus after minimize when not debugging --- src/ManagedShell.WindowsTasks/ApplicationWindow.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ManagedShell.WindowsTasks/ApplicationWindow.cs b/src/ManagedShell.WindowsTasks/ApplicationWindow.cs index 4a476713..fade969f 100644 --- a/src/ManagedShell.WindowsTasks/ApplicationWindow.cs +++ b/src/ManagedShell.WindowsTasks/ApplicationWindow.cs @@ -588,7 +588,7 @@ public void BringToFront() else { // If the window is maximized, use ShowMaximize so that it doesn't un-maximize - if (GetWindowShowStyle(Handle) != NativeMethods.WindowShowStyle.ShowMaximized || + if (GetWindowShowStyle(Handle) != NativeMethods.WindowShowStyle.ShowMaximized || !NativeMethods.ShowWindow(Handle, NativeMethods.WindowShowStyle.ShowMaximized)) { NativeMethods.ShowWindow(Handle, NativeMethods.WindowShowStyle.Show); @@ -603,8 +603,9 @@ public void Minimize() { if ((WindowStyles & (int)NativeMethods.WindowStyles.WS_MINIMIZEBOX) != 0) { - IntPtr retval = IntPtr.Zero; - NativeMethods.SendMessageTimeout(Handle, (int)NativeMethods.WM.SYSCOMMAND, NativeMethods.SC_MINIMIZE, 0, 2, 200, ref retval); + NativeMethods.GetWindowThreadProcessId(Handle, out uint procId); + NativeMethods.AllowSetForegroundWindow(procId); + NativeMethods.PostMessage(Handle, (int)NativeMethods.WM.SYSCOMMAND, (IntPtr)NativeMethods.SC_MINIMIZE, IntPtr.Zero); } }