public static void Clear() { int hWrittenChars = 0; CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); COORD Home; Home.x = Home.y = 0; GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo); FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars); SetConsoleCursorPosition(hConsoleHandle, Home); }
public ColorConsole() { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if(IsHandleValid()) { CONSOLE_SCREEN_BUFFER_INFO csbiInfo = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(hStdout, ref csbiInfo); attributes = csbiInfo.wAttributes; } }
static ConsoleEx() { // Grab input and output buffer handles hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); hConsoleInput = GetStdHandle(STD_INPUT_HANDLE); if (hConsoleOutput == INVALID_HANDLE_VALUE || hConsoleInput == INVALID_HANDLE_VALUE) throw new ApplicationException("Unable to obtain buffer handle during initialization."); // Get information about the console window characteristics. ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); //ConsoleOutputLocation = new COORD(); GetConsoleScreenBufferInfo(hConsoleOutput, ref ConsoleInfo); OriginalConsolePen = ConsoleInfo.wAttributes; // Disable wrapping at the end of a line (ENABLE_WRAP_AT_EOL_INPUT); this enables rectangles // to be drawn that fill the screen without the window scrolling. SetConsoleMode(hConsoleOutput, (int) OutputModeFlags.ENABLE_PROCESSED_OUTPUT); }
private CONSOLE_SCREEN_BUFFER_INFO get_console_buffer() { var defaultConsoleBuffer = new CONSOLE_SCREEN_BUFFER_INFO { dwSize = new COORD(), dwCursorPosition = new COORD(), dwMaximumWindowSize = new COORD(), srWindow = new SMALL_RECT(), wAttributes = 0, }; if (!is_windows()) return defaultConsoleBuffer; CONSOLE_SCREEN_BUFFER_INFO csbi; if (GetConsoleScreenBufferInfo(GetStdHandle(StdHandle.StdOut), out csbi)) { // if the console buffer exists return csbi; } return defaultConsoleBuffer; }
internal static extern bool GetConsoleScreenBufferInfo( IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo );
internal static bool GetConsoleScreenBufferInfo(IntPtr consoleHandle, out ConsoleControl.CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo) { ConsoleControl.CONSOLE_SCREEN_BUFFER_INFO c = new CONSOLE_SCREEN_BUFFER_INFO(); c.BufferSize.X = (short)Console.BufferWidth; c.BufferSize.Y = (short)Console.BufferHeight; c.CursorPosition.X = (short)Console.CursorLeft; c.CursorPosition.Y = (short)Console.CursorTop; c.MaxWindowSize.X = (short)Console.LargestWindowWidth; c.MaxWindowSize.Y = (short)Console.LargestWindowHeight; consoleScreenBufferInfo = c; return true; }
/// <summary> /// Moves the console cursor to the specified location on the screen. /// </summary> /// <param name="x">X co-ordinate for desired location (typically 0 to 79)</param> /// <param name="y">Y co-ordinate for desired location (typically 0 to 24)</param> public static void Move(int x, int y) { // Check that parameters specified are sane CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(hConsoleOutput, ref strConsoleInfo); if (x >= strConsoleInfo.dwSize.x || x < 0) throw new ArgumentOutOfRangeException("x", x, "The co-ordinates specified must be within the dimensions of the window."); if (y >= strConsoleInfo.dwSize.y || y < 0) throw new ArgumentOutOfRangeException("y", y, "The co-ordinates specified must be within the dimensions of the window."); COORD cursorLocation; cursorLocation.x = (short)x; cursorLocation.y = (short)y; SetConsoleCursorPosition(hConsoleOutput, cursorLocation); }
/// <summary> /// Clears screen. /// </summary> public static void Clear() { int hWrittenChars = 0; CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); COORD Home; Home.x = Home.y = 0; if (GetConsoleScreenBufferInfo(hConsoleOutput, ref strConsoleInfo) == 0) { // If the device does not support GetConsoleScreenBufferInfo, then try just // writing ^L (ASCII control code for Form Feed) to the screen. This may // work for some scenarios such as using telnet to access a remote console. Console.Write('\x0c'); // ^L return; } FillConsoleOutputCharacter(hConsoleOutput, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars); FillConsoleOutputAttribute(hConsoleOutput, CurrentConsolePen, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars); SetConsoleCursorPosition(hConsoleOutput, Home); }
private static extern bool GetConsoleScreenBufferInfo(IntPtr hConsole, out CONSOLE_SCREEN_BUFFER_INFO info);
public static extern bool GetConsoleScreenBufferInfo( IntPtr consoleHandle, out CONSOLE_SCREEN_BUFFER_INFO bufferInfo);
private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
public void Refresh() { short w = BufferSize; short h = BufferSize; short x = 0; short y = 0; if (!_resizeFlag) { x = BufferSize - 1; y = x; short x2 = 0; short y2 = 0; for (short ix = 0; ix < BufferSize; ix++) { for (short iy = 0; iy < BufferSize; iy++) { var ofs = iy * BufferSize + ix; var xor = (_buffer[ofs].Attributes ^ _buffer2[ofs].Attributes) | (_buffer[ofs].Char ^ _buffer2[ofs].Char); if (xor != 0) { x = Math.Min(x, ix); y = Math.Min(y, iy); x2 = Math.Max(x2, ix); y2 = Math.Max(y2, iy); } } } if (x > x2) { x = (short)(x2 + 1); } if (y > y2) { y = (short)(y2 + 1); } w = (short)(x2 - x + 1); h = (short)(y2 - y + 1); } var info = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(_stdout, ref info); var rect = new SMALL_RECT { Left = (short)(info.Window.Left + x), Top = (short)(info.Window.Top + y), Right = (short)(info.Window.Left + x + w - 1), Bottom = (short)(info.Window.Top + y + h - 1) }; Memset(new IntPtr(_buffer2), 0, BufferSize * BufferSize); WriteConsoleOutputW(_stdout, new IntPtr(_buffer), new COORD { X = BufferSize, Y = BufferSize }, new COORD { X = x, Y = y }, ref rect); if (!_resizeFlag) { var tbuf = _buffer2; _buffer2 = _buffer; _buffer = tbuf; } _resizeFlag = false; }
public WindowsNativeConsoleProvider() { _threadToken = new CancellationTokenSource(); _buffer = (CHAR_INFO *)Marshal.AllocHGlobal(BufferSize * BufferSize * CHAR_INFO.SizeOf).ToPointer(); _buffer2 = (CHAR_INFO *)Marshal.AllocHGlobal(BufferSize * BufferSize * CHAR_INFO.SizeOf).ToPointer(); _stdin = GetStdHandle(-10); _stdout = GetStdHandle(-11); { var info = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(_stdout, ref info); WindowWidth = info.Window.Right - info.Window.Left + 1; WindowHeight = info.Window.Bottom - info.Window.Top + 1; _prevBuf = info.Size; SetConsoleScreenBufferSize(_stdout, new COORD { X = (short)WindowWidth, Y = (short)WindowHeight }); } new Thread(() => // window size monitor { while (!_threadToken.IsCancellationRequested) { var info = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(_stdout, ref info); int nw = info.Window.Right - info.Window.Left + 1, nh = info.Window.Bottom - info.Window.Top + 1; if (nw < 0) { nw = WindowWidth; } if (nh < 0) { nh = WindowHeight; // fukken winapi bugs } if (nw == WindowWidth && nh == WindowHeight) { continue; } var pw = WindowWidth; var ph = WindowHeight; _resizeFlag = true; WindowWidth = nw; WindowHeight = nh; SetConsoleScreenBufferSize(_stdout, new COORD { X = (short)WindowWidth, Y = (short)WindowHeight }); try { SizeChanged?.Invoke(this, new SizeChangedEventArgs(new Size(pw, ph), new Size(nw, nh))); } catch { /* Do not care if subscriber f****d up */ } //Refresh(); Thread.Sleep(16); } }) { Priority = ThreadPriority.Lowest }.Start(); _keyboardThread = new Thread(() => // keyboard monitor { INPUT_RECORD *charBuf = stackalloc INPUT_RECORD[1]; var ptr = new IntPtr(charBuf); var sizeOf = Marshal.SizeOf(typeof(INPUT_RECORD)); while (!_threadToken.IsCancellationRequested) { uint nchars = 0; Memset(ptr, 0, sizeOf); ReadConsoleInputW(_stdin, ptr, 1, ref nchars); if (charBuf->EventType == 1 && nchars == 1 && charBuf->KeyEvent.KeyDown) { KeyPressed?.Invoke( this, new KeyPressedEventArgs(new ConsoleKeyInfo( charBuf->KeyEvent.Char, (ConsoleKey)charBuf->KeyEvent.KeyCode, (charBuf->KeyEvent.ControlKeyState & 0x10) > 0, (charBuf->KeyEvent.ControlKeyState & 0x03) > 0, (charBuf->KeyEvent.ControlKeyState & 0x0c) > 0 ))); } } }); _keyboardThread.Start(); }
/// <summary> /// Returns the number of columns in the current console window /// </summary> /// <returns>Returns the number of columns in the current console window</returns> public static int GetConsoleWindowWidth() { int screenWidth; CONSOLE_SCREEN_BUFFER_INFO csbi = new CONSOLE_SCREEN_BUFFER_INFO(); // Just to remove the warning messages... csbi.dwCursorPosition.x = 0; csbi.dwCursorPosition.y = 0; csbi.srWindow.Bottom = 0; csbi.srWindow.Top = 0; csbi.srWindow.Left = 0; csbi.srWindow.Right = 0; int rc; rc = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), ref csbi); screenWidth = csbi.dwSize.x; return screenWidth; }
internal static extern bool GetConsoleScreenBufferInfo(NakedWin32Handle consoleHandle, out CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo);
/// <summary> /// Returns the number of columns in the current console window /// </summary> /// <returns>Returns the number of columns in the current console window</returns> public static int GetConsoleWindowWidth() { int screenWidth; CONSOLE_SCREEN_BUFFER_INFO csbi = new CONSOLE_SCREEN_BUFFER_INFO(); // If the function succeeds, the return value is nonzero. int rc = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), ref csbi); if (rc == 0){ throw new ApplicationException ("Unable to obtain console width"); } screenWidth = csbi.dwSize.x; return screenWidth; }
/// <summary> /// Adds forground color intensity /// </summary> internal static void AddIntensity() { CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(hConsoleOut, ref ConsoleInfo); SetConsoleTextAttribute(hConsoleOut, (ushort)(ConsoleInfo.wAttributes | ForeIntensity)); }
private static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
internal static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
/// <summary> /// Returns the number of columns in the current console window /// </summary> /// <returns>Returns the number of columns in the current console window</returns> public static int GetConsoleWindowWidth() { int screenWidth; CONSOLE_SCREEN_BUFFER_INFO csbi = new CONSOLE_SCREEN_BUFFER_INFO(); int rc; rc = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), ref csbi); screenWidth = csbi.dwSize.x; return screenWidth; }
public static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
private static extern bool GetConsoleScreenBufferInfo( IntPtr consoleHandle, out CONSOLE_SCREEN_BUFFER_INFO bufferInfo);
/// <summary> /// Decreases forground color intensity /// </summary> internal static void RemoveIntensity() { CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(hConsoleOut, ref ConsoleInfo); SetConsoleTextAttribute(hConsoleOut, (ushort)(ConsoleInfo.wAttributes & (~FR_INTENSITY))); }
// Constructor. public ColorConsole() { ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); ConsoleOutputLocation = new COORD(); hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsoleHandle, ref ConsoleInfo); OriginalColors = ConsoleInfo.wAttributes; }