点击窗体上的“X”关闭使之最小化到托盘,然后右击弹出菜单,选择“关闭”如何完全关闭?private void  Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {                      this.WindowState = FormWindowState.Minimized;
            e.Cancel = true;           
            
        } private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState ==FormWindowState.Minimized )
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }
========
这是我单击"X"按钮时,代码
请教高手,如何退出程序

解决方案 »

  1.   

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (result == DialogResult.Yes)
        {
            e.Cancel = false;
            Application.Exit();
        }
        else
        {
            e.Cancel = true;
            this.Hide();
            this.Visible = false;
        }
    }private void ConExit_Click(object sender, EventArgs e)
    {
        result=MessageBox.Show("确认退出系统?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        Application.Exit();
    }
      

  2.   

    你一定要在如下的方法里添加参数来标识是不是应该真的退出程序:
    private void  Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {   
        if (!this.m_allowExit)
        {
            this.WindowState = FormWindowState.Minimized;
            e.Cancel = true;           
        }
    }然后在弹出的菜单的Click事件里设置这个this.m_allowExit=true;
    这样窗口就会顺利的关闭了.
      

  3.   

    Application.Exit(true);行不?
    如果不行的话设置一个变量,在formClosing中根据变量的值来设置e。Cancel的值
      

  4.   

    在程序开始的时候设置this.m_allowExit=false,只有在菜单的退出事件代码里设置这个this.m_allowExit=true;就可以了.具体看你的思路了.
      

  5.   

    可以加个全局变量判断一下是由哪里触发Closing事件,如果是关闭按钮就最小化,是右键菜单就关闭
      

  6.   

    /// <summary>
    /// 重写窗体关闭事件
    /// </summary>
    /// <param name="m"></param>
    protected override void WndProc(ref Message m) 

    const int WM_SYSCOMMAND = 0x0112; 
    const int SC_CLOSE = 0xF060; 
    if (m.Msg == WM_SYSCOMMAND && (int) m.WParam == SC_CLOSE) 

    this.Hide(); 
    return; 

    base.WndProc (ref m); 
    }
      

  7.   

    点x和使用菜单关闭程序执行不同程序你只要在菜单那多设一个变量加以区分就OK了