C# NotifyIcon从任务栏隐藏应用程序。如何避免这种情况?
我有一个应用程序,当我最小化它时,它会进入系统托盘。我创建了一个通知图标来处理应用程序的一些辅助选项,使用鼠标右键单击通知图标 但是我希望在最小化任务栏并保留系统托盘上的通知图标时,该应用程序不会从任务栏中消失。 有什么办法完成任务吗 编辑:当我最小化应用程序时,我使用Hide()命令来使用notify图标。但我希望它留在任务栏上 请参见此处的代码:C# NotifyIcon从任务栏隐藏应用程序。如何避免这种情况?,c#,.net,wpf,winforms,notifyicon,C#,.net,Wpf,Winforms,Notifyicon,我有一个应用程序,当我最小化它时,它会进入系统托盘。我创建了一个通知图标来处理应用程序的一些辅助选项,使用鼠标右键单击通知图标 但是我希望在最小化任务栏并保留系统托盘上的通知图标时,该应用程序不会从任务栏中消失。 有什么办法完成任务吗 编辑:当我最小化应用程序时,我使用Hide()命令来使用notify图标。但我希望它留在任务栏上 请参见此处的代码: private void MainWindow_OnStateChanged(object sender, EventArgs e) {
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized) return;
Hide();
ShowInTaskbar = true;
if (notifyIcon != null)
notifyIcon.ShowBalloonTip(2000);
}
注意:此NotifyIcon
以编程方式嵌入到WPF容器上,如下所示:
DrawNotifyIcon();
private void DrawNotifyIcon()
{
try
{
string source = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MainWindow)).CodeBase);
string tmpSource = source + @"\Resources\mainico.ico";
tmpSource = tmpSource.Replace(@"file:\", "");
// notify Icon
notifyIcon = new NotifyIcon
{
BalloonTipTitle = Cultures.Resources.Title,
BalloonTipText = Cultures.Resources.NotifyIconExecuting,
BalloonTipIcon = ToolTipIcon.Info,
Icon = new System.Drawing.Icon(tmpSource)
};
notifyIcon.DoubleClick += notifyIcon_DoubleClick;
notifyIcon.Click += notifyIcon_Click;
notifyIcon.MouseUp += notifyIcon_MouseUp;
// Create ContextMenu
contextMenu = new ContextMenuStrip();
contextMenu.Closing += contextMenu_Closing;
// Exit item
menuItemExit = new ToolStripMenuItem
{
Text = Cultures.Resources.Exit,
Image = Cultures.Resources.close
};
menuItemExit.Click += menuItemExit_Click;
// Restore item
menuItemRestore = new ToolStripMenuItem
{
Text = Cultures.Resources.Restore,
Image = Cultures.Resources.restore1
};
menuItemRestore.Click += menuItemRestore_Click;
// Active or inactive log
menuItemActive = new ToolStripMenuItem
{
Text = Cultures.Resources.On,
Image = Cultures.Resources.green,
Checked = true
};
menuItemActive.Click += menuItemActive_Click;
menuItemActive.CheckStateChanged += menuItemActive_CheckStateChanged;
// Order of appearance of ContextMenu items
contextMenu.Items.Add(menuItemActive);
contextMenu.Items.Add("-");
contextMenu.Items.Add(menuItemRestore);
contextMenu.Items.Add(menuItemExit);
notifyIcon.ContextMenuStrip = contextMenu;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
你知道如何为WPF保留两个图标吗?好吧,在处于隐藏状态的任务栏中显示表单是不可能的。仍然可以强制最小化表单。请尝试以下修改的代码:
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized) return;
this.ShowInTaskbar = true;
if (notifyIcon != null)
notifyIcon.ShowBalloonTip(2000);
this.WindowState = FormWindowState.Minimized;
}
只需删除该代码。但如果我删除该代码,我将错过Notify图标…您的用户不会错过它。如果它足够重要,那么他将确保它不会在图标溢出区域消失。当然,你也可以。单击通知区域中的三角形,然后单击“自定义”。@HansPassant谢谢。我看到它在WinForms上工作,但我正在使用WPF并在WinForms容器中嵌入NotifyIcon。所以我想我错过了什么?这段代码使窗体在任务栏上最小化,但我需要在系统托盘上显示Notify图标,并正常最小化应用程序。