Console.SetWindowSize(Int32, Int32) 方法

定义

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

public:
 static void SetWindowSize(int width, int height);
[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);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member SetWindowSize : int * int -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member SetWindowSize : int * int -> unit
static member SetWindowSize : int * int -> unit
Public Shared Sub SetWindowSize (width As Integer, height As Integer)

参数

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 namespace System;
int main()
{
   int origWidth;
   int width;
   int origHeight;
   int 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.

*/
// 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.

*/
// This example demonstrates the Console.SetWindowSize method,
//                           the Console.WindowWidth property,
//                       and the Console.WindowHeight property.
open System

//
// Step 1: Get the current window dimensions.
//
let origWidth  = Console.WindowWidth
let origHeight = Console.WindowHeight
printfn $"The current window width is {Console.WindowWidth}, and the current window height is {Console.WindowHeight}."
printfn "  (Press any key to continue...)"
Console.ReadKey true |> ignore

//
// Step 2: Cut the window to 1/4 its original size.
//
let width  = origWidth / 2
let height = origHeight / 2
Console.SetWindowSize(width, height)
printfn $"The new window width is {Console.WindowWidth}, and the new window height is {Console.WindowHeight}."
                        
printfn "  (Press any key to continue...)"
Console.ReadKey true |> ignore

//
// Step 3: Restore the window to its original size.
//
Console.SetWindowSize(origWidth, origHeight)
printfn $"The current window width is {Console.WindowWidth}, and the current window height is {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.
' This example demonstrates the Console.SetWindowSize method,
'                           the Console.WindowWidth property, 
'                       and the Console.WindowHeight property.
Class Sample
   Public Shared Sub Main()
      Dim origWidth, width As Integer
      Dim origHeight, height As Integer
      Dim m1 As String = "The current window width is {0}, and the " & _
                         "current window height is {1}."
      Dim m2 As String = "The new window width is {0}, and the new " & _
                         "window height is {1}."
      Dim m4 As String = "  (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)
   End Sub
End Class
'
'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.
'
'

适用于