public static void SetCursorPosition(int left, int top)
{
// Note on argument checking - the upper bounds are NOT correct
// here! But it looks slightly expensive to compute them. Let
// Windows calculate them, then we'll give a nice error message.
if (left < 0 || left >= short.MaxValue)
throw new ArgumentOutOfRangeException(nameof(left), left, SR.ArgumentOutOfRange_ConsoleBufferBoundaries);
if (top < 0 || top >= short.MaxValue)
throw new ArgumentOutOfRangeException(nameof(top), top, SR.ArgumentOutOfRange_ConsoleBufferBoundaries);
Contract.EndContractBlock();
IntPtr hConsole = OutputHandle;
Interop.Kernel32.COORD coords = new Interop.Kernel32.COORD();
coords.X = (short)left;
coords.Y = (short)top;
if (!Interop.Kernel32.SetConsoleCursorPosition(hConsole, coords))
{
// Give a nice error message for out of range sizes
int errorCode = Marshal.GetLastWin32Error();
Interop.Kernel32.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo();
if (left < 0 || left >= csbi.dwSize.X)
throw new ArgumentOutOfRangeException(nameof(left), left, SR.ArgumentOutOfRange_ConsoleBufferBoundaries);
if (top < 0 || top >= csbi.dwSize.Y)
throw new ArgumentOutOfRangeException(nameof(top), top, SR.ArgumentOutOfRange_ConsoleBufferBoundaries);
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
}