C#控制台窗口禁用关闭按钮
public static void RemoveCloseMenu()
{
// 与控制台标题名一样的路径
// Process.GetCurrentProcess().MainModule.FileName;
string fullPath = Console.Title;
// 根据控制台标题找控制台
int WINDOW_HANDLER =Win32Helper.FindWindow(null, fullPath);
// 找关闭按钮
IntPtr CLOSE_MENU = Win32Helper.GetSystemMenu((IntPtr)WINDOW_HANDLER, IntPtr.Zero);
int SC_CLOSE = 0xF060;
// 关闭按钮禁用
Win32Helper.RemoveMenu(CLOSE_MENU, SC_CLOSE, 0x0);
}
public static class Win32Helper
{
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
[DllImport("user32.dll", EntryPoint = "RemoveMenu")]
public static extern int RemoveMenu(IntPtr hMenu, int nPos, int flags);
}
在main函数开始调用RemoveCloseMenu()即可。