Console.SetCursorPosition(Int32, Int32) 方法

定义

设置光标位置。Sets the position of the cursor.

public:
 static void SetCursorPosition(int left, int top);
public static void SetCursorPosition (int left, int top);
static member SetCursorPosition : int * int -> unit
Public Shared Sub SetCursorPosition (left As Integer, top As Integer)

参数

left
Int32

光标的列位置。The column position of the cursor. 将从 0 开始从左到右对列进行编号。Columns are numbered from left to right starting at 0.

top
Int32

光标的行位置。The row position of the cursor. 从上到下,从 0 开始为行编号。Rows are numbered from top to bottom starting at 0.

例外

lefttop 小于零。left or top is less than zero.

- 或 --or- left 大于或等于 BufferWidthleft is greater than or equal to BufferWidth.

- 或 --or- top 大于或等于 BufferHeighttop is greater than or equal to BufferHeight.

用户没有执行此操作的权限。The user does not have permission to perform this action.

出现 I/O 错误。An I/O error occurred.

示例

此示例演示 CursorLeftCursorTop 属性以及 SetCursorPositionClear 方法。This example demonstrates the CursorLeft and CursorTop properties, and the SetCursorPosition and Clear methods. 该示例定位游标,该游标确定下一次写入的位置,以使用 "+"、"|" 和 "-" 字符串的组合来绘制5个字符,每5个字符矩形。The example positions the cursor, which determines where the next write will occur, to draw a 5 character by 5 character rectangle using a combination of "+", "|", and "-" strings. 请注意,可以使用其他字符串的组合来绘制矩形,并减少步骤。Note that the rectangle could be drawn with fewer steps using a combination of other strings.

// This example demonstrates the 
//     Console.CursorLeft and 
//     Console.CursorTop properties, and the
//     Console.SetCursorPosition and 
//     Console.Clear methods.
using namespace System;
int origRow;
int origCol;
void WriteAt( String^ s, int x, int y )
{
   try
   {
      Console::SetCursorPosition( origCol + x, origRow + y );
      Console::Write( s );
   }
   catch ( ArgumentOutOfRangeException^ e ) 
   {
      Console::Clear();
      Console::WriteLine( e->Message );
   }

}

int main()
{
   
   // Clear the screen, then save the top and left coordinates.
   Console::Clear();
   origRow = Console::CursorTop;
   origCol = Console::CursorLeft;
   
   // Draw the left side of a 5x5 rectangle, from top to bottom.
   WriteAt( "+", 0, 0 );
   WriteAt( "|", 0, 1 );
   WriteAt( "|", 0, 2 );
   WriteAt( "|", 0, 3 );
   WriteAt( "+", 0, 4 );
   
   // Draw the bottom side, from left to right.
   WriteAt( "-", 1, 4 ); // shortcut: WriteAt("---", 1, 4)
   WriteAt( "-", 2, 4 ); // ...
   WriteAt( "-", 3, 4 ); // ...
   WriteAt( "+", 4, 4 );
   
   // Draw the right side, from bottom to top.
   WriteAt( "|", 4, 3 );
   WriteAt( "|", 4, 2 );
   WriteAt( "|", 4, 1 );
   WriteAt( "+", 4, 0 );
   
   // Draw the top side, from right to left.
   WriteAt( "-", 3, 0 ); // shortcut: WriteAt("---", 1, 0)
   WriteAt( "-", 2, 0 ); // ...
   WriteAt( "-", 1, 0 ); // ...
   
   //
   WriteAt( "All done!", 0, 6 );
   Console::WriteLine();
}

/*
This example produces the following results:

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

All done!

*/
// This example demonstrates the 
//     Console.CursorLeft and 
//     Console.CursorTop properties, and the
//     Console.SetCursorPosition and 
//     Console.Clear methods.
using System;

class Sample 
{
    protected static int origRow;
    protected static int origCol;

    protected static void WriteAt(string s, int x, int y)
    {
    try
        {
        Console.SetCursorPosition(origCol+x, origRow+y);
        Console.Write(s);
        }
    catch (ArgumentOutOfRangeException e)
        {
        Console.Clear();
        Console.WriteLine(e.Message);
        }
    }

