简体   繁体   English

C# 控制台禁用调整大小

[英]C# Console Disable Resize

How to achieve programmatically that the console window is not resizable.如何以编程方式实现控制台窗口不可调整大小。

I don't want user to change the console window size with their mouse.我不希望用户用鼠标更改控制台窗口大小。

See the answer in this post on MSDN.请参阅 MSDN 上这篇文章中的答案。 It requires p-invoking:它需要 p 调用:

private const int MF_BYCOMMAND = 0x00000000;
public const int SC_CLOSE = 0xF060;
public const int SC_MINIMIZE = 0xF020;
public const int SC_MAXIMIZE = 0xF030;
public const int SC_SIZE = 0xF000;//resize

[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)
{
    IntPtr handle = GetConsoleWindow();
    IntPtr sysMenu = GetSystemMenu(handle, false);

    if (handle != IntPtr.Zero)
    {
        DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND);
        DeleteMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND);
        DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
        DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND);//resize
    }
    Console.Read();
}

Note that this does not prevent Windows window snapping (eg dragging window to edge of screen, Win + Left and Win + Right )请注意,这不会阻止 Windows 窗口捕捉(例如,将窗口拖动到屏幕边缘, Win + LeftWin + Right

Since you're trying to disable resizing, I'm guessing you won't want scrollbars either.由于您正在尝试禁用调整大小,我猜您也不会想要滚动条。 For that, see this answer to Remove space left after console scrollbars in C# (leftover after matching Console.SetWindowSize and SetBufferSize ; also requires p-invoking to "fix").为此,请参阅在 C# 中删除控制台滚动条后剩余的空间的答案(匹配Console.SetWindowSizeSetBufferSize后的剩余空间;还需要 p 调用以“修复”)。

I'm not sure you can avoid that kind of action. 我不确定您是否可以避免这种行为。 But you may try to use WindowHeight, WindowWidth, LargestWindowHeight and LargestWindowWidth. 但您可以尝试使用WindowHeight,WindowWidth,LargestWindowHeight和LargestWindowWidth。 See this: https://msdn.microsoft.com/pt-br/library/system.console(v=vs.110).aspx 看到这个: https : //msdn.microsoft.com/pt-br/library/system.console(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM