简体   繁体   中英

Hide dummy taskbar icon for notification icon context menu

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:

在此处输入图片说明

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?

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);
}

Try Me.Hide()

lets see the outcome

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM