【发布时间】:2016-07-29 16:10:40
【问题描述】:
Windows 7
如何禁用控制台窗口上下文菜单的Close项?
更新
我使用 C# 中的 PInvoke:
const uint MF_BYCOMMAND = 0x00000000;
const uint MF_GRAYED = 0x00000001;
const uint SC_CLOSE = 0xF060;
const uint MF_DISABLED = 0x00000002;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("User32.dll", SetLastError = true)]
static extern uint EnableMenuItem(IntPtr hMenu, uint itemId, uint uEnable);
...
// Disable the close button and "Close" context menu item of the Console window
IntPtr hwnd = GetConsoleWindow();
IntPtr hmenu = GetSystemMenu(hwnd, false);
uint hWindow = EnableMenuItem(hmenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
我的代码禁用了“X”按钮,但“关闭”项仍然启用并且可以启动:
【问题讨论】:
-
GetSystemMenu 然后是带有 CLOSE 标识符的 DeleteMenu
-
@AlexK.:这完全删除了菜单条目。 EnableMenuItem 可用于禁用菜单项。
-
为什么要删除这个选项?好像有点可疑……
-
@DenisAnisimov,你的变种也很好,工作正常。谢谢。