Hide dummy taskbar icon for notification icon context menu

144 Views Asked by At

I have a simple .Net 5.0 WinForm form which is automatically hidden after the application started:

private void form_Shown (object sender, EventArgs e)
{
    Hide ();
}

It only creates a notify icon. This notify icon has a context menu strip that is shown by a left mouse click:

private void notifyIcon_Click (object sender, EventArgs e)
{
    contextMenuStrip.Show (MousePosition);
}

This works fine, but as long as the context menu strip is visible, a dummy taskbar icon appears:

enter image description here

This doesn't happen if the form is not hidden or the context menu strip is shown by a right mouse click (via ContextMenuStrip property).

How can I prevent this icon?

2

There are 2 best solutions below

1
Sola Oshinowo On

Try Me.Hide()

lets see the outcome

Try assign your context menu to the notifyicons .ContextMenu property like and use the notifyicons .Visible property to show it instead of using the .Show method.

0
André On

I finally found a solution myself:

[DllImport ("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern bool SetForegroundWindow (HandleRef hWnd);

private void notifyIcon_Click (object sender, EventArgs e)
{
    SetForegroundWindow (new HandleRef (notifyIcon.ContextMenuStrip, notifyIcon.ContextMenuStrip.Handle));
    notifyIcon.ContextMenuStrip.Show (MousePosition);
}