1

I have created an MAUI app and I want to hide its icon from Taskbar:

enter image description here

Anyone knows how to do it?

I was searching for a solution in internet but without success. I expect to have a simple solution. Thanks in advance!

1
  • No, they want to hide the icon, not the whole taskbar. I assume (and hope) this is impossible to do programmatically; it smells of malware. It is up to the user and the OS to control what is visible to user. Commented Jul 27, 2023 at 19:35

1 Answer 1

2

Because the Maui for Windows is based on the WinUI 3, so I refer to this case about Removing the window from the taskbar in WinUI 3. I have tested it and the icon will be hiden.

Add the method in the /Platforms/Windows/App.cs:

public partial class App : MauiWinUIApplication
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        InitializeComponent();
    }

    #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

    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}

And in the MainPage.cs:

    protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();
#if WINDOWS
        Microsoft.UI.Xaml.Window window = (Microsoft.UI.Xaml.Window)App.Current.Windows.First<Window>().Handler.PlatformView;
        IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
        int exStyle = (int)GetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE);
        exStyle |= (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW;
        SetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE, (IntPtr)exStyle);
#endif
      }

And the result image:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

I did exactly what you mentioned above but, in the MainPage.cs UI is not found in Windows namespace, which package I need to install?
You don't have to install any package. Which api or method is not found? Can you show more details? The code I provided worked well in my project. Did you copy it? @BlerimSherifi
You can just copy my code to your project and you can use the fisrt content page in your project instead of the MainPage. @BlerimSherifi
I did that but, I am getting build errors.
Can you show the details such as the image or error message? @BlerimSherifi
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.