Console.WindowLeft 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置相对于屏幕缓冲区的控制台窗口区域最左侧的位置。
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
属性值
最左侧的控制台窗口位置以列为单位。
- 属性
例外
读取或写入信息时出错。
在 Windows 以外的操作系统上调用设置操作。
示例
以下示例打开一个 80 列控制台窗口,并定义宽为 120 列的缓冲区区域。 它显示有关窗口和缓冲区大小的信息,然后等待用户按向左键或向右键。 在前一种情况下,如果结果为合法值,则它将属性的值 WindowLeft 递减 1。 在后一种情况下,如果结果合法,它将属性的值 WindowLeft 增加一个。 请注意,该示例不必处理一个 ArgumentOutOfRangeException值,因为它检查要分配给 WindowLeft 该属性的值是否为负值,并且不会导致属性和属性的总 WindowLeft 和 WindowWidth 超过 BufferWidth 属性值。
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 属性 WindowWidth 定义。 该 WindowLeft 属性确定在控制台窗口的第一列中显示缓冲区区域的哪一列。 该属性的值WindowLeft可以介于 0 到 - BufferWidthWindowWidth . 尝试将其设置为该范围之外的值会引发一个 ArgumentOutOfRangeException。
当控制台窗口首次打开时,属性的 WindowLeft 默认值为零,指示控制台显示的第一列对应于缓冲区区域中第一列(位于零位置的列)。 控制台窗口和缓冲区区域的默认宽度为 80 列。 这意味着 WindowLeft ,仅当控制台窗口变窄或缓冲区区域变宽时,才能修改该属性。
请注意,如果缓冲区区域的宽度超过控制台窗口的宽度,则当用户使用水平滚动条定义窗口与缓冲区区域的关系时,将自动调整该属性的值 WindowLeft 。
当重定向输出时尝试设置属性的值 WindowLeft 会 IOException 引发异常。 若要防止异常,仅当属性返回false时IsOutputRedirected,才能设置此属性的值。