C#控制台窗口禁用关闭按钮

 

 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 }

 

效果图:

 

 

参考:如何让C#控制台程序关闭按钮无效

 

posted @ 2020-04-28 17:05  橘子香气  阅读(1000)  评论(0编辑  收藏  举报