public static unsafe void SetWindowPosition(int left, int top) { // Get the size of the current console window Interop.mincore.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); Interop.mincore.SMALL_RECT srWindow = csbi.srWindow; // Check for arithmetic underflows & overflows. int newRight = left + srWindow.Right - srWindow.Left + 1; if (left < 0 || newRight > csbi.dwSize.X || newRight < 0) { throw new ArgumentOutOfRangeException("left", left, SR.ArgumentOutOfRange_ConsoleWindowPos); } int newBottom = top + srWindow.Bottom - srWindow.Top + 1; if (top < 0 || newBottom > csbi.dwSize.Y || newBottom < 0) { throw new ArgumentOutOfRangeException("top", top, SR.ArgumentOutOfRange_ConsoleWindowPos); } // Preserve the size, but move the position. srWindow.Bottom -= (short)(srWindow.Top - top); srWindow.Right -= (short)(srWindow.Left - left); srWindow.Left = (short)left; srWindow.Top = (short)top; bool r = Interop.mincore.SetConsoleWindowInfo(OutputHandle, true, &srWindow); if (!r) { throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); } }
public static void ResetColor() { bool succeeded; Interop.mincore.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(false, out succeeded); if (!succeeded) { return; // For code that may be used from Windows app w/ no console } Debug.Assert(_haveReadDefaultColors, "Resetting color before we've read the default foreground color!"); // Ignore errors here - there are some scenarios for running code that wants // to print in colors to the console in a Windows application. Interop.mincore.SetConsoleTextAttribute(OutputHandle, (short)(ushort)_defaultColors); }
[System.Security.SecuritySafeCritical] // auto-generated public static void SetBufferSize(int width, int height) { // Ensure the new size is not smaller than the console window Interop.mincore.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); Interop.mincore.SMALL_RECT srWindow = csbi.srWindow; if (width < srWindow.Right + 1 || width >= short.MaxValue) { throw new ArgumentOutOfRangeException("width", width, SR.ArgumentOutOfRange_ConsoleBufferLessThanWindowSize); } if (height < srWindow.Bottom + 1 || height >= short.MaxValue) { throw new ArgumentOutOfRangeException("height", height, SR.ArgumentOutOfRange_ConsoleBufferLessThanWindowSize); } Interop.mincore.COORD size = new Interop.mincore.COORD(); size.X = (short)width; size.Y = (short)height; if (!Interop.mincore.SetConsoleScreenBufferSize(OutputHandle, size)) { throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); } }
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("left", left, SR.ArgumentOutOfRange_ConsoleBufferBoundaries); } if (top < 0 || top >= short.MaxValue) { throw new ArgumentOutOfRangeException("top", top, SR.ArgumentOutOfRange_ConsoleBufferBoundaries); } Contract.EndContractBlock(); IntPtr hConsole = OutputHandle; Interop.mincore.COORD coords = new Interop.mincore.COORD(); coords.X = (short)left; coords.Y = (short)top; if (!Interop.mincore.SetConsoleCursorPosition(hConsole, coords)) { // Give a nice error message for out of range sizes int errorCode = Marshal.GetLastWin32Error(); Interop.mincore.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); if (left < 0 || left >= csbi.dwSize.X) { throw new ArgumentOutOfRangeException("left", left, SR.ArgumentOutOfRange_ConsoleBufferBoundaries); } if (top < 0 || top >= csbi.dwSize.Y) { throw new ArgumentOutOfRangeException("top", top, SR.ArgumentOutOfRange_ConsoleBufferBoundaries); } throw Win32Marshal.GetExceptionForWin32Error(errorCode); } }
public static unsafe void SetWindowSize(int width, int height) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", width, SR.ArgumentOutOfRange_NeedPosNum); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", height, SR.ArgumentOutOfRange_NeedPosNum); } // Get the position of the current console window Interop.mincore.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); // If the buffer is smaller than this new window size, resize the // buffer to be large enough. Include window position. bool resizeBuffer = false; Interop.mincore.COORD size = new Interop.mincore.COORD(); size.X = csbi.dwSize.X; size.Y = csbi.dwSize.Y; if (csbi.dwSize.X < csbi.srWindow.Left + width) { if (csbi.srWindow.Left >= short.MaxValue - width) { throw new ArgumentOutOfRangeException("width", SR.ArgumentOutOfRange_ConsoleWindowBufferSize); } size.X = (short)(csbi.srWindow.Left + width); resizeBuffer = true; } if (csbi.dwSize.Y < csbi.srWindow.Top + height) { if (csbi.srWindow.Top >= short.MaxValue - height) { throw new ArgumentOutOfRangeException("height", SR.ArgumentOutOfRange_ConsoleWindowBufferSize); } size.Y = (short)(csbi.srWindow.Top + height); resizeBuffer = true; } if (resizeBuffer) { if (!Interop.mincore.SetConsoleScreenBufferSize(OutputHandle, size)) { throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); } } Interop.mincore.SMALL_RECT srWindow = csbi.srWindow; // Preserve the position, but change the size. srWindow.Bottom = (short)(srWindow.Top + height - 1); srWindow.Right = (short)(srWindow.Left + width - 1); if (!Interop.mincore.SetConsoleWindowInfo(OutputHandle, true, &srWindow)) { int errorCode = Marshal.GetLastWin32Error(); // If we resized the buffer, un-resize it. if (resizeBuffer) { Interop.mincore.SetConsoleScreenBufferSize(OutputHandle, csbi.dwSize); } // Try to give a better error message here Interop.mincore.COORD bounds = Interop.mincore.GetLargestConsoleWindowSize(OutputHandle); if (width > bounds.X) { throw new ArgumentOutOfRangeException("width", width, SR.Format(SR.ArgumentOutOfRange_ConsoleWindowSize_Size, bounds.X)); } if (height > bounds.Y) { throw new ArgumentOutOfRangeException("height", height, SR.Format(SR.ArgumentOutOfRange_ConsoleWindowSize_Size, bounds.Y)); } throw Win32Marshal.GetExceptionForWin32Error(errorCode); } }