lepoco / lepoco/wpfui

NotifyIcon.ParentHandle unused?

Open
#1,256 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
9.6k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

I wanted to set the parent handle directly instead of using the automatic mechanism which tries to get the HwndSource from the main window, which might not be available yet due to the window being hidden at startup.

It seems that the property ParentHandle isn't used at all, neither by the NotifyIcon class nor the InternalNotifyIconManager.

To Reproduce
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    var mainWindow = new MainWindow();
    MainWindow = mainWindow;

    var helper = new WindowInteropHelper(mainWindow);

    var trayIcon = new NotifyIcon()
    {
        Icon = new BitmapImage(new Uri("pack://application:,,,/Resources/icon.ico")),
        ContextMenu = new ContextMenu()
        {
            Items =
            {
                new MenuItem()
                {
                    Header = "Exit",
                }
            }
        },
        ParentHandle = helper.EnsureHandle(),
        TooltipText = "Test",
    };

    trayIcon.Register();
    _trayIcon = trayIcon;
}
Expected behavior

The InternalNotifyIconManager should use the ParentHandle instead of trying to find the handle of the main window.

Screenshots

No response

OS version

Win 11 24H2

.NET version

.NET Framework 4.7.2

WPF-UI NuGet version

4.0.0-rc.2

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting the NotifyIcon and InternalNotifyIconManager classes to trace how the parent window handle is selected during registration. Verify whether ParentHandle is ignored, then make the manager use the explicitly assigned handle and confirm that registration works when the main window is hidden at startup.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.