共用方式為


Console.WindowLeft 屬性

定義

取得或設定相對於螢幕緩衝區的主控台視窗區域最左邊的位置。

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")>]
member this.WindowLeft : int with get, set
member this.WindowLeft : int with get, set
Public Shared Property WindowLeft As Integer

屬性值

Int32

以資料行數測量的主控台視窗最左邊的位置。

屬性

例外狀況

在設定作業中,要指派的值小於零。

-或-

由於指派的緣故,WindowLeft 加上 WindowWidth 會超過 BufferWidth

讀取或寫入資訊時發生錯誤。

在不是 Windows 的作業系統上叫用設定作業。

範例

下列範例會開啟80資料行的主控台視窗,並定義120資料行寬的緩衝區區域。 它會顯示視窗和緩衝區大小的相關資訊,然後等候使用者按下向左鍵或向右鍵鍵。 在先前的案例中, WindowLeft 如果結果為合法值,它會將屬性的值遞減一。 在後者的情況下,如果結果為合法,則會將屬性的值增加 WindowLeft 一。 請注意,此範例不需要處理 ArgumentOutOfRangeException ,因為它會檢查要指派給屬性的值 WindowLeft 不是負數,且不會造成 WindowLeft 和屬性的總和 WindowWidth 超過 BufferWidth 屬性值。

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

備註

主控台將矩形視窗表示為較大的矩形緩衝區區域。 視窗和緩衝區都是由其資料列數垂直測量,並依其資料行數水準測量。 緩衝區區域的維度是由和屬性所定義 BufferHeight BufferWidth 。 主控台區域的維度是由和屬性所定義 WindowHeight WindowWidthWindowLeft屬性會決定要在主控台視窗的第一個資料行中顯示緩衝區區域的哪一個資料行。 屬性值的 WindowLeft 範圍可以從0到 BufferWidth - WindowWidth 。 若嘗試將它設定為超出範圍的值,則會擲回 ArgumentOutOfRangeException

當主控台視窗第一次開啟時,屬性的預設值 WindowLeft 為零,表示主控台所顯示的第一個資料行對應于緩衝區區域中位置為零) (資料行的第一個資料行。 主控台視窗和緩衝區區域的預設寬度為80個數據行。 這表示 WindowLeft 只有在主控台視窗變得較窄或緩衝區區域更寬時,才可以修改此屬性。

請注意,如果緩衝區區域的寬度超過主控台視窗的寬度, WindowLeft 當使用者使用水準捲軸來定義視窗與緩衝區區域的關聯性時,屬性的值就會自動調整。

嘗試 WindowLeft 在重新導向輸出時設定屬性的值時,會擲回 IOException 例外狀況。 若要避免這個例外狀況,只有在屬性傳回時,才可以設定這個屬性的值 IsOutputRedirected false

適用於