c# - 如何使关闭按钮像最小化一样工作 : C# Winforms

标签 c# .net winforms

如何在我的应用程序中制作(默认)关闭 按钮(位于右上角),以将其用作最小化。 实际上我想在点击十字符号时最小化应用程序,但是当使用点击我的菜单选项退出时退出应用程序。

我写了这段代码来最小化点击关闭按钮时的表单:

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

并编写了这段代码,用于在单击菜单选项中的退出时退出应用程序。

private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

但是现在,当我点击退出菜单选项时,表单也会被最小化,而不是退出。

有人可以帮忙吗?

最佳答案

查看是否FormClosingEventArgs.CloseReason在决定最小化窗口之前等于 CloseReason.UserClosing。或者,比较 CloseReason.ApplicationExitCall

来自 CloseReason 的文档:

Members

...

UserClosing

The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.

...

ApplicationExitCall

The Exit method of the Application class was invoked.

关于c# - 如何使关闭按钮像最小化一样工作 : C# Winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15459105/

相关文章:

c# - 如何添加不同的列表框项目 WPF

c# - 覆盖 Json.Net 中的默认基元类型处理

c# - 无法加载文件或程序集“System.Net.Http

.net - 域驱动的设计:微服务(CQRS + ES),客户端可见性和元数据

c# - 在 C# 中的 ListView 中添加文本到列

c# - 绑定(bind)在 ListView 中不起作用

c# - 哪种语言习语/范式/特性使得添加对 "type providers"的支持变得困难?

c# - 如何根据引用的dll的文件版本执行不同的语句?

c# - 在 C# 中获得更快的 Ping 结果

c# - 从另一个进程更新 TextBox 文本