-2

我需要知道控制台应用程序中是否有取消退出键的选项?

我添加了一个图片 url,描述了我想取消的按钮,并用红色包围。

http://i44.tinypic.com/2dj18d0.jpg

谢谢!

4

1 回答 1

0

您可以使用 Windows API 做到这一点。

这段代码可以解决问题:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace BlogConsole
{
    class API
    {
        [DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        internal const UInt32 SC_CLOSE = 0xF060;
        internal const UInt32 MF_ENABLED = 0x00000000;
        internal const UInt32 MF_GRAYED = 0x00000001;
        internal const UInt32 MF_DISABLED = 0x00000002;
        internal const uint MF_BYCOMMAND = 0x00000000;

        public static void EnableCloseButton(IntPtr handle, bool bEnabled)
        {
            IntPtr hSystemMenu = GetSystemMenu(handle, false);
            EnableMenuItem(hSystemMenu, SC_CLOSE, (uint)(MF_ENABLED | (bEnabled ? MF_ENABLED : MF_GRAYED)));
        }
    }
}

这就是你所说的方式:

static void Main(string[] args)
{
    IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
    API.EnableCloseButton(handle, false);
}
于 2013-08-22T11:25:54.433 回答