Console.WindowLeft 属性

定义

获取或设置控制台窗口区域的最左边相对于屏幕缓冲区的位置。Gets or sets the leftmost position of the console window area relative to the screen buffer.

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

属性值

控制台窗口的最左边的位置,以列为单位。The leftmost console window position measured in columns.

异常

在集运算中,要赋予的值小于零。In a set operation, the value to be assigned is less than zero.

- 或 --or- 赋值之后,WindowLeftWindowWidth 将超过 BufferWidthAs a result of the assignment, WindowLeft plus WindowWidth would exceed BufferWidth.

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

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

示例

下面的示例打开一个80列控制台窗口,并定义一个宽度为120列的缓冲区。The following example opens an 80-column console window and defines a buffer area that is 120 columns wide. 它显示有关窗口和缓冲区大小的信息,然后等待用户按向左键或向右键。It displays information on window and buffer size, and then waits for the user to press either the LEFT ARROW key or the RIGHT ARROW key. 在前一种情况下,如果结果是合法值,则它会将 WindowLeft 属性的值减一。In the former case, it decrements the value of the WindowLeft property by one if the result is a legal value. 在后一种情况下,如果结果是合法的,则会将 WindowLeft 属性的值增加1。In the latter case, it increases the value of the WindowLeft property by one if the result would be legal. 请注意,该示例不必处理 ArgumentOutOfRangeException,因为它会检查要分配给 WindowLeft 属性的值是否不是负数,并且不会导致 WindowLeftWindowWidth 属性的总和超过 BufferWidth 属性值。Note that the example does not have to handle an ArgumentOutOfRangeException, because it checks that the value to be assigned to the WindowLeft property is not negative and does not cause the sum of the WindowLeft and WindowWidth properties to exceed the BufferWidth property value.

using namespace System;

void ShowConsoleStatistics()
{
   Console::WriteLine("Console statistics:");
   Console::WriteLine("   Buffer: {0} x {1}", Console::BufferHeight, Console::BufferWidth);
   Console::WriteLine("   Window: {0} x {1}", Console::WindowHeight, Console::WindowWidth);
   Console::WriteLine("   Window starts at {0}.", Console::WindowLeft);
   Console::WriteLine("Press <- or -> to move window, Ctrl+C to exit.");
}

int main()
{
   ConsoleKeyInfo key;
   bool moved = false;

   Console::BufferWidth = 120;
   Console::Clear();

   ShowConsoleStatistics();
   
   do {
      key = Console::ReadKey(true);
      if (key.Key == ConsoleKey::LeftArrow)
      {
         int pos = Console::WindowLeft - 1;
         if (pos >= 0 && pos + Console::WindowWidth <= Console::BufferWidth)
         { 
            Console::WindowLeft = pos;
            moved = true;
         }       
      } 
      else if (key.Key == ConsoleKey::RightArrow)
      {
            int pos = Console::WindowLeft + 1;
            if (pos + Console::WindowWidth <= Console::BufferWidth)
            { 
               Console::WindowLeft = pos;
               moved = true;
            }
      }
      if (moved)
      { 
         ShowConsoleStatistics(); 
         moved = false;
      }   
      Console::WriteLine();
   } while (true);
}
using System;

public class Example
{
   public static void Main()
   {
      ConsoleKeyInfo key;
      bool moved = false;
            
      Console.BufferWidth += 4;
      Console.Clear();
      
      ShowConsoleStatistics();
      do 
      {
         key = Console.ReadKey(true);
         if (key.Key == ConsoleKey.LeftArrow)
         {
            int pos = Console.WindowLeft - 1;
            if (pos >= 0 && pos + Console.WindowWidth <= Console.BufferWidth)
            { 
               Console.WindowLeft = pos;
               moved = true;
            }       
         } 
         else if (key.Key == ConsoleKey.RightArrow)
         {
            int pos = Console.WindowLeft + 1;
            if (pos + Console.WindowWidth <= Console.BufferWidth)
            { 
               Console.WindowLeft = pos;
               moved = true;
            }
         }
         if (moved)
         { 
            ShowConsoleStatistics(); 
            moved = false;
         }   
         Console.WriteLine();
      } while (true);
   }

