如何在.NET MAUI桌面应用程序中隐藏任务栏图标

huangapple go评论104阅读模式
英文:

How to hide Taskbar icon for .NET MAUI desktop application

问题

我已经创建了一个 MAUI 应用程序,我想要隐藏它在任务栏中的图标:

有人知道如何做到吗?

我在互联网上搜索了解决方案,但没有成功。我希望能有一个简单的解决方案。提前谢谢!

英文:

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

如何在.NET MAUI桌面应用程序中隐藏任务栏图标

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

得分: 2

因为Maui for Windows基于WinUI 3,所以我参考了这个关于在WinUI 3中从任务栏中移除窗口的案例。我已经测试过了,图标会被隐藏。

在**/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();
}

在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
    }

结果图片:

如何在.NET MAUI桌面应用程序中隐藏任务栏图标

英文:

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
{
/// &lt;summary&gt;
/// 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().
/// &lt;/summary&gt;
public App()
{
InitializeComponent();
}
#region Window styles
[Flags]
public enum ExtendedWindowStyles
{
// ...
WS_EX_TOOLWINDOW = 0x00000080,
// ...
}
public enum GetWindowLongFields
{
// ...
GWL_EXSTYLE = (-20),
// ...
}
[DllImport(&quot;user32.dll&quot;)]
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&#39;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) &amp;&amp; (error != 0))
{
throw new System.ComponentModel.Win32Exception(error);
}
return result;
}
[DllImport(&quot;user32.dll&quot;, EntryPoint = &quot;SetWindowLongPtr&quot;, SetLastError = true)]
private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport(&quot;user32.dll&quot;, EntryPoint = &quot;SetWindowLong&quot;, 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(&quot;kernel32.dll&quot;, EntryPoint = &quot;SetLastError&quot;)]
public static extern void SetLastError(int dwErrorCode);
#endregion
protected override MauiApp CreateMauiApp() =&gt; 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&lt;Window&gt;().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:

如何在.NET MAUI桌面应用程序中隐藏任务栏图标

huangapple
  • 本文由 发表于 2023年7月27日 18:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定