[英]C# application closing problem
I want my application such that, it will minimize to System Tray on clicking the close(X) button. 我希望我的应用程序能够在单击close(X)按钮时将其最小化到系统托盘。
And it will only be closed by clicking a different button/menu on the main application window or clicking a system tray context menuItem. 而且,只有在主应用程序窗口上单击其他按钮/菜单或单击系统托盘上下文菜单项才能关闭它。
I am able to make the window minimize to tray on close. 我能够使窗口最小化以关闭托盘。
But the problem I am facing is, I am now unable to close the application. 但是我面临的问题是,我现在无法关闭该应用程序。
This is my code (it is unable to close the application): 这是我的代码(无法关闭该应用程序):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void hideToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = false;
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
}
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.DoEvents();
Application.Exit();
}
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
}
In the button, set a field, for example: 在按钮中,设置一个字段,例如:
bool isClosing;
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
isClosing = true;
Close();
}
and check this in the "closing": 并在“结束”中进行检查:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(!isClosing) {
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}
That's because it always hits the form closing event handler where you cancel the event. 这是因为它总是会在您取消事件的地方触发表单关闭事件处理程序。
Place a condition there and skip canceling the event if the form is already minimized 在此放置条件,如果表单已最小化,则跳过取消事件
I might recommend a slightly different approach, the main issue that you have is that your application is running as the form, so you have a lot of workarounds to ensure that the form doesn't close. 我可能会建议一种略有不同的方法,您遇到的主要问题是您的应用程序正在作为表单运行,因此您有很多变通办法来确保表单不会关闭。
What I do, when working with an application that really lives in the system tray is to create a custom application context, which actually simplifies the process. 使用真正存在于系统托盘中的应用程序时,我要做的是创建自定义应用程序上下文,这实际上简化了过程。 Here is an article that I wrote that shows you how to do it.
这是我写的一篇文章 ,向您展示如何做。
Add a flag in places where you are overriding close behavior, and also cover other exit cases by checking the CloseReason enumeration value on the close event argument. 在您要覆盖关闭行为的地方添加一个标志,并通过检查close事件参数上的CloseReason枚举值来覆盖其他退出情况。
bool m_NeedClose = false;
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
m_NeedClose = true;
Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(m_NeedClose ||
(e.CloseReason != CloseReason.UserClosing))
{
return;
}
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
FormClosingEventArgs @ MSDN FormClosingEventArgs @ MSDN
CloseReason Enumeration @ MSDN 关闭原因枚举@ MSDN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.