public CONSOLE_SCREEN_BUFFER_INFOEX GetConsoleScreenBufferInfo(ConsoleOutputHandle consoleOutputHandle) { CONSOLE_SCREEN_BUFFER_INFOEX info = new CONSOLE_SCREEN_BUFFER_INFOEX { Size = Marshal.SizeOf <CONSOLE_SCREEN_BUFFER_INFOEX>() }; if (!NativeMethods.GetConsoleScreenBufferInfoEx(consoleOutputHandle, ref info)) { throw Exceptions.Win32(); } return(info); }
/// <summary> /// Writes the current value of the <see cref="Colors"/> collection to the system console properties. /// </summary> public void UpdateSystemColors() { var h = GetStdHandle(STD_HANDLE.STD_OUTPUT_HANDLE); var lpInfo = new CONSOLE_SCREEN_BUFFER_INFOEX(); lpInfo.cbSize = (uint)Marshal.SizeOf <CONSOLE_SCREEN_BUFFER_INFOEX>(); var handle = new SafeFileHandle(h, false); GetConsoleScreenBufferInfoEx(handle, ref lpInfo); foreach (var color in Colors) { lpInfo.ColorTable[color.Index] = (uint)color.Color.ToArgb(); } SetConsoleScreenBufferInfoEx(handle, lpInfo); }
public (bool visible, int size, Point position) GetCursorInfo(ConsoleOutputHandle consoleOutputHandle) { if (!NativeMethods.GetConsoleCursorInfo(consoleOutputHandle, out var info)) { throw Exceptions.Win32(); } var infoEx = new CONSOLE_SCREEN_BUFFER_INFOEX { Size = Marshal.SizeOf <CONSOLE_SCREEN_BUFFER_INFOEX>() }; if (!NativeMethods.GetConsoleScreenBufferInfoEx(consoleOutputHandle, ref infoEx)) { throw Exceptions.Win32(); } return(info.Visible, info.Size, new Point(infoEx.CursorPosition.X, infoEx.CursorPosition.Y)); }
/// <summary> /// Sets extended information about the specified console screen buffer. /// </summary> /// <param name="hConsoleOutput">A handle to the console screen buffer. The handle must have the <see cref="BUFFER_ACCESS_MODE.GENERIC_READ"/> access right.</param> /// <param name="lpConsoleScreenBufferInfoEx">A <see cref="CONSOLE_SCREEN_BUFFER_INFOEX"/> structure that contains the console screen buffer information.</param> /// <returns>If the function succeeds, returns TRUE, otherwise, retun FALSE.</returns> [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetConsoleScreenBufferInfoEx([In] IntPtr hConsoleOutput, [In] CONSOLE_SCREEN_BUFFER_INFOEX lpConsoleScreenBufferInfoEx);
public static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFOEX info);
/// <summary> /// Refresh the color values from the system. /// </summary> public void RefreshColors() { var h = GetStdHandle(STD_HANDLE.STD_OUTPUT_HANDLE); var lpInfo = new CONSOLE_SCREEN_BUFFER_INFOEX(); lpInfo.cbSize = (uint)Marshal.SizeOf <CONSOLE_SCREEN_BUFFER_INFOEX>(); var handle = new SafeFileHandle(h, false); GetConsoleScreenBufferInfoEx(handle, ref lpInfo); Colors = new List <ColorTableColor>(); // // Summary: // The color black. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[0], 0, "Black")); // // Summary: // The color dark blue. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[1], 1, "DarkBlue")); // // Summary: // The color dark green. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[2], 2, "DarkGreen")); // // Summary: // The color dark cyan (dark blue-green). Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[3], 3, "DarkCyan")); // // Summary: // The color dark red. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[4], 4, "DarkRed")); // // Summary: // The color dark magenta (dark purplish-red). Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[5], 5, "DarkMagenta")); // // Summary: // The color dark yellow (ochre). Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[6], 6, "DarkYellow")); // // Summary: // The color gray. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[7], 7, "Gray")); // // Summary: // The color dark gray. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[8], 8, "DarkGray")); // // Summary: // The color blue. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[9], 9, "Blue")); // // Summary: // The color green. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[10], 10, "Green")); // // Summary: // The color cyan (blue-green). Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[11], 11, "Cyan")); // // Summary: // The color red. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[12], 12, "Red")); // // Summary: // The color magenta (purplish-red). Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[13], 13, "Magenta")); // // Summary: // The color yellow. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[14], 14, "Yellow")); // // Summary: // The color white. Colors.Add(new ColorTableColor((int)lpInfo.ColorTable[15], 15, "White")); }
internal static extern bool GetConsoleScreenBufferInfoEx(ConsoleOutputHandle consoleOutputHandle, ref CONSOLE_SCREEN_BUFFER_INFOEX info);
/// <summary> /// Sets console window attributes more conducive to an output status window. /// </summary> /// <param name="title">Console window title</param> /// <param name="backgroundColor">Color of window background</param> /// <param name="textColor">Color of console text</param> /// <returns>false if there is NO associated console window</returns> /// <remarks> /// This consists of not only setting the caller-specified title and window colors, but also /// (1)setting the font to a smaller size (still very readable). /// (2)Enabling select text and copy to clipboard. /// (3)Setting the buffer to a very large size (256x9999) to support a lot of scrolling status text. /// (4)Setting the window size to a slightly larger size (reasonable with the smaller font) to make /// the scrolling status text visible longer before it scrolls off the window. /// </remarks> public static bool SetStdStatusProperties(string title, Color backgroundColor, Color textColor) { IntPtr hConsoleOutput = GetStdHandle(STD_HANDLE.OUTPUT); IntPtr hConsoleInput = GetStdHandle(STD_HANDLE.INPUT); if (hConsoleInput.ToInt32() <= 0) { return(false); //No console window! } bool ok = false; //---------------------- //Get Console Attributes //---------------------- ConsoleInputMode conInMode; ok = GetConsoleMode(hConsoleInput, out conInMode); //We don't need to set anything because users are not writing to the console window //ConsoleOutputMode conOutMode; //ok = GetConsoleMode(hOutput, out conOutMode); CONSOLE_FONT_INFOEX cfi = new CONSOLE_FONT_INFOEX(); cfi.cbSize = Marshal.SizeOf(typeof(CONSOLE_FONT_INFOEX)); ok = GetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi); CONSOLE_SCREEN_BUFFER_INFOEX csb = new CONSOLE_SCREEN_BUFFER_INFOEX(); csb.cbSize = Marshal.SizeOf(typeof(CONSOLE_SCREEN_BUFFER_INFOEX)); ok = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csb); //--------------------------------------------- //Tweak the console attributes and update them. //--------------------------------------------- //Add ability to click and select text to copy to the clipboard. conInMode |= ConsoleInputMode.ENABLE_QUICK_EDIT_MODE | ConsoleInputMode.ENABLE_EXTENDED_FLAGS; ok = SetConsoleMode(hConsoleInput, conInMode); //Use small but very readable font to optimize desktop real estate. cfi.FontSize_X = 6; cfi.FontSize_Y = 8; ok = SetCurrentConsoleFontEx(hConsoleOutput, false, ref cfi); //Set console text and background colors. csb.Gray = textColor; csb.Black = backgroundColor; ok = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csb); //There are only 16 color values (e.g. Console only supports 4-bit color). //We set the color indices to match the colors we had set above. System.Console.BackgroundColor = ConsoleColor.Black; System.Console.ForegroundColor = ConsoleColor.Gray; System.Console.Title = title; System.Console.BufferWidth = 256; //Make console buffer huge to hold a lot of text before rolling over. System.Console.BufferHeight = 9999; System.Console.WindowWidth = 80; //Nice console window size. User will have to manually System.Console.WindowHeight = 50; //resize or scroll to see additional status text. System.Console.Clear(); //clears the text buffer but also sets the foreground/background colors. return(true); }
[DllImport("Kernel32.dll")] private static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFOEX csbe);