diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index a42dd5fc07..e242c7b247 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -85,6 +85,7 @@ namespace Avalonia.Win32 private ExtendClientAreaChromeHints _extendChromeHints = ExtendClientAreaChromeHints.Default; private bool _isCloseRequested; private bool _shown; + private WindowImpl _hiddenWindow; public WindowImpl() { @@ -581,7 +582,7 @@ namespace Avalonia.Win32 public void SetParent(IWindowImpl parent) { _parent = (WindowImpl)parent; - SetWindowLongPtr(_hwnd, (int)WindowLongParam.GWL_HWNDPARENT, _parent._hwnd); + SetWindowLongPtr(_hwnd, (int)WindowLongParam.GWL_HWNDPARENT, _parent?._hwnd ?? IntPtr.Zero); } public void SetEnabled(bool enable) => EnableWindow(_hwnd, enable); @@ -755,6 +756,11 @@ namespace Avalonia.Win32 } } + private WindowImpl GetOrCreateHiddenWindow() + { + return _hiddenWindow ??= new WindowImpl(); + } + private void CreateDropTarget() { var odt = new OleDropTarget(this, _owner); @@ -1094,16 +1100,35 @@ namespace Avalonia.Win32 if (newProperties.ShowInTaskbar) { exStyle |= WindowStyles.WS_EX_APPWINDOW; + + if (_hiddenWindow is object && _parent == _hiddenWindow) + { + // Can't enable the taskbar icon by clearing the parent window unless the window + // is hidden. Hide the window and show it again with the same activation state + // when we've finished. Interestingly it seems to work fine the other way. + var shown = IsWindowVisible(_hwnd); + var activated = GetActiveWindow() == _hwnd; + + if (shown) + Hide(); + + SetParent(null); + + if (shown) + Show(activated); + } + } else { + // To hide a non-owned window's taskbar icon we need to parent it to a hidden window. + if (_parent is null) + SetParent(GetOrCreateHiddenWindow()); + exStyle &= ~WindowStyles.WS_EX_APPWINDOW; } SetExtendedStyle(exStyle); - - // TODO: To hide non-owned window from taskbar we need to parent it to a hidden window. - // Otherwise it will still show in the taskbar. } WindowStyles style;