I create a trayicon in C# WinUI project using Win32. I click the trayicon, the WinUI Flyout will show, but I do not know why if you do not move your mouse, the mouse always be a "busy" state. No error code, I think this is just a situation that should not happen. This problem also occurs in Logitech GHUB, as shown in the figure.
Here is my code on how to handle the tray incident.
internal LRESULT WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_UNIQUE_MESSAGE:
{
if ((uint)(lParam.Value & 0xFFFF) == PInvoke.WM_LBUTTONUP | (uint)(lParam.Value & 0xFFFF) == PInvoke.WM_RBUTTONUP)
{
PInvoke.SetForegroundWindow(hWnd);
Point mousePos;
PInvoke.GetCursorPos(out mousePos);
var scaleFactor = PInvoke.GetDpiForWindow(hWnd) / 96f;
var page = (TrayIconPage)(_source!.Content);
page.ContextFlyout.As<MenuFlyout>().ShowAt(page, new Windows.Foundation.Point((mousePos.X) / scaleFactor, (mousePos.Y) / scaleFactor));
}
break;
}
case PInvoke.WM_DESTROY:
{
DeleteNotifyIcon();
break;
}
case WM_CONTEXTMENU_DOCSLINK:
{
// Bind Xaml to Win32 Windows
_source = new();
var thing = Win32Interop.GetWindowIdFromWindow(hWnd);
_source.Initialize(thing);
_source.Content = new TrayIconPage();
// Indicates that the XAML content will automatically adjust as the parent window is resized, at the same time display Xaml
_source.SiteBridge.ResizePolicy = Microsoft.UI.Content.ContentSizePolicy.ResizeContentToParentWindow;
_source.SiteBridge.Show();
break;
}
default:
{
if (uMsg == _taskbarRestartMessageId)
{
DeleteNotifyIcon();
CreateOrModifyNotifyIcon();
}
return PInvoke.DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
return default;
}
I uploaded this complete project on Github.
The main problem should occur in \BumpyScreen\Taskbar\SystemTrayIcon.cs.
My code for creating tray icons basically comes from Files. But Files use Win32 context menu, so this situation will not happen in Files.
But this situation happen in MicaForEveryone, although I and MicaFE used similar code. I try to create page(to display flyout) from WinUI window, but same thing happed too.
I guess this may be because the show of WinUI Flyout has caused some messages in the window loop to be blocked, but this blocking will not cause program errors, but will cause your mouse to be in such a busy state all the time(This also indicates that something is busy doing sth? :)
This is just a small problem that bothers obsessive compulsive disorder >.<

WaitCursor. It can occur on WinOS whether its WinUI, WinForms, Maui-Windows or WPF. The way I've always dealt with it is is to programmatically move the Cursor a few pixels. Timing is everything, so you may want to dispatch these programmatic cursor moves to the end of the message queue or await whatever action is being performed. Things likeBeginInvokeorDispatchAsyncto post those cursor commands can help.