Console.SetCursorPosition(Int32,Int32)方法用于设置光标的位置。本质上,它指定下一个写操作将从控制台窗口开始的位置。如果指定的光标位置在控制台窗口中当前可见的区域之外,则窗口原点会自动更改以使光标可见。
用法: public static void SetCursorposition(int left, int top);
参数:
left:它是光标的列位置。列从0开始从左到右编号。
top:它是光标的行位置。行从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/en-us/dotnet/api/system.console.setcursorposition?view=netframework-4.7.2
 
相关用法
- C# Uri.IsBaseOf(Uri)用法及代码示例
 - C# Random.Next()用法及代码示例
 - C# Uri.ToString()用法及代码示例
 - C# Uri.IsWellFormedOriginalString()用法及代码示例
 - C# Uri.GetHashCode()用法及代码示例
 - C# Math.Log()用法及代码示例
 - C# Uri.FromHex()用法及代码示例
 - C# Uri.IsHexDigit()用法及代码示例
 - C# Queue.Contains()用法及代码示例
 
注:本文由纯净天空筛选整理自kanakasrijaathukuri大神的英文原创作品 Console.SetCursorPosition() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
