Editar

Compartir vía


Console.WindowLeft Property

Definition

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; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
public static int WindowLeft { get; set; }
[<set: System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member WindowLeft : int with get, set
static member WindowLeft : int with get, set
Public Shared Property WindowLeft As Integer

Property Value

The leftmost console window position measured in columns.

Attributes

Exceptions

In a set operation, the value to be assigned is less than zero.

-or-

As a result of the assignment, WindowLeft plus WindowWidth would exceed BufferWidth.

Error reading or writing information.

The set operation is invoked on an operating system other than Windows.

Examples

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. In the former case, it decrements the value of the WindowLeft property by one if the result is a legal value. In the latter case, it increases the value of the WindowLeft property by one if the result would be legal. 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.");
   }
}
open System

let showConsoleStatistics () =
    printfn "Console statistics:"
    printfn $"   Buffer: {Console.BufferHeight} x {Console.BufferWidth}" 
    printfn $"   Window: {Console.WindowHeight} x {Console.WindowWidth}"
    printfn $"   Window starts at {Console.WindowLeft}."
    printfn "Press <- or -> to move window, Ctrl+C to exit."

Console.BufferWidth <- Console.BufferWidth + 4
Console.Clear()

showConsoleStatistics ()

let mutable moved = false

while true do
    let key = Console.ReadKey true
    if key.Key = ConsoleKey.LeftArrow then
        let pos = Console.WindowLeft - 1
        if pos >= 0 && pos + Console.WindowWidth <= Console.BufferWidth then
            Console.WindowLeft <- pos
            moved <- true
    elif key.Key = ConsoleKey.RightArrow then
        let pos = Console.WindowLeft + 1
        if pos + Console.WindowWidth <= Console.BufferWidth then
            Console.WindowLeft <- pos
            moved <- true
    if moved then
        showConsoleStatistics ()
        moved <- false
    
    printfn ""
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

Remarks

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. The dimensions of the buffer area are defined by the BufferHeight and BufferWidth properties. The dimensions of the console area are defined by the WindowHeight and WindowWidth properties. The WindowLeft property determines which column of the buffer area is displayed in the first column of the console window. The value of the WindowLeft property can range from 0 to BufferWidth - WindowWidth. Attempting to set it to a value outside that range throws an ArgumentOutOfRangeException.

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. The default width of both the console window and the buffer area is 80 columns. This means that the WindowLeft property can be modified only if the console window is made narrower or the buffer area is made wider.

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.

Attempting to set the value of the WindowLeft property when output is redirected throws an IOException exception. To prevent the exception, you can set the value of this property only if the IsOutputRedirected property returns false.

Applies to