C#应用程序关闭问题

发布于 2024-08-12 21:49:16 字数 1551 浏览 3 评论 0原文

我希望我的应用程序在单击关闭(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然#的苍凉 2024-08-19 21:49:16

在按钮中,设置一个字段,例如:

bool isClosing;
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
    isClosing = true;
    Close();
}

并在“结束”中选中此字段:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if(!isClosing) {
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;
    }
}

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;
    }
}
第七度阳光i 2024-08-19 21:49:16

这是因为它总是会命中表单关闭事件处理程序,您可以在其中取消事件。

在那里放置一个条件,如果表单已经最小化,则跳过取消事件

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

终陌 2024-08-19 21:49:16

我可能会推荐一种稍微不同的方法,您遇到的主要问题是您的应用程序作为表单运行,因此您有很多解决方法来确保表单不会关闭。

当使用真正存在于系统托盘中的应用程序时,我所做的是创建一个自定义应用程序上下文,这实际上简化了该过程。 这是我写的一篇文章向您展示如何操作。

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.

蓝海似她心 2024-08-19 21:49:16

在要覆盖关闭行为的位置添加一个标志,并通过检查关闭事件参数上的 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
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.

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
CloseReason Enumeration @ MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文