Console.SetCursorPosition(Int32,Int32)方法用于设置光标的位置。基本上,它指定下一个写操作将从控制台窗口开始的位置。如果指定的光标位置在控制台窗口中当前可见的区域之外,则窗口原点会自动更改以使光标可见。
Syntax: public static void SetCursorposition(int left, int top);
Parameters:
left: It is the column position of the cursor. Columns are numbered from left to right starting at 0.
top: It is the row position of the cursor. Rows are numbered from top to bottom starting at 0.
例外情况:
- ArgumentOutOfRangeException:如果left或top小于0或left > = BufferWidth或top > = BufferHeight 。
- SecurityException:如果用户没有执行此操作的权限。
例子:
// C# Program to illustrate
// Console.CursorPosition() method
using System;
class GFG {
// Main Method
public static void Main()
{
// setting the window size
Console.SetWindowSize(40, 40);
// setting buffer size of console
Console.SetBufferSize(80, 80);
// using the method
Console.SetCursorPosition(20, 20);
Console.WriteLine("Hello GFG!");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
输出:
不使用Console.SetCursorPosition()方法时:
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.console.setcursorposition?view=netframework-4.7.2