使用英语阅读

通过


Console.SetWindowSize(Int32, Int32) 方法

定义

将控制台窗口的高度和宽度设置为指定值。

[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void SetWindowSize (int width, int height);
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static void SetWindowSize (int width, int height);
public static void SetWindowSize (int width, int height);

参数

width
Int32

控制台窗口的宽度,以列为单位。

height
Int32

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

属性

例外

widthheight 小于或等于零。

width plus WindowLeftheight plus WindowTop 大于或等于 Int16.MaxValue

widthheight 的值大于当前屏幕分辨率和控制台字体的最大可能的窗口宽度或高度。

用户没有执行此操作的权限。

出现 I/O 错误。

当前操作系统不是 Windows。

示例

此示例演示 SetWindowSize 了 方法以及 WindowWidthWindowHeight 属性。 必须运行此示例才能看到更改控制台窗口大小的完整效果。

该示例报告设置为 85 列和 43 行的控制台窗口的尺寸,然后等待按下某个键。 按下任意键时,控制台窗口的尺寸减半,报告新维度,示例将等待另一次按键。 最后,当按下任何键时,控制台窗口将还原到其原始尺寸,并且示例终止。

// This example demonstrates the Console.SetWindowSize method,
//                           the Console.WindowWidth property,
//                       and the Console.WindowHeight property.
using System;

class Sample
{
    public static void Main()
    {
    int origWidth, width;
    int origHeight, height;
    string m1 = "The current window width is {0}, and the " +
                "current window height is {1}.";
    string m2 = "The new window width is {0}, and the new " +
                "window height is {1}.";
    string m4 = "  (Press any key to continue...)";
//
// Step 1: Get the current window dimensions.
//
    origWidth  = Console.WindowWidth;
    origHeight = Console.WindowHeight;
    Console.WriteLine(m1, Console.WindowWidth,
                          Console.WindowHeight);
    Console.WriteLine(m4);
    Console.ReadKey(true);
//
// Step 2: Cut the window to 1/4 its original size.
//
    width  = origWidth/2;
    height = origHeight/2;
    Console.SetWindowSize(width, height);
    Console.WriteLine(m2, Console.WindowWidth,
                          Console.WindowHeight);
    Console.WriteLine(m4);
    Console.ReadKey(true);
//
// Step 3: Restore the window to its original size.
//
    Console.SetWindowSize(origWidth, origHeight);
    Console.WriteLine(m1, Console.WindowWidth,
                          Console.WindowHeight);
    }
}
/*
This example produces the following results:

The current window width is 85, and the current window height is 43.
  (Press any key to continue...)
The new window width is 42, and the new window height is 21.
  (Press any key to continue...)
The current window width is 85, and the current window height is 43.

*/

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1