Browse Source

Fix ShowInTaskbar on Win32.

To hide the taskbar icon on win32 we need to parent the window to a hidden window.
pull/5715/head
Steven Kirk 6 years ago
parent
commit
1bb6fad799
  1. 33
      src/Windows/Avalonia.Win32/WindowImpl.cs

33
src/Windows/Avalonia.Win32/WindowImpl.cs

@ -85,6 +85,7 @@ namespace Avalonia.Win32
private ExtendClientAreaChromeHints _extendChromeHints = ExtendClientAreaChromeHints.Default; private ExtendClientAreaChromeHints _extendChromeHints = ExtendClientAreaChromeHints.Default;
private bool _isCloseRequested; private bool _isCloseRequested;
private bool _shown; private bool _shown;
private WindowImpl _hiddenWindow;
public WindowImpl() public WindowImpl()
{ {
@ -581,7 +582,7 @@ namespace Avalonia.Win32
public void SetParent(IWindowImpl parent) public void SetParent(IWindowImpl parent)
{ {
_parent = (WindowImpl)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); 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() private void CreateDropTarget()
{ {
var odt = new OleDropTarget(this, _owner); var odt = new OleDropTarget(this, _owner);
@ -1094,16 +1100,35 @@ namespace Avalonia.Win32
if (newProperties.ShowInTaskbar) if (newProperties.ShowInTaskbar)
{ {
exStyle |= WindowStyles.WS_EX_APPWINDOW; 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 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; exStyle &= ~WindowStyles.WS_EX_APPWINDOW;
} }
SetExtendedStyle(exStyle); 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; WindowStyles style;

Loading…
Cancel
Save