public static unsafe void SetWindowSize(int width, int height) { if (width <= 0) throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum")); if (height <= 0) throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum")); Contract.EndContractBlock(); new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); // Get the position of the current console window Win32Native.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); bool r; // If the buffer is smaller than this new window size, resize the // buffer to be large enough. Include window position. bool resizeBuffer = false; Win32Native.COORD size = new Win32Native.COORD(); size.X = csbi.dwSize.X; size.Y = csbi.dwSize.Y; if (csbi.dwSize.X < csbi.srWindow.Left + width) { if (csbi.srWindow.Left >= Int16.MaxValue - width) throw new ArgumentOutOfRangeException("width", Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowBufferSize")); size.X = (short) (csbi.srWindow.Left + width); resizeBuffer = true; } if (csbi.dwSize.Y < csbi.srWindow.Top + height) { if (csbi.srWindow.Top >= Int16.MaxValue - height) throw new ArgumentOutOfRangeException("height", Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowBufferSize")); size.Y = (short) (csbi.srWindow.Top + height); resizeBuffer = true; } if (resizeBuffer) { r = Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, size); if (!r) __Error.WinIOError(); } Win32Native.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); r = Win32Native.SetConsoleWindowInfo(ConsoleOutputHandle, true, &srWindow); if (!r) { int errorCode = Marshal.GetLastWin32Error(); // If we resized the buffer, un-resize it. if (resizeBuffer) { Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, csbi.dwSize); } // Try to give a better error message here Win32Native.COORD bounds = Win32Native.GetLargestConsoleWindowSize(ConsoleOutputHandle); if (width > bounds.X) throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowSize_Size", bounds.X)); if (height > bounds.Y) throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowSize_Size", bounds.Y)); __Error.WinIOError(errorCode, String.Empty); } }
public unsafe static void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop, char sourceChar, ConsoleColor sourceForeColor, ConsoleColor sourceBackColor) { if (sourceForeColor < ConsoleColor.Black || sourceForeColor > ConsoleColor.White) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidConsoleColor"), "sourceForeColor"); if (sourceBackColor < ConsoleColor.Black || sourceBackColor > ConsoleColor.White) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidConsoleColor"), "sourceBackColor"); Contract.EndContractBlock(); Win32Native.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); Win32Native.COORD bufferSize = csbi.dwSize; if (sourceLeft < 0 || sourceLeft > bufferSize.X) throw new ArgumentOutOfRangeException("sourceLeft", sourceLeft, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); if (sourceTop < 0 || sourceTop > bufferSize.Y) throw new ArgumentOutOfRangeException("sourceTop", sourceTop, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); if (sourceWidth < 0 || sourceWidth > bufferSize.X - sourceLeft) throw new ArgumentOutOfRangeException("sourceWidth", sourceWidth, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); if (sourceHeight < 0 || sourceTop > bufferSize.Y - sourceHeight) throw new ArgumentOutOfRangeException("sourceHeight", sourceHeight, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); // Note: if the target range is partially in and partially out // of the buffer, then we let the OS clip it for us. if (targetLeft < 0 || targetLeft > bufferSize.X) throw new ArgumentOutOfRangeException("targetLeft", targetLeft, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); if (targetTop < 0 || targetTop > bufferSize.Y) throw new ArgumentOutOfRangeException("targetTop", targetTop, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); // If we're not doing any work, bail out now (Windows will return // an error otherwise) if (sourceWidth == 0 || sourceHeight == 0) return; new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); // Read data from the original location, blank it out, then write // it to the new location. This will handle overlapping source and // destination regions correctly. // See the "Reading and Writing Blocks of Characters and Attributes" // sample for help // Read the old data Win32Native.CHAR_INFO[] data = new Win32Native.CHAR_INFO[sourceWidth * sourceHeight]; bufferSize.X = (short) sourceWidth; bufferSize.Y = (short) sourceHeight; Win32Native.COORD bufferCoord = new Win32Native.COORD(); Win32Native.SMALL_RECT readRegion = new Win32Native.SMALL_RECT(); readRegion.Left = (short) sourceLeft; readRegion.Right = (short) (sourceLeft + sourceWidth - 1); readRegion.Top = (short) sourceTop; readRegion.Bottom = (short) (sourceTop + sourceHeight - 1); bool r; fixed(Win32Native.CHAR_INFO* pCharInfo = data) r = Win32Native.ReadConsoleOutput(ConsoleOutputHandle, pCharInfo, bufferSize, bufferCoord, ref readRegion); if (!r) __Error.WinIOError(); // Overwrite old section // I don't have a good function to blank out a rectangle. Win32Native.COORD writeCoord = new Win32Native.COORD(); writeCoord.X = (short) sourceLeft; Win32Native.Color c = ConsoleColorToColorAttribute(sourceBackColor, true); c |= ConsoleColorToColorAttribute(sourceForeColor, false); short attr = (short) c; int numWritten; for(int i = sourceTop; i<sourceTop + sourceHeight; i++) { writeCoord.Y = (short) i; r = Win32Native.FillConsoleOutputCharacter(ConsoleOutputHandle, sourceChar, sourceWidth, writeCoord, out numWritten); Contract.Assert(numWritten == sourceWidth, "FillConsoleOutputCharacter wrote the wrong number of chars!"); if (!r) __Error.WinIOError(); r = Win32Native.FillConsoleOutputAttribute(ConsoleOutputHandle, attr, sourceWidth, writeCoord, out numWritten); if (!r) __Error.WinIOError(); } // Write text to new location Win32Native.SMALL_RECT writeRegion = new Win32Native.SMALL_RECT(); writeRegion.Left = (short) targetLeft; writeRegion.Right = (short) (targetLeft + sourceWidth); writeRegion.Top = (short) targetTop; writeRegion.Bottom = (short) (targetTop + sourceHeight); fixed(Win32Native.CHAR_INFO* pCharInfo = data) r = Win32Native.WriteConsoleOutput(ConsoleOutputHandle, pCharInfo, bufferSize, bufferCoord, ref writeRegion); }
public static void SetBufferSize(int width, int height) { new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); // Ensure the new size is not smaller than the console window Win32Native.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); Win32Native.SMALL_RECT srWindow = csbi.srWindow; if (width < srWindow.Right + 1 || width >= Int16.MaxValue) throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferLessThanWindowSize")); if (height < srWindow.Bottom + 1 || height >= Int16.MaxValue) throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferLessThanWindowSize")); Win32Native.COORD size = new Win32Native.COORD(); size.X = (short) width; size.Y = (short) height; bool r = Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, size); if (!r) __Error.WinIOError(); }
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 >= Int16.MaxValue) throw new ArgumentOutOfRangeException("left", left, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); if (top < 0 || top >= Int16.MaxValue) throw new ArgumentOutOfRangeException("top", top, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); Contract.EndContractBlock(); new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); IntPtr hConsole = ConsoleOutputHandle; Win32Native.COORD coords = new Win32Native.COORD(); coords.X = (short) left; coords.Y = (short) top; bool r = Win32Native.SetConsoleCursorPosition(hConsole, coords); if (!r) { // Give a nice error message for out of range sizes int errorCode = Marshal.GetLastWin32Error(); Win32Native.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo(); if (left < 0 || left >= csbi.dwSize.X) throw new ArgumentOutOfRangeException("left", left, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); if (top < 0 || top >= csbi.dwSize.Y) throw new ArgumentOutOfRangeException("top", top, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); __Error.WinIOError(errorCode, String.Empty); } }
public static void Clear() { Win32Native.COORD coordScreen = new Win32Native.COORD(); Win32Native.CONSOLE_SCREEN_BUFFER_INFO csbi; bool success; int conSize; IntPtr hConsole = ConsoleOutputHandle; if (hConsole == Win32Native.INVALID_HANDLE_VALUE) throw new IOException(Environment.GetResourceString("IO.IO_NoConsole")); // get the number of character cells in the current buffer // Go through my helper method for fetching a screen buffer info // to correctly handle default console colors. csbi = GetBufferInfo(); conSize = csbi.dwSize.X * csbi.dwSize.Y; // fill the entire screen with blanks int numCellsWritten = 0; success = Win32Native.FillConsoleOutputCharacter(hConsole, ' ', conSize, coordScreen, out numCellsWritten); if (!success) __Error.WinIOError(); // now set the buffer's attributes accordingly numCellsWritten = 0; success = Win32Native.FillConsoleOutputAttribute(hConsole, csbi.wAttributes, conSize, coordScreen, out numCellsWritten); if (!success) __Error.WinIOError(); // put the cursor at (0, 0) success = Win32Native.SetConsoleCursorPosition(hConsole, coordScreen); if (!success) __Error.WinIOError(); }
public static unsafe void SetWindowSize(int width, int height) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum")); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum")); } new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(); bool flag2 = false; Win32Native.COORD size = new Win32Native.COORD { X = bufferInfo.dwSize.X, Y = bufferInfo.dwSize.Y }; if (bufferInfo.dwSize.X < (bufferInfo.srWindow.Left + width)) { if (bufferInfo.srWindow.Left >= (0x7fff - width)) { throw new ArgumentOutOfRangeException("width", Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowBufferSize")); } size.X = (short) (bufferInfo.srWindow.Left + width); flag2 = true; } if (bufferInfo.dwSize.Y < (bufferInfo.srWindow.Top + height)) { if (bufferInfo.srWindow.Top >= (0x7fff - height)) { throw new ArgumentOutOfRangeException("height", Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowBufferSize")); } size.Y = (short) (bufferInfo.srWindow.Top + height); flag2 = true; } if (flag2 && !Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, size)) { __Error.WinIOError(); } Win32Native.SMALL_RECT srWindow = bufferInfo.srWindow; srWindow.Bottom = (short) ((srWindow.Top + height) - 1); srWindow.Right = (short) ((srWindow.Left + width) - 1); if (!Win32Native.SetConsoleWindowInfo(ConsoleOutputHandle, true, &srWindow)) { int errorCode = Marshal.GetLastWin32Error(); if (flag2) { Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, bufferInfo.dwSize); } Win32Native.COORD largestConsoleWindowSize = Win32Native.GetLargestConsoleWindowSize(ConsoleOutputHandle); if (width > largestConsoleWindowSize.X) { throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowSize_Size", new object[] { largestConsoleWindowSize.X })); } if (height > largestConsoleWindowSize.Y) { throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_ConsoleWindowSize_Size", new object[] { largestConsoleWindowSize.Y })); } __Error.WinIOError(errorCode, string.Empty); } }
public static void SetCursorPosition(int left, int top) { if ((left < 0) || (left >= 0x7fff)) { throw new ArgumentOutOfRangeException("left", left, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((top < 0) || (top >= 0x7fff)) { throw new ArgumentOutOfRangeException("top", top, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); IntPtr consoleOutputHandle = ConsoleOutputHandle; Win32Native.COORD cursorPosition = new Win32Native.COORD { X = (short) left, Y = (short) top }; if (!Win32Native.SetConsoleCursorPosition(consoleOutputHandle, cursorPosition)) { int errorCode = Marshal.GetLastWin32Error(); Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(); if ((left < 0) || (left >= bufferInfo.dwSize.X)) { throw new ArgumentOutOfRangeException("left", left, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((top < 0) || (top >= bufferInfo.dwSize.Y)) { throw new ArgumentOutOfRangeException("top", top, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } __Error.WinIOError(errorCode, string.Empty); } }
public static void SetBufferSize(int width, int height) { new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); Win32Native.SMALL_RECT srWindow = GetBufferInfo().srWindow; if ((width < (srWindow.Right + 1)) || (width >= 0x7fff)) { throw new ArgumentOutOfRangeException("width", width, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferLessThanWindowSize")); } if ((height < (srWindow.Bottom + 1)) || (height >= 0x7fff)) { throw new ArgumentOutOfRangeException("height", height, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferLessThanWindowSize")); } Win32Native.COORD size = new Win32Native.COORD { X = (short) width, Y = (short) height }; if (!Win32Native.SetConsoleScreenBufferSize(ConsoleOutputHandle, size)) { __Error.WinIOError(); } }
public static unsafe void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop, char sourceChar, ConsoleColor sourceForeColor, ConsoleColor sourceBackColor) { if ((sourceForeColor < ConsoleColor.Black) || (sourceForeColor > ConsoleColor.White)) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidConsoleColor"), "sourceForeColor"); } if ((sourceBackColor < ConsoleColor.Black) || (sourceBackColor > ConsoleColor.White)) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidConsoleColor"), "sourceBackColor"); } Win32Native.COORD dwSize = GetBufferInfo().dwSize; if ((sourceLeft < 0) || (sourceLeft > dwSize.X)) { throw new ArgumentOutOfRangeException("sourceLeft", sourceLeft, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((sourceTop < 0) || (sourceTop > dwSize.Y)) { throw new ArgumentOutOfRangeException("sourceTop", sourceTop, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((sourceWidth < 0) || (sourceWidth > (dwSize.X - sourceLeft))) { throw new ArgumentOutOfRangeException("sourceWidth", sourceWidth, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((sourceHeight < 0) || (sourceTop > (dwSize.Y - sourceHeight))) { throw new ArgumentOutOfRangeException("sourceHeight", sourceHeight, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((targetLeft < 0) || (targetLeft > dwSize.X)) { throw new ArgumentOutOfRangeException("targetLeft", targetLeft, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((targetTop < 0) || (targetTop > dwSize.Y)) { throw new ArgumentOutOfRangeException("targetTop", targetTop, Environment.GetResourceString("ArgumentOutOfRange_ConsoleBufferBoundaries")); } if ((sourceWidth != 0) && (sourceHeight != 0)) { bool flag; Win32Native.CHAR_INFO[] char_infoArray3; new UIPermission(UIPermissionWindow.SafeTopLevelWindows).Demand(); Win32Native.CHAR_INFO[] char_infoArray = new Win32Native.CHAR_INFO[sourceWidth * sourceHeight]; dwSize.X = (short) sourceWidth; dwSize.Y = (short) sourceHeight; Win32Native.COORD bufferCoord = new Win32Native.COORD(); Win32Native.SMALL_RECT readRegion = new Win32Native.SMALL_RECT { Left = (short) sourceLeft, Right = (short) ((sourceLeft + sourceWidth) - 1), Top = (short) sourceTop, Bottom = (short) ((sourceTop + sourceHeight) - 1) }; fixed (Win32Native.CHAR_INFO* char_infoRef = char_infoArray) { flag = Win32Native.ReadConsoleOutput(ConsoleOutputHandle, char_infoRef, dwSize, bufferCoord, ref readRegion); } if (!flag) { __Error.WinIOError(); } Win32Native.COORD dwWriteCoord = new Win32Native.COORD { X = (short) sourceLeft }; Win32Native.Color color = (Win32Native.Color) ((short) (ConsoleColorToColorAttribute(sourceBackColor, true) | ConsoleColorToColorAttribute(sourceForeColor, false))); short wColorAttribute = (short) color; for (int i = sourceTop; i < (sourceTop + sourceHeight); i++) { int num2; dwWriteCoord.Y = (short) i; if (!Win32Native.FillConsoleOutputCharacter(ConsoleOutputHandle, sourceChar, sourceWidth, dwWriteCoord, out num2)) { __Error.WinIOError(); } if (!Win32Native.FillConsoleOutputAttribute(ConsoleOutputHandle, wColorAttribute, sourceWidth, dwWriteCoord, out num2)) { __Error.WinIOError(); } } Win32Native.SMALL_RECT writeRegion = new Win32Native.SMALL_RECT { Left = (short) targetLeft, Right = (short) (targetLeft + sourceWidth), Top = (short) targetTop, Bottom = (short) (targetTop + sourceHeight) }; if (((char_infoArray3 = char_infoArray) == null) || (char_infoArray3.Length == 0)) { char_infoRef2 = null; goto Label_02C4; } fixed (Win32Native.CHAR_INFO* char_infoRef2 = char_infoArray3) { Label_02C4: flag = Win32Native.WriteConsoleOutput(ConsoleOutputHandle, char_infoRef2, dwSize, bufferCoord, ref writeRegion); } } }
public static void Clear() { Win32Native.COORD dwWriteCoord = new Win32Native.COORD(); IntPtr consoleOutputHandle = ConsoleOutputHandle; if (consoleOutputHandle == Win32Native.INVALID_HANDLE_VALUE) { throw new IOException(Environment.GetResourceString("IO.IO_NoConsole")); } Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(); int nLength = bufferInfo.dwSize.X * bufferInfo.dwSize.Y; int pNumCharsWritten = 0; if (!Win32Native.FillConsoleOutputCharacter(consoleOutputHandle, ' ', nLength, dwWriteCoord, out pNumCharsWritten)) { __Error.WinIOError(); } pNumCharsWritten = 0; if (!Win32Native.FillConsoleOutputAttribute(consoleOutputHandle, bufferInfo.wAttributes, nLength, dwWriteCoord, out pNumCharsWritten)) { __Error.WinIOError(); } if (!Win32Native.SetConsoleCursorPosition(consoleOutputHandle, dwWriteCoord)) { __Error.WinIOError(); } }