C#关闭到系统托盘(如MSN信使) [英] C# close to tray (like msn messenger)
问题描述
我有一个C#.net应用程序。所以,我创建了一个在盘坐在一个NotifyIcon的。我想做的是当用户点击窗体上的X按钮,我希望它关闭托盘。他们应该只能够通过使用托盘图标的上下文菜单,退出程序。
I have a c# .net app. So I created a notifyIcon that sits in the tray. What I want to do is when the user hits the "x" button on the form, I want it to close to the tray. They should only be able to exit program by using the context menu in the tray icon.
所以我所做的就是,在表单关闭事件,我检查是否形式是可见的。如果它的可见的,我把它设置为不可见,并设置showInTaskbar为假(模拟最小化到托盘)如果表单是不可见已经,那么他们很可能从托盘关闭它,所以我将退出在这种情况下程序。
So what I did was, on the form close event, I check whether the form is visible. If its visible, i set it to invisible and set showInTaskbar to false (simulating minimize to tray) If the form is invisible already, then they are probably closing it from the tray, so I will exit the program in that case.
不过,我的问题是,如果窗口是可见的,但他们右键单击任务栏图标的上下文菜单和命中退出,我需要退出程序,而不是最小化。
However, the problem I have is that if the window is visible, but they right click on the context menu of the tray icon and hit exit, I need to exit the program and not minimize.
我该如何解决这个问题。
How do I solve this problem?
推荐答案
试试这个?
bool _closingFromMenu;
void NOTIFYICON_EXIT_MENU_HANDLER(object sender, EventArgs e)
{
_closingFromMenu = true;
Close();
}
//form closing handler
FormClosing +=(a,b) =>{
if(_closingFromMenu){
Close();
}
else{
e.Cancel = true;
//do minimize stuff;
}
}
如果你只有一种形式,你可以调用 Application.Exit();
在上下文菜单项处理程序
or if you have only one form you can call Application.Exit();
in context menu item handler
这篇关于C#关闭到系统托盘(如MSN信使)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!