Console.WindowWidth 属性

定义

获取或设置控制台窗口的宽度。Gets or sets the width of the console window.

public:
 static property int WindowWidth { int get(); void set(int value); };
public static int WindowWidth { get; set; }
member this.WindowWidth : int with get, set
Public Shared Property WindowWidth As Integer

属性值

控制台窗口的宽度,以列为单位。The width of the console window measured in columns.

异常

属性 WindowWidth 的值或属性 WindowHeight 的值小于或等于零。The value of the WindowWidth property or the value of the WindowHeight property is less than or equal to 0.

-或--or- 属性 WindowHeight 的值加上属性 WindowTop 的值大于或等于 MaxValueThe value of the WindowHeight property plus the value of the WindowTop property is greater than or equal to MaxValue.

-或--or- WindowWidth 属性的值或 WindowHeight 属性的值大于当前屏幕分辨率和控制台字体的最大可能的窗口宽度或高度。The value of the WindowWidth property or the value of the WindowHeight property is greater than the largest possible window width or height for the current screen resolution and console font.

读取或写入信息时发生错误。Error reading or writing information.

设置操作在 Windows 之外的操作系统上调用。The set operation is invoked on an operating system other than Windows.

示例

此示例演示 SetWindowSize 方法以及 WindowWidthWindowHeight 属性。This example demonstrates the SetWindowSize method, and the WindowWidth and WindowHeight properties. 若要查看更改控制台窗口大小的完整效果,必须运行该示例。You must run the example to see the full effect of changing the console window size.

该示例报告将控制台窗口的维度设置为85列和43行,然后等待按键。The example reports the dimensions of a console window set to 85 columns and 43 rows, then waits for a key press. 按任意键时,控制台窗口的维度会减半,并报告新维度,然后该示例将等待另一个按键。When any key is pressed, the dimensions of the console window are halved, the new dimensions are reported, and the example waits for another key press. 最后,当按任意键时,控制台窗口将还原为其原始维度,该示例将终止。Finally, when any key is pressed the console window is restored to its original dimensions and the example terminates.

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

注解

尝试在输出重定向时设置 WindowWidth 属性的值将引发 ArgumentOutOfRangeExceptionIOException 异常。Attempting to set the value of the WindowWidth property when output is redirected throws either an ArgumentOutOfRangeException or an IOException exception. 若要防止出现异常,只能在 IsOutputRedirected 属性返回 false时设置此属性的值。To prevent an exception, you can set the value of this property only if the IsOutputRedirected property returns false.

适用于