C# 更改控制台窗口的大小会引发 ArgumentOutOfRangeException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15099523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:03:03  来源:igfitidea点击:

Changing Console Window's size throws ArgumentOutOfRangeException

c#consolesizeconsole-application

提问by msbg

I am trying to set the size of the Console Window in a c# console application. I get an ArgumentOutOfRangeExceptionwith this message:

我正在尝试在 ac# 控制台应用程序中设置控制台窗口的大小。我收到一条ArgumentOutOfRangeException消息:

The value must be less than the console's current maximum window size of 41 in that dimension. Note that this value depends on screen resolution and the console font.

该值必须小于该维度中控制台当前的最大窗口大小 41。请注意,此值取决于屏幕分辨率和控制台字体。

I am using this to set it:

我正在使用它来设置它:

Console.WindowHeight = 480;

How do you set the Console window's size properly?

如何正确设置控制台窗口的大小?

采纳答案by Soner G?nül

From MSDNof Console.WindowHeightproperty:

MSDNConsole.WindowHeight属性:

The height of the console window measured in rows.

以行为单位测量的控制台窗口的高度。

As you can see, these are not pixels. Just remember, these values can change depending on your screen resolution and the console font. You can find maximum heightand widthvalues with Console.LargestWindowWidthand Console.LargestWindowHeightproperties.

如您所见,这些不是像素。请记住,这些值可能会根据您的屏幕分辨率和控制台字体而变化。您可以使用和属性找到最大高度宽度值。Console.LargestWindowWidthConsole.LargestWindowHeight

Console.WriteLine(Console.LargestWindowHeight);
Console.WriteLine(Console.LargestWindowWidth);

回答by DOT.NET

you can set a windowHeight less than 62, if u try exceed this value error throw the system.

你可以设置一个小于 62 的 windowHeight,如果你尝试超过这个值错误抛出系统。

class Pro
{
    public static void fun()
    {
        Console.WindowHeight = 61;
        Console.WriteLine("Welcome to asp .net ");
    }


    static void Main(string[] args)
    {
        Pro.fun();
    }

    // Summary:
    //     Gets the largest possible number of console window rows, based on the current
    //     font and screen resolution.
    //
    // Returns:
    //     The height of the largest possible console window measured in rows.
    public static int LargestWindowHeight { get; }

    // Summary:
    //     Gets the largest possible number of console window columns, based on the
    //     current font and screen resolution.
    //
    // Returns:
    //     The width of the largest possible console window measured in columns.
    public static int LargestWindowWidth { get; }

The above information catch Console[from metadata].

上述信息捕获控制台[来自元数据]。

回答by David Pfeffer

Console height is specified in rows (lines), not pixels.

控制台高度以行(行)为单位指定,而不是像素。

http://msdn.microsoft.com/en-us/library/system.console.windowheight.aspx

http://msdn.microsoft.com/en-us/library/system.console.windowheight.aspx

回答by John Zabroski

Microsoft recently published some information around this, see:

微软最近发布了一些关于此的信息,请参阅:

  1. Understanding Windows Console Host Settings
  1. 了解 Windows 控制台主机设置

Try this in powershell:

在powershell中试试这个:

$windowSize = $(get-item hkcu:\console).GetValue("WindowSize")
$windowHeight = $windowSize -shr 16
$windowWidth = ($windowSize -shl 16) -shr 16