From 1bb6fad799ba644f30a199cb851c539d00219335 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 26 Mar 2021 10:21:40 +0100 Subject: [PATCH] Fix ShowInTaskbar on Win32. To hide the taskbar icon on win32 we need to parent the window to a hidden window. --- src/Windows/Avalonia.Win32/WindowImpl.cs | 33 +++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) 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;