    public static void Main() 
    {
// Clear the screen, then save the top and left coordinates.
    Console.Clear();
    origRow = Console.CursorTop;
    origCol = Console.CursorLeft;

// Draw the left side of a 5x5 rectangle, from top to bottom.
    WriteAt("+", 0, 0);
    WriteAt("|", 0, 1);
    WriteAt("|", 0, 2);
    WriteAt("|", 0, 3);
    WriteAt("+", 0, 4);

// Draw the bottom side, from left to right.
    WriteAt("-", 1, 4); // shortcut: WriteAt("---", 1, 4)
    WriteAt("-", 2, 4); // ...
    WriteAt("-", 3, 4); // ...
    WriteAt("+", 4, 4);

// Draw the right side, from bottom to top.
    WriteAt("|", 4, 3);
    WriteAt("|", 4, 2);
    WriteAt("|", 4, 1);
    WriteAt("+", 4, 0);

// Draw the top side, from right to left.
    WriteAt("-", 3, 0); // shortcut: WriteAt("---", 1, 0)
    WriteAt("-", 2, 0); // ...
    WriteAt("-", 1, 0); // ...
//
    WriteAt("All done!", 0, 6);
    Console.WriteLine();
    }
}
/*
This example produces the following results:

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

All done!

*/
' This example demonstrates the 
'     Console.CursorLeft and 
'     Console.CursorTop properties, and the
'     Console.SetCursorPosition and 
'     Console.Clear methods.
Class Sample
   Protected Shared origRow As Integer
   Protected Shared origCol As Integer
   
   Protected Shared Sub WriteAt(s As String, x As Integer, y As Integer)
      Try
         Console.SetCursorPosition(origCol + x, origRow + y)
         Console.Write(s)
      Catch e As ArgumentOutOfRangeException
         Console.Clear()
         Console.WriteLine(e.Message)
      End Try
   End Sub
   
   Public Shared Sub Main()
      ' Clear the screen, then save the top and left coordinates.
      Console.Clear()
      origRow = Console.CursorTop
      origCol = Console.CursorLeft
      
      ' Draw the left side of a 5x5 rectangle, from top to bottom.
      WriteAt("+", 0, 0)
      WriteAt("|", 0, 1)
      WriteAt("|", 0, 2)
      WriteAt("|", 0, 3)
      WriteAt("+", 0, 4)
      
      ' Draw the bottom side, from left to right.
      WriteAt("-", 1, 4) ' shortcut: WriteAt("---", 1, 4)
      WriteAt("-", 2, 4) ' ...
      WriteAt("-", 3, 4) ' ...
      WriteAt("+", 4, 4)
      
      ' Draw the right side, from bottom to top.
      WriteAt("|", 4, 3)
      WriteAt("|", 4, 2)
      WriteAt("|", 4, 1)
      WriteAt("+", 4, 0)
      
      ' Draw the top side, from right to left.
      WriteAt("-", 3, 0) ' shortcut: WriteAt("---", 1, 0)
      WriteAt("-", 2, 0) ' ...
      WriteAt("-", 1, 0) ' ...
      '
      WriteAt("All done!", 0, 6)
      Console.WriteLine()
   End Sub
End Class
'
'This example produces the following results:
'
'+---+
'|   |
'|   |
'|   |
'+---+
'
'All done!
'

注解

使用 SetCursorPosition 方法来指定在控制台窗口中的下一次写入操作开始的位置。Use the SetCursorPosition method to specify where the next write operation in the console window is to begin. 如果指定的光标位置在控制台窗口中当前可见的区域外,则窗口源会自动更改,使游标可见。If the specified cursor position is outside the area that is currently visible in the console window, the window origin changes automatically to make the cursor visible.

每次将字符写入控制台窗口时,光标会自动移动到下一个字符位置。The cursor automatically moves to the next character position each time a character is written to the console window. 如果光标位于控制台窗口的右下角字符位置,则下一次写入操作将导致控制台窗口滚动,使光标保持可见。If the cursor is at the bottom right character position of the console window, the next write operation causes the console window to scroll so the cursor remains visible. 如果要在不导致控制台窗口滚动的情况下向右下字符位置写入字符,请使用 MoveBufferArea 方法将字符移动到该位置。If you want to write a character to the bottom right character position without causing the console window to scroll, use the MoveBufferArea method to move a character to that position.

适用于