14

ALT+F4アプリケーション全体でC#アプリケーション の使用を無効にするにはどうすればよいですか?

ALT私のアプリケーションには多くのWinFormがあり、 +を使用してフォームを閉じる機能を無効にしたいですF4。ただし、ユーザーはフォームの「X」を使用してフォームを閉じることができるはずです。

繰り返しますが、これは1つの形式だけのものではありません。ALT+F4がアプリケーション全体で無効になり、どのフォームでも機能しないようにする方法を探しています。出来ますか?

4

1 に答える 1

5

次のようなものをメインの起動メソッドに入れることができます。

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [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;
        }
    }
}
于 2012-10-15T16:28:25.413 に答える