private void dataGrid_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
  if( e.KeyCode == Keys.Escape )
   { buttonExit_Click(sender, e);
   }
  if (e.Control && (e.KeyCode == Keys.F4))
   {
         buttonExit_Click( sender, e);
   }
  if (e.Alt && (e.KeyCode == Keys.F4))
   {
Application.Exit()
   }
}

解决方案 »

  1.   

    altF4,this.Close() 的sender都是MainForm.
    Application.Exit()不会引发Form_Closing事件。C#里没办法判断是自己还是因为系统关闭而发送的关闭消息。我做过一个小程序,重载WndProc,在0x0010消息时作相关动作。
    protected override void WndProc(ref Message m)
    {
    switch(m.Msg)
    {
    case 0x0010: //WM_CLOSE
    if(!_quitable)
    {
    HideMe();
    return;
    }
    break;
    default:
    break;
    }
    base.WndProc(ref m);
    }
      

  2.   

    而不是在Form_Closing里判断处理。
      

  3.   

    >>如何判断关闭窗口消息的sender是谁?是谁?那肯定是窗口自己.呵呵...通过form的DialogResult属性.来判断消息关闭按钮、AltF4、this.Close()这三种情况是指窗体本身.Applicat.Exit()、这指的是应用程序.
      

  4.   

    举个例子来说明一下,比如我们用flashget下载东西时,点了关闭按钮后程序并不退出,只是缩小到托盘中,只有从菜单"文件"→退出中才能真正结束程序,因为用户在不用一个窗口时往往喜欢点关闭按钮,但许多软件(如局域网中的通讯软件)不允许随便关闭,这就要屏蔽关闭按钮了,该如何处理?
      

  5.   

    private void Form1_OnCloseing(object sender, System.ComponentModel.CancelEventArgs e)
    {
      if(((Form)sender).DialogResult==DialogResult.None)

    e.Cancel=true;//重点是这e
    this.Hide();
    this.notifyIcon1.Visible=true;
    }
    }
      

  6.   

    private void Form1_OnCloseing(object sender, System.ComponentModel.CancelEventArgs e)
    {
      if(((Form)sender).DialogResult==DialogResult.None)

    e.Cancel=true;//重点是这e
    this.Hide();
    this.notifyIcon1.Visible=true;
    }
    }
      

  7.   

    部分同意nerk(尘世中一个迷途小书僮) 的观点,从OnCloseing更本就判断不出来.重载Form的WndProc方法,凡是有代码,处理SC_CLOSE消息,注意这个值是0xF060,不是nerk(尘世中一个迷途小书僮)说的那个值. 有代码产生的关闭,不会触发这个消息(比如this.close,application.exit),有动作产生的关闭(Alt+F4,点击X关闭)才会有这个消息.参考这个代码,这是关闭的时候,让它最小化.
    protected override void WndProc(ref Message m )
    {
    if (m.WParam.ToInt32()== 0xF060) // 关闭消息
    {
    m.Result = new IntPtr(0); // Take care of message
    this.WindowState = FormWindowState.Minimized;
    }
    else
    base.WndProc(ref m); // ...let Form take care of this message (normally)
    }
      

  8.   

    to: TheAres(班门斧) 其實我們的目的不一樣,我用WM_CLOSE是希望所有正常退出都要經過我的過濾,
    而系統關機或者強制終止這些動作都不會發送WM_CLOSE消息我一直都這麼用沒什麼問題.你用的0xF060(SC_CLOSE)