1 public class Program 2 { 3 #region 禁用控制台黑窗口的关闭按钮 part1 4 5 private const int MF_BYCOMMAND = 0x00000000; 6 public const int SC_CLOSE = 0xF060; 7 8 [DllImport("user32.dll")] 9 public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);// 删除菜单 10 11 [DllImport("user32.dll")] 12 private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);// 获取系统菜单句柄 13 14 [DllImport("kernel32.dll", ExactSpelling = true)] 15 private static extern IntPtr GetConsoleWindow();// 获取控制台窗口句柄 16 17 #endregion 18 19 20 static void Main(string[] args) 21 { 22 23 #region 禁用控制台黑窗口的关闭按钮 part2 24 25 Console.WriteLine(""); 26 DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND); 27 Console.Read(); 28 29 #endregion 30 } 31 }
效果图: