简体   繁体   English

隐藏通知图标上下文菜单的虚拟任务栏图标

[英]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:我有一个简单的 .Net 5.0 WinForm 表单,它在应用程序启动后自动隐藏:

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).如果表单未隐藏或右键单击(通过 ContextMenuStrip 属性)显示上下文菜单条,则不会发生这种情况。

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.尝试将您的上下文菜单分配给 notifyicon s .ContextMenu property like and use the notifyicon .ContextMenu s .ContextMenu property like and use the notifyicon s .Visible 属性来显示它,而不是使用 .Show 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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