0

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.

Busy state mouse

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 >.<

6
  • 1
    I'm familiar with what you describe where the mouse isn't "really" busy but the icon sticks at 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 like BeginInvoke or DispatchAsync to post those cursor commands can help. Commented Feb 20 at 20:34
  • There is no doubt that this action can get rid of busy icon. The root of this method is to keep Flyout away from the cursor, because I can avoid being busy icon by trying to show Flyout away from the mouse position. It's just that I'm confused about why there is a false busyness :) But thank you very much for your answer Commented Feb 21 at 3:23
  • I don't quite reproduce on my PC. If I click the tray icon, I see the mouse going to busy state but it just goes back to normal, very quickly i.imgur.com/SEYiMwG.mp4 Commented Feb 21 at 7:34
  • To be honest, I do not know exactly how the problem happened. I try it on two computers with different cpu (Of course, both were Win11), but both Test 1 and Test 2 pointed to the same result, I'm not sure if you accidentally moved the mouse when you clicked it or for other reasons >.< Commented Feb 21 at 15:21
  • 1
    FYI, I've made a sample WinU3 + NotifyIcon project github.com/smourier/WinUI3NotifyIcon which works a bit differently Commented Feb 21 at 18:12

0

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.