C#中的Console.SetWindowPosition(Int32,Int32)方法用于设置控制台窗口相对于屏幕缓冲区的位置。
Syntax: public static void SetWindowposition(int left, int top);
Parameters:
left: It is the column position of the upper left corner of the console window.
top: It is the row position of the upper left corner of the console window.
例外情况:
- ArgumentOutOfRangeException:当left或top小于0或left + WindowWidth > BufferWidth或top + Windowheight > BufferHeight时。
- SecurityException:如果用户没有执行此操作的权限。
例子:
// C# Program to illustrate the use of
// Console.WindowPosition() method
using System;
using System.Text;
using System.IO;
class GFG {
// Main Method
public static void Main(string[] args)
{
Console.SetWindowSize(20, 20);
// setting buffer size
Console.SetBufferSize(80, 80);
// using the method
Console.SetWindowPosition(0, 0);
Console.WriteLine("Hello GFG!");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
输出:
不使用Console.WindowPosition()方法时:
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.console.setwindowposition?view=netframework-4.7.2