mainform:
private flag=0;MainForm_Closing: 置flag =1;ChildForm_Closing: 置flag =2;这样就可以判断了

解决方案 »

  1.   

    首先我觉得你在Closing 事件里这样写不好
    this.Visible=false;
    e.Cancel=true;如何判断是用户关闭还是应用程序在关闭,我想可以通过发消息传入不同参数来解决吧
      

  2.   

    置flag不能判断是用户关闭了子窗体还是应用程序在关闭子窗体啊。 Closing事件都会执行。发消息? 怎么发?应用程序关闭时还由的自己发消息吗?
      

  3.   

    private void ChildForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
             //为真时是用户操作关闭
    if(this.Focus())
    {
    this.Visible=false;
    e.Cancel=true; }
    }
      

  4.   

    this.Focus() 还是this.Focused啊? 不管是哪种根本不起作用!拜托给别人回答问题的时候,自己先测试一下是否真的可行! 
    没测试就写上去是没有一点责任心的!
      

  5.   

    再次说明问题:有一个多文档的MDI程序,每个mdi子窗体显示不同的数据。
    我想在子窗体被用户点击关闭按钮时不销毁, 而是隐藏起来,省去再次打开时重新加载数据并显示。
    因此有:
    private void ChildForm_Closing(...)
    {
        this.Visible=false;
        e.Cancel=true;
    }在主窗体的Closing函数里显示一个确认对话框,如下:
    private void MainForm_Closing(.....)
    {
      DialogResult r=MessageBox.Show( this, "文档已修改,是否保存?");
      if( r == DialogResult.Cancel )
         e.Cancel=true;
      else
         e.Cancel=false;}而主窗体在调用Closing事件处理函数之前,是会调用子窗体的Closing事件函数的,
    因此显示确认对话框时,当前的子窗体已经被调用Closing()函数了,就是已经被隐藏了。 
    但是此时程序还未关闭, 这不是希望看到的。 不知道有什么方法可以解决此问题? 或者实现这样的功能?
      

  6.   

    http://community.csdn.net/Expert/topic/3648/3648493.xml?temp=.1477777
      

  7.   

    this.Visible=false;
    e.Cancel=true;
      

  8.   

    提供另外一种思路:假如你关闭的时候不需要做什么事情的话,直接调用Application.Exit()该方法不会触发Form.Closed和Form.Closing事件
      

  9.   

    可惜关闭的时候,如果文档修改了未保存,还是要提示用户的哦。
    不能直接调用Application.Exit()。 http://community.csdn.net/Expert/topic/3648/3648493.xml?temp=.1477777文章没有什么用啊。
      

  10.   

    上面的方法不行当用户关闭主窗体时,可以捕获关闭消息,然后做个标志,子窗体检查这个标志就可以了主窗体中加一个标志
    static public bool IsMdiClosed = false;
    protected override void WndProc(ref Message m)
    {
    if (m.Msg == WM_CLOSE)
    主窗体类名.IsMdiClosed = true;
    base.WndProc (ref m);
    }在子窗体就可以利用这个标志做你要做的事情了
      

  11.   

    protected override void WndProc(ref Message m)
    {
    if (m.Msg == WM_CLOSE)
    主窗体类名.IsMdiClosed = true;
    base.WndProc (ref m);
             //最好把这句加上,免得出错
    if (m.Msg == WM_CLOSE)
    主窗体类名.IsMdiClosed = false;
    }
      

  12.   

    for2中:
    bool flag = false;
    private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(this.flag)
    {
    this.Visible=false;
    e.Cancel = true;
    Debug.WriteLine("用户操作"); }
    else
    {
    e.Cancel = true;
    Debug.WriteLine("不是用户操作");
    }
    }
    protected override void WndProc(ref Message m)
    {
    if(m.Msg == 16)
    {
    this.flag = true;
    }
    else
    {
    this.flag = false;
    }
    base.WndProc (ref m);
    }
    form1中:
    private void Form1_Load(object sender, System.EventArgs e)
    {
    this.IsMdiContainer = true;
    frm = new Form2();
    frm.MdiParent = this;
    frm.Show();
    }
    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(DialogResult.No == MessageBox.Show("是否关闭","系统提示",MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
    {
    e.Cancel = true;
    }
    else
    {
       Application.Exit();
    }
    }
    楼主看看是不是你想要的效果