如何为应用程序禁用alt + F4?

如何在C#应用程序中禁用ALT + F4应用程序范围?

在我的应用程序中,我有很多WinForms,我想禁用使用ALT + F4关闭表单的function。 用户应该能够使用表单的“X”关闭表单。

同样,这不仅仅是一种forms。 我正在寻找一种方法,因此对于整个应用程序禁用ALT + F4 ,并且不适用于任何表单。 可能吗?

你可以在主要的启动方法中加入这样的东西:

 namespace WindowsFormsApplication1 { static class Program { ///  /// The main entry point for the application. ///  [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.AddMessageFilter(new AltF4Filter()); // Add a message filter Application.Run(new Form1()); } } public class AltF4Filter : IMessageFilter { public bool PreFilterMessage(ref Message m) { const int WM_SYSKEYDOWN = 0x0104; if (m.Msg == WM_SYSKEYDOWN) { bool alt = ((int)m.LParam & 0x20000000) != 0; if (alt && (m.WParam == new IntPtr((int)Keys.F4))) return true; // eat it! } return false; } } }