C#应用程序关闭问题
我希望我的应用程序在单击关闭(X)按钮时最小化到系统托盘。
只有通过单击主应用程序窗口上的不同按钮/菜单或单击系统托盘上下文菜单项才能关闭它。
我能够使窗口最小化到关闭时的托盘。
但我面临的问题是,我现在无法关闭该应用程序。
这是我的代码(它无法关闭应用程序):
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;
}
}
I want my application such that, it will minimize to System Tray on clicking the close(X) button.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在按钮中,设置一个字段,例如:
并在“结束”中选中此字段:
In the button, set a field, for example:
and check this in the "closing":
这是因为它总是会命中表单关闭事件处理程序,您可以在其中取消事件。
在那里放置一个条件,如果表单已经最小化,则跳过取消事件
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.
在要覆盖关闭行为的位置添加一个标志,并通过检查关闭事件参数上的 CloseReason 枚举值来覆盖其他退出情况。
FormClosingEventArgs @ MSDN
CloseReason 枚举@ MSDN
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.
FormClosingEventArgs @ MSDN
CloseReason Enumeration @ MSDN