جلوگیری از بسته شدن فرم با دکمه های Alt+F4

برای جلوگیری از بسته شدن فرم با دکمه های Alt+F4 در رویداد KeyDown فرم کد زیر را بنویسید :

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
	if (e.Alt && e.KeyCode == Keys.F4) 
	    e.SuppressKeyPress = true;     
}

یا در solution Explorer بر روی program.cs دوبار کلیک کنید و کدهای زیر را در آن بنویسید:

using System;
using System.Windows.Forms;

namespace AltF4
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.SetCompatibleTextRenderingDefault(false);
            Application.AddMessageFilter(new AltF4Filter()); 
            Application.EnableVisualStyles();
            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;                
                }
                return false;
            }
        }
    }
}