Bagikan melalui


Console.WindowLeft Properti

Definisi

Mendapatkan atau mengatur posisi paling kiri area jendela konsol relatif terhadap buffer layar.

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

Nilai Properti

Posisi jendela konsol paling kiri diukur dalam kolom.

Atribut

Pengecualian

Dalam operasi yang ditetapkan, nilai yang akan ditetapkan kurang dari nol.

-atau-

Sebagai hasil dari penugasan, WindowLeft plus WindowWidth akan melebihi BufferWidth.

Kesalahan membaca atau menulis informasi.

Operasi set dipanggil pada sistem operasi selain Windows.

Contoh

Contoh berikut membuka jendela konsol 80 kolom dan menentukan area buffer yang lebarnya 120 kolom. Ini menampilkan informasi tentang ukuran jendela dan buffer, lalu menunggu pengguna menekan tombol PANAH KIRI atau tombol PANAH KANAN. Dalam kasus sebelumnya, ia mengurangi nilai WindowLeft properti satu per satu jika hasilnya adalah nilai hukum. Dalam kasus terakhir, itu meningkatkan nilai WindowLeft properti satu per satu jika hasilnya akan legal. Perhatikan bahwa contoh tidak harus menangani ArgumentOutOfRangeException, karena memeriksa bahwa nilai yang akan ditetapkan ke WindowLeft properti tidak negatif dan tidak menyebabkan jumlah WindowLeft properti dan WindowWidth melebihi BufferWidth nilai properti.

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

Keterangan

Konsol mewakili jendela persegi panjang ke area buffer persegi panjang yang lebih besar. Jendela dan buffer diukur secara vertikal berdasarkan jumlah barisnya dan secara horizontal berdasarkan jumlah kolomnya. Dimensi area buffer ditentukan oleh BufferHeight properti dan BufferWidth . Dimensi area konsol ditentukan oleh WindowHeight properti dan WindowWidth . Properti WindowLeft menentukan kolom area buffer mana yang ditampilkan di kolom pertama jendela konsol. Nilai WindowLeft properti dapat berkisar dari 0 hingga - BufferWidthWindowWidth . Mencoba mengaturnya ke nilai di luar rentang tersebut melempar ArgumentOutOfRangeException.

Saat jendela konsol pertama kali terbuka, nilai WindowLeft default properti adalah nol, yang menunjukkan bahwa kolom pertama yang ditampilkan oleh konsol sesuai dengan kolom pertama (kolom pada posisi nol) di area buffer. Lebar default jendela konsol dan area buffer adalah 80 kolom. Ini berarti bahwa WindowLeft properti dapat dimodifikasi hanya jika jendela konsol dibuat lebih sempit atau area buffer dibuat lebih luas.

Perhatikan bahwa jika lebar area buffer melebihi lebar jendela konsol, nilai WindowLeft properti secara otomatis disesuaikan ketika pengguna menggunakan bilah gulir horizontal untuk menentukan hubungan jendela ke area buffer.

Mencoba mengatur nilai WindowLeft properti saat output dialihkan melemparkan IOException pengecualian. Untuk mencegah pengecualian, Anda dapat mengatur nilai properti ini hanya jika IsOutputRedirected properti mengembalikan false.

Berlaku untuk