禁用c#控制台的最大化和最小化按钮

时间:2015-12-10 10:35:51

标签: c# .net console

我一直在寻找禁用c#应用程序的“控制”栏。它非常相似 how can i disable close button of console window in a visual studio console application?

但是我也希望将它应用于最小化和最大化按钮。我不确定以前的解释中的代码是什么,所以这可能是我将它应用于所有按钮的问题。

2 个答案:

答案 0 :(得分:6)

这是一个解决方案:

 class Program
    {
        private const int MF_BYCOMMAND = 0x00000000;
        public const int SC_CLOSE = 0xF060;
        public const int SC_MINIMIZE = 0xF020;
        public const int SC_MAXIMIZE = 0xF030;

        [DllImport("user32.dll")]
        public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("kernel32.dll", ExactSpelling = true)]
        private static extern IntPtr GetConsoleWindow();

        static void Main(string[] args)
        {
            DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND);
            DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MINIMIZE, MF_BYCOMMAND);
            DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MAXIMIZE, MF_BYCOMMAND);
            Console.Read();
        }

    }

答案 1 :(得分:-2)

您应该尝试使用此代码,我希望它能够通过我给定的链接 以下

internal static class WindowExtensions
{
    [DllImport("user32.dll")]
    internal extern static int SetWindowLong(IntPtr hwnd, int index, int value);

    [DllImport("user32.dll")]
    internal extern static int GetWindowLong(IntPtr hwnd, int index);

    internal static void HideMinimizeAndMaximizeButtons(this Window window)
    {
        const int GWL_STYLE = -16;

        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
        long value = GetWindowLong(hwnd, GWL_STYLE);

        SetWindowLong(hwnd, GWL_STYLE, (int)(value & -131073 & -65537));

    }
}

this.SourceInitialized += (x, y) =>
{
    this.HideMinimizeAndMaximizeButtons();
};

其他链接: Link Here