[CSharpTips]C# 控制台程序屏蔽关闭按钮,关闭快速编辑模式,注册关闭事件
C# 控制台程序屏蔽关闭按钮,关闭快速编辑模式,注册关闭事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleCloseTest { internal class Program { //激活关闭窗口事件 public delegate bool ControlCtrlDelegate( int CtrlType); [DllImport( "kernel32.dll" )] private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerAppClose, bool Add); private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerAppClose); //取消控制台关闭按钮 private const int MF_BYCOMMAND = 0x00000000; public const int SC_CLOSE = 0xF060; [DllImport( "user32.dll" )] public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags); [DllImport( "user32.dll" )] public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport( "kernel32.dll" , ExactSpelling = true )] public static extern IntPtr GetConsoleWindow(); //关闭控制台快速编辑模式 const int STD_INPUT_HANDLE = -10; const uint ENABLE_QUICK_EDIT_MODE = 0x0040; [DllImport( "kernel32.dll" , SetLastError = true )] internal static extern IntPtr GetStdHandle( int hConsoleHandle); [DllImport( "kernel32.dll" , SetLastError = true )] internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint mode); [DllImport( "kernel32.dll" , SetLastError = true )] internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint mode); static void Main( string [] args) { //注册窗口关闭事件 bool bRet = SetConsoleCtrlHandler(cancelHandler, true ); //禁用关闭按钮 IntPtr consoleWindow = GetConsoleWindow(); IntPtr sysMenu = GetSystemMenu(consoleWindow, false ); if (consoleWindow != IntPtr.Zero) DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND); //关闭控制台快速编辑模式 DisableQuickEditMode(); Console.WriteLine( "Func Main Called" ); Console.WriteLine( "Enter 'exit' to exit the program ..." ); while ( true ) { Thread.Sleep(1); string s = Console.ReadLine(); //一次输入一行 if (s.ToUpper() == "EXIT" ) break ; } } /// <summary> /// 打开快速编辑模式 /// </summary> public static void EnableQuickEditMode() { IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE); uint mode; GetConsoleMode(hStdin, out mode); mode |= ENABLE_QUICK_EDIT_MODE; SetConsoleMode(hStdin, mode); } /// <summary> /// 关闭快速编辑模式 /// </summary> public static void DisableQuickEditMode() { IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE); uint mode; GetConsoleMode(hStdin, out mode); mode &= ~ENABLE_QUICK_EDIT_MODE; SetConsoleMode(hStdin, mode); } /// <summary> /// 关闭窗口时的事件 /// </summary> /// <param name="CtrlType"></param> /// <returns></returns> static bool HandlerAppClose( int CtrlType) { Console.WriteLine( "关闭窗口事件被激活" ); Console.WriteLine( "do something..." ); Thread.Sleep(5000); Environment.Exit(0); return false ; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义