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")>]
member this.WindowLeft : int with get, set
member this.WindowLeft : int with get, set
Public Shared Property WindowLeft As Integer
属性值
控制台窗口的最左边的位置,以列为单位。
- 属性
例外
读取或写入信息时发生错误。
设置操作在 Windows 之外的操作系统上调用。
示例
下面的示例打开一个80列控制台窗口,并定义一个宽度为120列的缓冲区。 它显示有关窗口和缓冲区大小的信息,然后等待用户按向左键或向右键。 在前一种情况下, WindowLeft 如果结果是合法值,则它会将属性的值减一。 在后一种情况下,如果结果是合法的,它会将属性的值增加 WindowLeft 1。 请注意,该示例不需要处理 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 WindowWidth 定义。 WindowLeft属性确定在控制台窗口的第一列中显示缓冲区的哪一列。 属性的值 WindowLeft 的范围可以是 0 到 BufferWidth - WindowWidth 。 尝试将该值设置为超出该范围的值将引发 ArgumentOutOfRangeException 。
首次打开控制台窗口时,属性的默认值为零,指示控制台显示的第一列对应于缓冲区中 (位置为零) 列的第一列。 WindowLeft 控制台窗口和缓冲区的默认宽度为 80 列。 这意味着,只有在控制台窗口变窄或缓冲区变宽时, WindowLeft 才能修改属性。
请注意,如果缓冲区的宽度超过控制台窗口的宽度,则当用户使用水平滚动条定义窗口与缓冲区的关系时,将自动调整 属性的值。 WindowLeft
在重定向输出时尝试设置 属性的值 WindowLeft 会引发 IOException 异常。 若要防止出现此异常,只能在属性返回时设置此属性的值 IsOutputRedirected false
。