   private static void ShowConsoleStatistics()
   {
      Console.WriteLine("Console statistics:");
      Console.WriteLine("   Buffer: {0} x {1}", Console.BufferHeight, Console.BufferWidth);
      Console.WriteLine("   Window: {0} x {1}", Console.WindowHeight, Console.WindowWidth);
      Console.WriteLine("   Window starts at {0}.", Console.WindowLeft);
      Console.WriteLine("Press <- or -> to move window, Ctrl+C to exit.");
   }
}
Module Example
   Public Sub Main()
      Dim key As ConsoleKeyInfo
      Dim moved As Boolean = False
            
      Console.BufferWidth = 120
      Console.Clear()
      
      ShowConsoleStatistics()
      Do While True
         key = Console.ReadKey(True)
         If key.Key = ConsoleKey.LeftArrow Then
            Dim pos As Integer = Console.WindowLeft - 1
            If pos >= 0 And pos + Console.WindowWidth <= Console.BufferWidth Then 
               Console.WindowLeft = pos
               moved = True
            End If       
         ElseIf key.Key = ConsoleKey.RightArrow Then
            Dim pos As Integer = Console.WindowLeft + 1
            If pos + Console.WindowWidth <= Console.BufferWidth Then 
               Console.WindowLeft = pos
               moved = True
            End If
         End If
         If moved Then ShowConsoleStatistics() : moved = False
         Console.WriteLine()
      Loop
   End Sub
   
   Private Sub ShowConsoleStatistics()
      Console.WriteLine("Console statistics:")
      Console.WriteLine("   Buffer: {0} x {1}", Console.BufferHeight, Console.BufferWidth)
      Console.WriteLine("   Window: {0} x {1}", Console.WindowHeight, Console.WindowWidth)
      Console.WriteLine("   Window starts at {0}.", Console.WindowLeft)
      Console.WriteLine("Press <- or -> to move window, Ctrl+C to exit.")
   End Sub
End Module

注解

控制台将一个矩形窗口表示为一个更大的矩形缓冲区区域。The console represents a rectangular window into a larger rectangular buffer area. 窗口和缓冲区都按行数垂直度量,并按列数水平测量。Both the window and the buffer are measured vertically by their number of rows and horizontally by their number of columns. 缓冲区的尺寸由 BufferHeightBufferWidth 属性定义。The dimensions of the buffer area are defined by the BufferHeight and BufferWidth properties. 控制台区域的尺寸由 WindowHeightWindowWidth 属性定义。The dimensions of the console area are defined by the WindowHeight and WindowWidth properties. WindowLeft 属性确定在控制台窗口的第一列中显示缓冲区区域的哪一列。The WindowLeft property determines which column of the buffer area is displayed in the first column of the console window. WindowLeft 属性的值的范围为0到 BufferWidth - WindowWidthThe value of the WindowLeft property can range from 0 to BufferWidth - WindowWidth. 尝试将其设置为超出该范围的值将引发 ArgumentOutOfRangeExceptionAttempting to set it to a value outside that range throws an ArgumentOutOfRangeException.

当控制台窗口首次打开时,WindowLeft 属性的默认值为零,指示控制台显示的第一列对应于缓冲区中的第一列(位置为零的列)。When a console window first opens, the default value of the WindowLeft property is zero, which indicates that the first column shown by the console corresponds to the first column (the column at position zero) in the buffer area. 控制台窗口和缓冲区区域的默认宽度均为80列。The default width of both the console window and the buffer area is 80 columns. 这意味着,仅当控制台窗口变窄或缓冲区区域变宽时,才能修改 WindowLeft 属性。This means that the WindowLeft property can be modified only if the console window is made narrower or the buffer area is made wider.

请注意,如果缓冲区的宽度超过控制台窗口的宽度,则当用户使用水平滚动条来定义窗口与缓冲区区域的关系时,将自动调整 WindowLeft 属性的值。Note that if the width of the buffer area exceeds the width of the console window, the value of the WindowLeft property is automatically adjusted when the user uses the horizontal scroll bar to define the window's relationship to the buffer area.

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

适用于