Browse Source

Merge pull request #4928 from MarchingCube/fix-win32-dialog-minimizing-parent

Fix closing dialog windows on win32 causing parent window to be moved to back
pull/4934/head
danwalmsley 6 years ago
committed by GitHub
parent
commit
d19bcfb1de
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
  2. 18
      src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs

6
src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs

@ -1017,6 +1017,9 @@ namespace Avalonia.Win32.Interop
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool ValidateRect(IntPtr hWnd, IntPtr lpRect); public static extern bool ValidateRect(IntPtr hWnd, IntPtr lpRect);
[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool IsWindowEnabled(IntPtr hWnd); public static extern bool IsWindowEnabled(IntPtr hWnd);
@ -1050,6 +1053,9 @@ namespace Avalonia.Win32.Interop
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint); public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetActiveWindow();
[DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetActiveWindow(IntPtr hWnd); public static extern IntPtr SetActiveWindow(IntPtr hWnd);

18
src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs

@ -65,6 +65,24 @@ namespace Avalonia.Win32
return IntPtr.Zero; return IntPtr.Zero;
} }
// Based on https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L4270-L4337
// We need to enable parent window before destroying child window to prevent OS from activating a random window behind us.
// This is described here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablewindow#remarks
// Our window closed callback will set enabled state to a correct value after child window gets destroyed.
// We need to verify if parent is still alive (perhaps it got destroyed somehow).
if (_parent != null && IsWindow(_parent._hwnd))
{
var wasActive = GetActiveWindow() == _hwnd;
_parent.SetEnabled(true);
// We also need to activate our parent window since again OS might try to activate a window behind if it is not set.
if (wasActive)
{
SetActiveWindow(_parent._hwnd);
}
}
break; break;
} }

Loading…
Cancel
Save