Console.WindowHeight 属性

定义

获取或设置控制台窗口区域的高度。

public:
 static property int WindowHeight { int get(); void set(int value); };
public static int WindowHeight { [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] get; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
public static int WindowHeight { [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] [System.Runtime.Versioning.UnsupportedOSPlatform("android")] [System.Runtime.Versioning.UnsupportedOSPlatform("ios")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] get; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static int WindowHeight { get; set; }
public static int WindowHeight { get; set; }
[<get: System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<set: System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member WindowHeight : int with get, set
[<get: System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<set: System.Runtime.Versioning.SupportedOSPlatform("windows")>]
[<get: System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<get: System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<get: System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member WindowHeight : int with get, set
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member WindowHeight : int with get, set
static member WindowHeight : int with get, set
Public Shared Property WindowHeight As Integer

属性值

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

属性

例外

属性 WindowWidth 的值或属性 WindowHeight 的值小于或等于零。

属性的值 WindowHeight 加上 属性的值 WindowTop 大于或等于 Int16.MaxValue

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

读取或写入信息时发生错误。

设置操作在 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.
'
'

注解

尝试在重定向输出时设置 属性的值 WindowHeight 会引发 ArgumentOutOfRangeExceptionIOException 异常。 为防止异常,仅当属性返回 falseIsOutputRedirected,才能设置此属性的值。

适用于