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

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

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**中添加以下方法:

  1. public partial class App : MauiWinUIApplication
  2. {
  3. /// <summary>
  4. /// Initializes the singleton application object. This is the first line of authored code
  5. /// executed, and as such is the logical equivalent of main() or WinMain().
  6. /// </summary>
  7. public App()
  8. {
  9. InitializeComponent();
  10. }
  11. #region Window styles
  12. [Flags]
  13. public enum ExtendedWindowStyles
  14. {
  15. // ...
  16. WS_EX_TOOLWINDOW = 0x00000080,
  17. // ...
  18. }
  19. public enum GetWindowLongFields
  20. {
  21. // ...
  22. GWL_EXSTYLE = (-20),
  23. // ...
  24. }
  25. [DllImport("user32.dll")]
  26. public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
  27. public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
  28. {
  29. int error = 0;
  30. IntPtr result = IntPtr.Zero;
  31. // Win32 SetWindowLong doesn't clear error on success
  32. SetLastError(0);
  33. if (IntPtr.Size == 4)
  34. {
  35. // use SetWindowLong
  36. Int32 tempResult = IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong));
  37. error = Marshal.GetLastWin32Error();
  38. result = new IntPtr(tempResult);
  39. }
  40. else
  41. {
  42. // use SetWindowLongPtr
  43. result = IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
  44. error = Marshal.GetLastWin32Error();
  45. }
  46. if ((result == IntPtr.Zero) && (error != 0))
  47. {
  48. throw new System.ComponentModel.Win32Exception(error);
  49. }
  50. return result;
  51. }
  52. [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
  53. private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  54. [DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
  55. private static extern Int32 IntSetWindowLong(IntPtr hWnd, int nIndex, Int32 dwNewLong);
  56. private static int IntPtrToInt32(IntPtr intPtr)
  57. {
  58. return unchecked((int)intPtr.ToInt64());
  59. }
  60. [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
  61. public static extern void SetLastError(int dwErrorCode);
  62. #endregion
  63. protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
  64. }

在MainPage.cs中:

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

结果图片:

如何在.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:

  1. public partial class App : MauiWinUIApplication
  2. {
  3. /// &lt;summary&gt;
  4. /// Initializes the singleton application object. This is the first line of authored code
  5. /// executed, and as such is the logical equivalent of main() or WinMain().
  6. /// &lt;/summary&gt;
  7. public App()
  8. {
  9. InitializeComponent();
  10. }
  11. #region Window styles
  12. [Flags]
  13. public enum ExtendedWindowStyles
  14. {
  15. // ...
  16. WS_EX_TOOLWINDOW = 0x00000080,
  17. // ...
  18. }
  19. public enum GetWindowLongFields
  20. {
  21. // ...
  22. GWL_EXSTYLE = (-20),
  23. // ...
  24. }
  25. [DllImport(&quot;user32.dll&quot;)]
  26. public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
  27. public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
  28. {
  29. int error = 0;
  30. IntPtr result = IntPtr.Zero;
  31. // Win32 SetWindowLong doesn&#39;t clear error on success
  32. SetLastError(0);
  33. if (IntPtr.Size == 4)
  34. {
  35. // use SetWindowLong
  36. Int32 tempResult = IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong));
  37. error = Marshal.GetLastWin32Error();
  38. result = new IntPtr(tempResult);
  39. }
  40. else
  41. {
  42. // use SetWindowLongPtr
  43. result = IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
  44. error = Marshal.GetLastWin32Error();
  45. }
  46. if ((result == IntPtr.Zero) &amp;&amp; (error != 0))
  47. {
  48. throw new System.ComponentModel.Win32Exception(error);
  49. }
  50. return result;
  51. }
  52. [DllImport(&quot;user32.dll&quot;, EntryPoint = &quot;SetWindowLongPtr&quot;, SetLastError = true)]
  53. private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  54. [DllImport(&quot;user32.dll&quot;, EntryPoint = &quot;SetWindowLong&quot;, SetLastError = true)]
  55. private static extern Int32 IntSetWindowLong(IntPtr hWnd, int nIndex, Int32 dwNewLong);
  56. private static int IntPtrToInt32(IntPtr intPtr)
  57. {
  58. return unchecked((int)intPtr.ToInt64());
  59. }
  60. [DllImport(&quot;kernel32.dll&quot;, EntryPoint = &quot;SetLastError&quot;)]
  61. public static extern void SetLastError(int dwErrorCode);
  62. #endregion
  63. protected override MauiApp CreateMauiApp() =&gt; MauiProgram.CreateMauiApp();
  64. }

And in the MainPage.cs:

  1. protected override void OnHandlerChanged()
  2. {
  3. base.OnHandlerChanged();
  4. #if WINDOWS
  5. Microsoft.UI.Xaml.Window window = (Microsoft.UI.Xaml.Window)App.Current.Windows.First&lt;Window&gt;().Handler.PlatformView;
  6. IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
  7. int exStyle = (int)GetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE);
  8. exStyle |= (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW;
  9. SetWindowLong(hWnd, (int)GetWindowLongFields.GWL_EXSTYLE, (IntPtr)exStyle);
  10. #endif
  11.       }

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:

确定