0

Form Closing当我单击按钮(经典红色 X 按钮)时,我想将我的应用程序隐藏在系统托盘中。我提供了此代码;

    private void Ana_FormClosing(object sender, FormClosingEventArgs e) 
    {            
        e.Cancel = true; 
        this.Hide();          
    }

因此,该应用程序仍在system tray. 我添加了一个ContextMenuStrip,当我右键单击鼠标时会出现ContextMenuStrip一个Close按钮,但是当我单击该Close按钮时,应用程序仍在运行。Close单击该按钮时,我想终止应用程序。这是我的代码:

   private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
   {
        DialogResult ext;
        ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

        if (ext == DialogResult.Yes)
        {
            Application.Exit();                
        }       
    }

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

2 回答 2

0

调用Application.Exit()实际上会尝试关闭您的所有表单,并且由于您FormClosing的硬编码被取消,因此它无法完成任务。

您当前结构的一个选项是创建一个AllowClose标志。这可以是表单的属性,也可以是全局静态值。

例如:

//in your form
public bool AllowClose {get;set;}

private void Ana_FormClosing(object sender, FormClosingEventArgs e) 
{
    if(!AllowClose)
    {            
        e.Cancel = true; 
        this.Hide();  
    }        
}

//in your context menu event
private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
{

    DialogResult ext;
    ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (ext == DialogResult.Yes)
    {
        this.AllowClose = true;
        Application.Exit();
    }       
}
于 2013-08-01T09:41:57.743 回答
0

尝试这个,

bool isClosing = false;


 private void Ana_FormClosing(object sender, FormClosingEventArgs e) 
    {            
        if(!isClosing)
        {
           e.Cancel = true; 
           this.Hide();          
        }
    }




private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
    {
        DialogResult ext;
        isClosing = true;

        ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (ext == DialogResult.Yes)
        {
            Application.Exit();

        }       
    }
于 2013-08-01T09:45:15.363 回答