Прочитать на английском

Поделиться через


Console.SetWindowSize(Int32, Int32) Метод

Определение

Устанавливает заданные значения высоты и ширины окна консоли.

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void SetWindowSize (int width, int height);
C#
[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);
C#
public static void SetWindowSize (int width, int height);

Параметры

width
Int32

Ширина окна консоли измеряется столбцами.

height
Int32

Высота окна консоли измеряется строками.

Атрибуты

Исключения

width или height меньше или равно нулю.

-или-

width значение plus WindowLeft или height plus WindowTop больше или равно Int16.MaxValue.

-или-

Значение параметра width или height больше ширины или высоты окна максимально возможной при текущем разрешении и шрифте консоли.

Пользователь не имеет разрешений на выполнение этого действия.

Ошибка ввода/вывода.

Текущая операционная система не является системой Windows.

Примеры

В этом примере демонстрируется SetWindowSize метод и WindowWidth свойства и WindowHeight . Чтобы увидеть полный эффект изменения размера окна консоли, необходимо выполнить пример.

В примере отображаются размеры окна консоли, равные 85 столбцам и 43 строкам, а затем ожидает нажатия клавиши. При нажатии любой клавиши размеры окна консоли уменьшается вдвое, отображаются новые измерения, а в примере ожидается другое нажатие клавиши. Наконец, при нажатии любой клавиши окно консоли восстанавливается до исходных размеров, и пример завершается.

C#
// 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