C# 命令行程序防止鼠标点击后卡死进程

    public class ConsoleHelper
    {
        const int STD_INPUT_HANDLE = -10;
        const uint ENABLE_QUICK_EDIT_MODE = 0x0040;
        const uint ENABLE_INSERT_MODE = 0x0020;

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GetStdHandle(int hConsoleHandle);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint mode);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint mode);

        public static void SetDefault()
        {
            IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE);
            uint mode;
            GetConsoleMode(hStdin, out mode);
            mode &= ~ENABLE_QUICK_EDIT_MODE; // 移除快速编辑模式
            mode &= ~ENABLE_INSERT_MODE; // 移除插入模式
            SetConsoleMode(hStdin, mode);
        }
    }

  

然后在Main函数中,调用该静态方法,就可以防止卡死了。

posted @ 2026-01-09 17:24  chyshx  阅读(1)  评论(0)    收藏  举报