C# 即使在应用程序关闭后,NotifyIcon 仍保留在托盘中,但在鼠标悬停时消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14723843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NotifyIcon remains in Tray even after application closing but disappears on Mouse Hover
提问by Swanand
There are many questions on SO asking same doubt. Solution for this is to set
关于 SO 有很多问题提出了同样的疑问。解决方案是设置
notifyIcon.icon = null
and calling Dispose
for it in FormClosing event.
notifyIcon.icon = null
并Dispose
在 FormClosing 事件中调用它。
In my application, there is no such form but has Notification icon which updates on Events.
On creation, I hide my form and make ShowInTaskbar
property false
. Hence I can not have a "FormClosing" or "FormClosed" events.
在我的应用程序中,没有这样的表单,但有更新事件的通知图标。在创建时,我隐藏了我的表单并创建了ShowInTaskbar
property false
。因此我不能有“FormClosing”或“FormClosed”事件。
If this application gets event to exit, It calls Process.GetCurrentProcess().Kill();
to exit.
如果此应用程序获得退出事件,则调用Process.GetCurrentProcess().Kill();
退出。
I have added notifyIcon.icon = null
as well as Dispose before killing, but still icon remains taskbar until I hover mouse over it.
我notifyIcon.icon = null
在杀死之前添加了以及处理,但图标仍然保留在任务栏上,直到我将鼠标悬停在它上面。
EDIT: If I assume that this behaviour is due to calling GetCurrentProcess().Kill()
, Is there any elegant way to exit from application which will clear all resources and remove icon from system tray.
编辑:如果我认为这种行为是由于调用GetCurrentProcess().Kill()
,是否有任何优雅的方式退出应用程序,这将清除所有资源并从系统托盘中删除图标。
回答by Matthew Watson
This is normal behaviour, unfortunately; it's due to the way Windows works. You can'r really do anything about it.
不幸的是,这是正常的行为;这是由于 Windows 的工作方式。你真的无能为力。
See Issue with NotifyIcon not dissappearing on Winforms Appfor some suggestions, but none of them ever worked for me.
有关一些建议,请参阅NotifyIcon 在 Winforms App 上不会消失的问题,但没有一个对我有用。
Also see Notify Icon stays in System Tray on Application Close
Microsoft have marked this as "won't fix" on Microsoft Connect.
Microsoft 已在 Microsoft Connect 上将此标记为“无法修复”。
回答by Jason Dias
You can either set
您可以设置
notifyIcon1.Visible = false;
OR
或者
notifyIcon.Icon = null;
in the form closing event.
在表单关闭事件中。
回答by victor
I had the exact same problem as you.
我和你有完全一样的问题。
The proper way are send WM_CLOSE message to a process.
I use the c# code I found in this article.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way
正确的方法是向进程发送 WM_CLOSE 消息。
我使用在本文中找到的 c# 代码。
http://social.msdn.microsoft.com/Forums/vstudio/en-US/82992842-80eb-43c8-a9e6-0a6a1d19b00f/terminating-a-process-in-a-friendly-way
回答by Lanello
i can tell you can solve the problem simply using the .dispose() method, but that is not called if you kill the process instead of exit the application.
我可以告诉您只需使用 .dispose() 方法即可解决问题,但如果您终止进程而不是退出应用程序,则不会调用该方法。
please refer to Application.Exitif you have built a simple Windows Form application else refer to Environment.Exitthat is more general.
如果您构建了一个简单的 Windows 窗体应用程序,请参阅Application.Exit,否则请参阅更通用的Environment.Exit。
回答by Ismael
Use notifyIcon.Visible = False
in FormClosing
event
使用notifyIcon.Visible = False
在FormClosing
事件
回答by Sinaesthetic
I don't think WPF has it's own NotifyIcon, does it? If you're using the 3rd party Harcodet.Wpf.TaskbarNotification, then try this:
我不认为 WPF 有它自己的 NotifyIcon,是吗?如果您使用的是第 3 方 Harcodet.Wpf.TaskbarNotification,请尝试以下操作:
In order to prevent my app from closing when the window is closed (run in background), I separated the logic for closing the window (hitting the x button in the upper right) and actually shutting it down (through the context menu). To make this work, make your context menu set _isExplicitClose
to true. Otherwise, it'll just hide the window and continue to run.
为了防止我的应用程序在窗口关闭时关闭(在后台运行),我将关闭窗口(点击右上角的 x 按钮)和实际关闭它(通过上下文菜单)的逻辑分开。要使其工作,请将上下文菜单设置_isExplicitClose
为 true。否则,它只会隐藏窗口并继续运行。
What this does is, on explicit close, hide tray icon and the form before closing. This way the icon isn't hanging around after the application is shutdown.
这样做是,在显式关闭时,在关闭之前隐藏托盘图标和表单。这样,在应用程序关闭后,图标就不会出现。
private bool _isExplicitClose;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (!_isExplicitClose)
{
e.Cancel = true;
Hide();
}
}
protected void QuitService(object sender, RoutedEventArgs e)
{
_isExplicitClose = true;
TaskbarIcon.Visibility = Visibility.Hidden;
Close();
}
回答by Cubi73
Try Application.DoEvents();
after setting notifyIcon.Icon
to null
and disposing:
Application.DoEvents();
设置notifyIcon.Icon
到配置后试试null
:
notifyIcon.Icon = null;
notifyIcon.Dispose();
Application.DoEvents();
And consider Environment.Exit(0);
instead of Process.GetCurrentProcess().Kill()
.
并考虑Environment.Exit(0);
代替Process.GetCurrentProcess().Kill()
.
回答by Pouya
Use this code when you want to do it when you press the Exit or Close button:
如果您想在按下退出或关闭按钮时执行此操作,请使用此代码:
private void ExitButton_Click(object sender, EventArgs e)
{
notifyIcon.Dispose();
Application.Exit(); // or this.Close();
}
Use this code when you want to do it when the form is closing:
如果您想在表单关闭时执行此操作,请使用此代码:
private void Form1_FormClosing(object sender, EventArgs e)
{
notifyIcon.Dispose();
Application.Exit(); // or this.Close();
}
The important code is this:
重要的代码是这样的:
notifyIcon.Dispose();
回答by The Muffin Man
The only solution that worked for me was to use the Closed event and hide and dispose of the icon.
对我有用的唯一解决方案是使用 Closed 事件并隐藏和处理图标。
icon.BalloonTipClosed += (sender, e) => {
var thisIcon = (NotifyIcon)sender;
thisIcon.Visible = false;
thisIcon.Dispose();
};
回答by Michael Pet?falvi
The right answer has already been given. But you must also provide a delay, for example with a timer. Only then the application can still remove the icon in the background.
已经给出了正确的答案。但是您还必须提供延迟,例如使用计时器。只有这样,应用程序仍然可以在后台删除图标。
private System.Windows.Forms.Timer mCloseAppTimer;
private void ExitButton_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false; notifyIcon.Dispose;
mCloseAppTimer = new System.Windows.Forms.Timer();
mCloseAppTimer.Interval = 100;
mCloseAppTimer.Tick += new EventHandler(OnCloseAppTimerTick);
}
private void OnCloseAppTimerTick(object sender, EventArgs e)
{
Environment.Exit(0); // other exit codes are also possible
}