public static char[,] ReadConsoleRaw() { NativeInterface.CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = ConsoleInterface.getScreenBufferInfo(); NativeInterface.SMALL_RECT readRegion = default(NativeInterface.SMALL_RECT); readRegion.Right = screenBufferInfo.dwSize.X; readRegion.Bottom = (short)(screenBufferInfo.dwCursorPosition.Y + 1); // read all lines from the top of the screen buffer to the current cursor position (inclusive) NativeInterface.CHAR_INFO[] charInfos = new NativeInterface.CHAR_INFO[screenBufferInfo.dwSize.X * screenBufferInfo.dwSize.Y]; using (SafeHandle hConsoleOutput = ConsoleInterface.getConsoleOutputHandle()) { ConsoleInterface.callWin32Func(() => NativeInterface.ReadConsoleOutput(hConsoleOutput, charInfos, screenBufferInfo.dwSize, default(NativeInterface.COORD), ref readRegion)); } char[,] output = new char[screenBufferInfo.dwSize.X, readRegion.Bottom]; for (int y = 0; y < readRegion.Bottom; y++) { for (int x = 0; x < screenBufferInfo.dwSize.X; x++) { output[x, y] = charInfos[y * screenBufferInfo.dwSize.X + x].UnicodeChar; } } return(output); }
public static void ClearConsole() { using (SafeHandle hConsoleOutput = ConsoleInterface.getConsoleOutputHandle()) { // get dimensions of the screen buffer NativeInterface.CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = ConsoleInterface.getScreenBufferInfo(); int charsToWrite = screenBufferInfo.dwSize.X * screenBufferInfo.dwSize.Y; int charsWritten; ConsoleInterface.callWin32Func(() => NativeInterface.FillConsoleOutputCharacter(hConsoleOutput, ConsoleInterface.SPACE_CHAR, charsToWrite, default(NativeInterface.COORD), out charsWritten)); ConsoleInterface.callWin32Func(() => NativeInterface.SetConsoleCursorPosition(hConsoleOutput, default(NativeInterface.COORD))); } }