Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击x并通过系统托盘ToolStripMenuItem关闭时,如何隐藏窗口窗体_C#_.net - Fatal编程技术网

C# 单击x并通过系统托盘ToolStripMenuItem关闭时,如何隐藏窗口窗体

C# 单击x并通过系统托盘ToolStripMenuItem关闭时,如何隐藏窗口窗体,c#,.net,C#,.net,我是C.Net新手,我正在寻找一种方法,当右上角的窗口关闭按钮上的“X”被单击时隐藏我的窗体窗口,当我从系统托盘上下文菜单中单击退出时关闭应用程序 为了简化我想做的是,我想要类似Skype的东西,它可以通过系统托盘选项退出,并在单击窗口十字时隐藏到系统尝试中 下面是我的代码,我尝试在表单关闭事件中将cancel属性重写为true,但它也会停止系统尝试选项的关闭过程,我如何区分它们 `using System; using System.Collections.Generic; using Sys

我是C.Net新手,我正在寻找一种方法,当右上角的窗口关闭按钮上的“X”被单击时隐藏我的窗体窗口,当我从系统托盘上下文菜单中单击退出时关闭应用程序

为了简化我想做的是,我想要类似Skype的东西,它可以通过系统托盘选项退出,并在单击窗口十字时隐藏到系统尝试中 下面是我的代码,我尝试在表单关闭事件中将cancel属性重写为true,但它也会停止系统尝试选项的关闭过程,我如何区分它们

`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Icon ico;




        private void Form1_Load(object sender, EventArgs e)
        {
              ico = notifyIcon1.Icon;
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void quitFormToolStripMenuItem_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
                System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
                messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason);
                messageBoxCS.AppendLine();
                messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel);
                messageBoxCS.AppendLine();
                MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event");

        }
    }
}
` 

通常,当我不得不这样做时,我会保留一个状态标志,以记住从其他地方打来的关闭我的电话。因此,在我的关闭处理程序中,我可以检查是否需要关闭或隐藏

bool bFormCloseRequested = false: //member of my Form

void MyCloseClick_Handler{object sender, eventArgs e)
{
    if(!bFormCloseRequested)
    {
        e.Cancel = true;
        this.hide();
    }
}

在你的课堂表格中使用bool,当从托盘关闭时将其设置为true,检查bool是否为true,然后不执行e。取消?