Console.WindowTop 属性

定义

获取或设置控制台窗口区域的最顶部相对于屏幕缓冲区的位置。

public:
 static property int WindowTop { int get(); void set(int value); };
public static int WindowTop { get; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
public static int WindowTop { get; set; }
[<set: System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.WindowTop : int with get, set
member this.WindowTop : int with get, set
Public Shared Property WindowTop As Integer

属性值

Int32

控制台窗口最顶部的位置,以行为单位。

属性

例外

在集运算中,要赋予的值小于零。

  • 或 -

赋值之后,WindowTopWindowHeight 将超过 BufferHeight

读取或写入信息时发生错误。

设置操作在 Windows 之外的操作系统上调用。

示例

下面的示例演示了 WindowLeftWindowTopWindowWidthWindowHeightBufferWidthBufferHeightCursorVisible 属性; 以及、和 SetWindowPosition SetBufferSize ReadKey 方法。 该示例基于屏幕缓冲区宽度在屏幕缓冲区中绘制网格模式。 然后,该示例将移动控制台窗口,以响应按下的向上键、向下键、向左键或向右键的控制台键。 网格模式可帮助您查看控制台窗口相对于屏幕缓冲区的移动。

// This example demonstrates the Console.WindowLeft and
//                               Console.WindowTop properties.
using namespace System;
using namespace System::Text;
using namespace System::IO;

//
int saveBufferWidth;
int saveBufferHeight;
int saveWindowHeight;
int saveWindowWidth;
bool saveCursorVisible;

//
int main()
{
   String^ m1 = "1) Press the cursor keys to move the console window.\n"
   "2) Press any key to begin. When you're finished...\n"
   "3) Press the Escape key to quit.";
   String^ g1 = "+----";
   String^ g2 = "|    ";
   String^ grid1;
   String^ grid2;
   StringBuilder^ sbG1 = gcnew StringBuilder;
   StringBuilder^ sbG2 = gcnew StringBuilder;
   ConsoleKeyInfo cki;
   int y;
   
   //
   try
   {
      saveBufferWidth = Console::BufferWidth;
      saveBufferHeight = Console::BufferHeight;
      saveWindowHeight = Console::WindowHeight;
      saveWindowWidth = Console::WindowWidth;
      saveCursorVisible = Console::CursorVisible;
      
      //
      Console::Clear();
      Console::WriteLine( m1 );
      Console::ReadKey( true );
      
      // Set the smallest possible window size before setting the buffer size.
      Console::SetWindowSize( 1, 1 );
      Console::SetBufferSize( 80, 80 );
      Console::SetWindowSize( 40, 20 );
      
      // Create grid lines to fit the buffer. (The buffer width is 80, but
      // this same technique could be used with an arbitrary buffer width.)
      for ( y = 0; y < Console::BufferWidth / g1->Length; y++ )
      {
         sbG1->Append( g1 );
         sbG2->Append( g2 );

      }
      sbG1->Append( g1, 0, Console::BufferWidth % g1->Length );
      sbG2->Append( g2, 0, Console::BufferWidth % g2->Length );
      grid1 = sbG1->ToString();
      grid2 = sbG2->ToString();
      Console::CursorVisible = false;
      Console::Clear();
      for ( y = 0; y < Console::BufferHeight - 1; y++ )
      {
         if ( y % 3 == 0 )
                  Console::Write( grid1 );
         else
                  Console::Write( grid2 );

      }
      Console::SetWindowPosition( 0, 0 );
      do
      {
         cki = Console::ReadKey( true );
         switch ( cki.Key )
         {
            case ConsoleKey::LeftArrow:
               if ( Console::WindowLeft > 0 )
                              Console::SetWindowPosition( Console::WindowLeft - 1, Console::WindowTop );
               break;

            case ConsoleKey::UpArrow:
               if ( Console::WindowTop > 0 )
                              Console::SetWindowPosition( Console::WindowLeft, Console::WindowTop - 1 );
               break;

            case ConsoleKey::RightArrow:
               if ( Console::WindowLeft < (Console::BufferWidth - Console::WindowWidth) )
                              Console::SetWindowPosition( Console::WindowLeft + 1, Console::WindowTop );
               break;

            case ConsoleKey::DownArrow:
               if ( Console::WindowTop < (Console::BufferHeight - Console::WindowHeight) )
                              Console::SetWindowPosition( Console::WindowLeft, Console::WindowTop + 1 );
               break;
         }
      }
      while ( cki.Key != ConsoleKey::Escape );
   }
   catch ( IOException^ e ) 
   {
      Console::WriteLine( e->Message );
   }
   finally
   {
      Console::Clear();
      Console::SetWindowSize( 1, 1 );
      Console::SetBufferSize( saveBufferWidth, saveBufferHeight );
      Console::SetWindowSize( saveWindowWidth, saveWindowHeight );
      Console::CursorVisible = saveCursorVisible;
   }

} // end Main


/*
This example produces results similar to the following:

1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.

...

+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-

*/
// This example demonstrates the Console.WindowLeft and
//                               Console.WindowTop properties.
using System;
using System.Text;
using System.IO;
//
class Sample
{
    public static int saveBufferWidth;
    public static int saveBufferHeight;
    public static int saveWindowHeight;
    public static int saveWindowWidth;
    public static bool saveCursorVisible;
//
    public static void Main()
    {
    string m1 = "1) Press the cursor keys to move the console window.\n" +
                "2) Press any key to begin. When you're finished...\n" +
                "3) Press the Escape key to quit.";
    string g1 = "+----";
    string g2 = "|    ";
    string grid1;
    string grid2;
    StringBuilder sbG1 = new StringBuilder();
    StringBuilder sbG2 = new StringBuilder();
    ConsoleKeyInfo cki;
    int y;
//
    try
    {
    saveBufferWidth  = Console.BufferWidth;
    saveBufferHeight = Console.BufferHeight;
    saveWindowHeight = Console.WindowHeight;
    saveWindowWidth  = Console.WindowWidth;
    saveCursorVisible = Console.CursorVisible;
//
    Console.Clear();
    Console.WriteLine(m1);
    Console.ReadKey(true);

// Set the smallest possible window size before setting the buffer size.
    Console.SetWindowSize(1, 1);
    Console.SetBufferSize(80, 80);
    Console.SetWindowSize(40, 20);

// Create grid lines to fit the buffer. (The buffer width is 80, but
// this same technique could be used with an arbitrary buffer width.)
    for (y = 0; y < Console.BufferWidth/g1.Length; y++)
        {
        sbG1.Append(g1);
        sbG2.Append(g2);
        }
    sbG1.Append(g1, 0, Console.BufferWidth%g1.Length);
    sbG2.Append(g2, 0, Console.BufferWidth%g2.Length);
    grid1 = sbG1.ToString();
    grid2 = sbG2.ToString();

    Console.CursorVisible = false;
    Console.Clear();
    for (y = 0; y < Console.BufferHeight-1; y++)
        {
        if (y%3 == 0)
            Console.Write(grid1);
        else
            Console.Write(grid2);
        }

    Console.SetWindowPosition(0, 0);
    do
        {
        cki = Console.ReadKey(true);
        switch (cki.Key)
            {
            case ConsoleKey.LeftArrow:
                if (Console.WindowLeft > 0)
                    Console.SetWindowPosition(
                            Console.WindowLeft-1, Console.WindowTop);
                break;
            case ConsoleKey.UpArrow:
                if (Console.WindowTop > 0)
                    Console.SetWindowPosition(
                            Console.WindowLeft, Console.WindowTop-1);
                break;
            case ConsoleKey.RightArrow:
                if (Console.WindowLeft < (Console.BufferWidth-Console.WindowWidth))
                    Console.SetWindowPosition(
                            Console.WindowLeft+1, Console.WindowTop);
                break;
            case ConsoleKey.DownArrow:
                if (Console.WindowTop < (Console.BufferHeight-Console.WindowHeight))
                    Console.SetWindowPosition(
                            Console.WindowLeft, Console.WindowTop+1);
                break;
            }
        }
    while (cki.Key != ConsoleKey.Escape);  // end do-while
    } // end try
    catch (IOException e)
        {
        Console.WriteLine(e.Message);
        }
    finally
        {
        Console.Clear();
        Console.SetWindowSize(1, 1);
        Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
        Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
        Console.CursorVisible = saveCursorVisible;
        }
    } // end Main
} // end Sample
/*
This example produces results similar to the following:

1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.

...

+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-

*/
' This example demonstrates the Console.WindowLeft and
'                               Console.WindowTop properties.
Imports System.IO
Imports System.Text

Class Sample
   Public Shared saveBufferWidth As Integer
   Public Shared saveBufferHeight As Integer
   Public Shared saveWindowHeight As Integer
   Public Shared saveWindowWidth As Integer
   Public Shared saveCursorVisible As Boolean
   
   Public Shared Sub Main()
      Dim m1 As String = _
          "1) Press the cursor keys to move the console window." & vbCrlf & _
          "2) Press any key to begin. When you're finished..."   & vbCrlf & _
          "3) Press the Escape key to quit."
      Dim g1 As String = "+----"
      Dim g2 As String = "|    "
      Dim grid1 As String
      Dim grid2 As String
      Dim sbG1 As New StringBuilder()
      Dim sbG2 As New StringBuilder()
      Dim cki As ConsoleKeyInfo
      Dim y As Integer
      '
      Try
         saveBufferWidth = Console.BufferWidth
         saveBufferHeight = Console.BufferHeight
         saveWindowHeight = Console.WindowHeight
         saveWindowWidth = Console.WindowWidth
         saveCursorVisible = Console.CursorVisible
         '
         Console.Clear()
         Console.WriteLine(m1)
         Console.ReadKey(True)
         
         ' Set the smallest possible window size before setting the buffer size.
         Console.SetWindowSize(1, 1)
         Console.SetBufferSize(80, 80)
         Console.SetWindowSize(40, 20)
         
         ' Create grid lines to fit the buffer. (The buffer width is 80, but
         ' this same technique could be used with an arbitrary buffer width.)
         For y = 0 To (Console.BufferWidth / g1.Length) - 1
            sbG1.Append(g1)
            sbG2.Append(g2)
         Next y
         sbG1.Append(g1, 0, Console.BufferWidth Mod g1.Length)
         sbG2.Append(g2, 0, Console.BufferWidth Mod g2.Length)
         grid1 = sbG1.ToString()
         grid2 = sbG2.ToString()
         
         Console.CursorVisible = False
         Console.Clear()
         For y = 0 To (Console.BufferHeight - 2)
            If y Mod 3 = 0 Then
               Console.Write(grid1)
            Else
               Console.Write(grid2)
            End If
         Next y 
         '
         Console.SetWindowPosition(0, 0)
         Do
            cki = Console.ReadKey(True)
            Select Case cki.Key
               Case ConsoleKey.LeftArrow
                  If Console.WindowLeft > 0 Then
                     Console.SetWindowPosition(Console.WindowLeft - 1, Console.WindowTop)
                  End If
               Case ConsoleKey.UpArrow
                  If Console.WindowTop > 0 Then
                     Console.SetWindowPosition(Console.WindowLeft, Console.WindowTop - 1)
                  End If
               Case ConsoleKey.RightArrow
                  If Console.WindowLeft < Console.BufferWidth - Console.WindowWidth Then
                     Console.SetWindowPosition(Console.WindowLeft + 1, Console.WindowTop)
                  End If
               Case ConsoleKey.DownArrow
                  If Console.WindowTop < Console.BufferHeight - Console.WindowHeight Then
                     Console.SetWindowPosition(Console.WindowLeft, Console.WindowTop + 1)
                  End If
            End Select
         Loop While cki.Key <> ConsoleKey.Escape
      ' end do-while
      ' end try
      Catch e As IOException
         Console.WriteLine(e.Message)
      Finally
         Console.Clear()
         Console.SetWindowSize(1, 1)
         Console.SetBufferSize(saveBufferWidth, saveBufferHeight)
         Console.SetWindowSize(saveWindowWidth, saveWindowHeight)
         Console.CursorVisible = saveCursorVisible
      End Try
   End Sub
End Class
'
'This example produces results similar to the following:
'
'1) Press the cursor keys to move the console window.
'2) Press any key to begin. When you're finished...
'3) Press the Escape key to quit.
'
'...
'
'+----+----+----+-
'|    |    |    |
'|    |    |    |
'+----+----+----+-
'|    |    |    |
'|    |    |    |
'+----+----+----+-
'
// This example demonstrates the Console.WindowLeft and
//                               Console.WindowTop properties.
open System
open System.Text
open System.IO

[<EntryPoint>]
let main argv =
    let m1 = 
        "1) Press the cursor keys to move the console window.\n" + 
        "2) Press any key to begin. When you're finished...\n" + 
        "3) Press the Escape key to quit."
    let g1 = "+----";
    let g2 = "|    ";
    let sbG1 = StringBuilder()
    let sbG2 = StringBuilder()


    let saveBufferWidth = Console.BufferWidth
    let saveBufferHeight = Console.BufferHeight
    let saveWindowHeight = Console.WindowHeight
    let saveWindowWidth = Console.WindowWidth
    let saveCursorVisible = Console.CursorVisible

    try
        try
            Console.Clear()
            Console.WriteLine(m1)
            Console.ReadKey(true) |> ignore

            // Set the smallest possible window size before setting the buffer size.
            Console.SetWindowSize(1, 1)
            Console.SetBufferSize(80, 80)
            Console.SetWindowSize(40, 20)

            // Create grid lines to fit the buffer. (The buffer width is 80, but
            // this same technique could be used with an arbitrary buffer width.)
            // for y = 0 to (Console.BufferWidth / g1.Length - 1)
            for _ in 1..(Console.BufferWidth / g1.Length) do
                (sbG1.Append(g1)) |> ignore
                (sbG2.Append(g2)) |> ignore

            sbG1.Append(g1, 0, Console.BufferWidth % g1.Length) |> ignore
            sbG2.Append(g2, 0, Console.BufferWidth % g2.Length) |> ignore
            let grid1 = sbG1.ToString()
            let grid2 = sbG2.ToString()

            Console.CursorVisible <- false
            Console.Clear()
            for y in 0..(Console.BufferHeight - 1) do
                if (y % 3 = 0) then
                    Console.Write(grid1)
                else
                    Console.Write(grid2)

            Console.SetWindowPosition(0, 0)

            let interactiveKeySequence = 
                Seq.initInfinite (fun _ -> (Console.ReadKey(true)).Key)
                |> Seq.takeWhile (fun key -> key <> ConsoleKey.Escape)

            for key in interactiveKeySequence do
                match key with
                | ConsoleKey.LeftArrow ->
                    if Console.WindowLeft > 0
                    then Console.SetWindowPosition(Console.WindowLeft - 1, Console.WindowTop)
                | ConsoleKey.UpArrow ->
                    if Console.WindowTop > 0
                    then Console.SetWindowPosition(Console.WindowLeft, Console.WindowTop - 1)
                | ConsoleKey.RightArrow ->
                    if Console.WindowLeft < (Console.BufferWidth - Console.WindowWidth)
                    then Console.SetWindowPosition(Console.WindowLeft + 1, Console.WindowTop)
                | ConsoleKey.DownArrow ->
                    if Console.WindowTop < (Console.BufferHeight - Console.WindowHeight)
                    then Console.SetWindowPosition(Console.WindowLeft, Console.WindowTop + 1)
                | _ -> 
                    ()
            0
        with 
        | :? IOException as ex -> printf "%s" ex.Message; 1
    finally
        Console.Clear()
        Console.SetWindowSize(1, 1)
        Console.SetBufferSize(saveBufferWidth, saveBufferHeight)
        Console.SetWindowSize(saveWindowWidth, saveWindowHeight)
        Console.CursorVisible <- saveCursorVisible

注解

控制台将一个矩形窗口表示为一个更大的矩形缓冲区区域。 窗口和缓冲区都按行数垂直度量,并按列数水平测量。 缓冲区的尺寸由 BufferHeightBufferWidth 属性定义。 控制台区域的尺寸由 WindowHeightWindowWidth 属性定义。 WindowTop属性决定哪个缓冲区区域显示在控制台窗口的第一列中。 属性的值的 WindowTop 范围为0到 BufferHeight - WindowHeight 。 尝试将其设置为超出该范围的值将引发 ArgumentOutOfRangeException

尝试 WindowTop 在输出重定向时设置属性的值会引发 IOException 异常。 若要防止出现此异常,只能在属性返回时设置此属性的值 IsOutputRedirected false

适用于