From 6e911e8aed8b2338469f7da4e51975d46749f40a Mon Sep 17 00:00:00 2001 From: lwz-sabrina <82400605+lwz-sabrina@users.noreply.github.com> Date: Tue, 5 Aug 2025 17:07:09 +0800 Subject: [PATCH] Enable tray icon creation in all circumstances when running as an admin (#19382) * Fix trayicon unable to create * Revert "Fix trayicon unable to create" This reverts commit 4f397dbcdfccea1b0865f3993dbba526802c3f71. * Fix trayicon unable to create * Use ChangeWindowMessageFilterEx in Win32Platform.CreateMessageWindow * modify the format * Change the enum from ChangeWindowMessageFilterEx * Change the enum from ChangeWindowMessageFilterEx --- .../Avalonia.Win32/Interop/UnmanagedMethods.cs | 15 +++++++++++++++ src/Windows/Avalonia.Win32/TrayIconImpl.cs | 7 ++++++- src/Windows/Avalonia.Win32/Win32Platform.cs | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index ed5c7ba85c..1177334680 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1502,6 +1502,21 @@ namespace Avalonia.Win32.Interop [DllImport("shell32", CharSet = CharSet.Auto)] public static extern int Shell_NotifyIcon(NIM dwMessage, NOTIFYICONDATA lpData); + [DllImport("user32.dll", SetLastError = true)] + public static extern bool ChangeWindowMessageFilterEx( + IntPtr hWnd, + uint message, + MessageFilterFlag action, + IntPtr pChangeFilterStruct + ); + + public enum MessageFilterFlag + { + MSGFLT_RESET = 0, + MSGFLT_ALLOW = 1, + MSGFLT_DISALLOW = 2, + } + [DllImport("shell32", CharSet = CharSet.Auto)] public static extern nint SHAppBarMessage(AppBarMessage dwMessage, ref APPBARDATA lpData); diff --git a/src/Windows/Avalonia.Win32/TrayIconImpl.cs b/src/Windows/Avalonia.Win32/TrayIconImpl.cs index 0199e1bc74..fdb6327a85 100644 --- a/src/Windows/Avalonia.Win32/TrayIconImpl.cs +++ b/src/Windows/Avalonia.Win32/TrayIconImpl.cs @@ -37,7 +37,12 @@ namespace Avalonia.Win32 new PixelSize(32, 32), new Vector(96, 96), PixelFormats.Bgra8888, AlphaFormat.Unpremul); s_emptyIcon = new Win32Icon(bitmap); } - + + internal static void ChangeWindowMessageFilter(IntPtr hWnd) + { + ChangeWindowMessageFilterEx(hWnd, WM_TASKBARCREATED, MessageFilterFlag.MSGFLT_ALLOW, IntPtr.Zero); + } + public TrayIconImpl() { FindTaskBarMonitor(); diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index e9df0d04cc..c7708f2c86 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -207,6 +207,8 @@ namespace Avalonia.Win32 { throw new Win32Exception(); } + + TrayIconImpl.ChangeWindowMessageFilter(_hwnd); } public ITrayIconImpl CreateTrayIcon()