0

I'm creating an app that needs UI but not show up inside the taskbar, I know exactly how to do this in, winforms and wpf, but the window object doesn't contain a ShowInTaskBar variable that I can just change.

I have tried NotifyIcon but that doesn't hide it from the taskbar, I also can't find any documentation on if this is even possible.

I only create a stack overflow question when I've spent more than 5 hours on this, so any help would be appreciated.

GHOST
  • 337
  • 3
  • 9
  • 1
    You can find your answer here: https://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher#:~:text=There%20are%20two%20ways%20of%20hiding%20a%20window,make%20it%20a%20child%20window%20of%20another%20window. – Nick Oct 31 '22 at 10:21
  • @Nick your link seems to be for WPF. I've just tried it for WinUI3 and `ShowInTaskbar` doesn't seem available on the `Window` control. (The OP already says that in the question) – JHBonarius Oct 31 '22 at 10:46
  • @JHBonarius, you are not being careful. :-) Look at the solution which involves changing the style of the window. – Nick Oct 31 '22 at 10:54
  • "Careful"? I think something got lost in translation there. Anyhow, that question has many many answers, most of which don't apply to WinUI3, so it's a needle in a haystack. Please be specific. – JHBonarius Oct 31 '22 at 10:59
  • @JHBonarius I applied it and it actually did work, I just had to add the window handle to in the win32 api and it worked. – GHOST Oct 31 '22 at 11:11
  • @JHBonarius im using C# too? – GHOST Oct 31 '22 at 13:24
  • ah, sorry, I'm not used to low-level WinRt access with c#. So misread this for C++. It's pretty ugly way of having to write code. Microsoft should definitely abstract is away... but WinUI3 has many issues like this in my experience. – JHBonarius Oct 31 '22 at 14:27

2 Answers2

1

Nick showed me this post https://stackoverflow.com/a/551847/14724147

Just add the window handle to the win32 api and it will hide it from alt+tab and the taskbar

public MainWindow() 
{
    InitializeComponent();

    IntPtr hWnd = WindowNative.GetWindowHandle(this);
    int exStyle = (int)GetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE);
    exStyle |= (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW;
    SetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE, (IntPtr)exStyle);
}

#region Window styles
[Flags]
public enum ExtendedWindowStyles
{
    // ...
    WS_EX_TOOLWINDOW = 0x00000080,
    // ...
}

public enum GetWindowLongFields
{
    // ...
    GWL_EXSTYLE = (-20),
    // ...
}

[DllImport("user32.dll")]
public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
{
    int error = 0;
    IntPtr result = IntPtr.Zero;
    // Win32 SetWindowLong doesn't clear error on success
    SetLastError(0);

    if (IntPtr.Size == 4)
    {
        // use SetWindowLong
        Int32 tempResult = IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong));
        error = Marshal.GetLastWin32Error();
        result = new IntPtr(tempResult);
    }
    else
    {
        // use SetWindowLongPtr
        result = IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
        error = Marshal.GetLastWin32Error();
    }

    if ((result == IntPtr.Zero) && (error != 0))
    {
        throw new System.ComponentModel.Win32Exception(error);
    }

    return result;
}

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

[DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
private static extern Int32 IntSetWindowLong(IntPtr hWnd, int nIndex, Int32 dwNewLong);

private static int IntPtrToInt32(IntPtr intPtr)
{
    return unchecked((int)intPtr.ToInt64());
}

[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(int dwErrorCode);
#endregion
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
GHOST
  • 337
  • 3
  • 9
0

I'm the author of H.NotifyIcon and I recently added extensions for the Window type - ShowInTaskbar and HideInTaskbar You can use them to achieve the desired behavior.

I think you just needed to write about it in issues on GitHub

Konstantin S.
  • 1,307
  • 14
  • 19
  • yes, recently. I posted this about 9 months ago so going to the issues on GitHub wouldn't have helped at all because the feature didn't exist back then – GHOST Aug 10 '23 at 16:57