Пример #1
0
        static bool SetProperties(ColorScheme colorScheme)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
            IntPtr hOut    = GetStdOutputHandle();
            bool   success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);

            if (success)
            {
                csbiex.srWindow.Bottom++;
                for (int i = 0; i < 16; i++)
                {
                    csbiex.ColorTable[i] = colorScheme.colorTable[i];
                }
                if (colorScheme.background != null && colorScheme.foreground != null)
                {
                    int fgidx = colorScheme.CalculateIndex(colorScheme.foreground.Value);
                    int bgidx = colorScheme.CalculateIndex(colorScheme.background.Value);
                    csbiex.wAttributes = (ushort)(fgidx | (bgidx << 4));
                }
                SetConsoleScreenBufferInfoEx(hOut, ref csbiex);
            }
            if (success)
            {
                if (!quietMode)
                {
                    PrintTable();
                }
            }
            return(success);
        }
        public void ApplyColorScheme(ColorScheme colorScheme, bool quietMode)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
            IntPtr hOut    = GetStdOutputHandle();
            bool   success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);

            if (!success)
            {
                throw new InvalidOperationException("Could not obtain console screen buffer");
            }

            csbiex.srWindow.Bottom++;
            for (int i = 0; i < 16; i++)
            {
                csbiex.ColorTable[i] = colorScheme.ColorTable[i];
            }
            if (colorScheme.ScreenColorAttributes is ushort wAttrs)
            {
                csbiex.wAttributes = wAttrs;
            }
            if (colorScheme.PopupColorAttributes is ushort wPopupAttrs)
            {
                csbiex.wPopupAttributes = wPopupAttrs;
            }
            SetConsoleScreenBufferInfoEx(hOut, ref csbiex);

            if (!quietMode)
            {
                ColorTable.PrintTable();
            }
        }
Пример #3
0
        /// <summary>
        /// Gets a collection of all 16 colors in the console buffer.
        /// </summary>
        /// <returns>Returns all 16 COLORREFs in the console buffer as a dictionary keyed by the COLORREF's alias
        /// in the buffer's ColorTable.</returns>
        public static Dictionary <ConsoleColor, COLORREF> GetBufferColors()
        {
            var    colors         = new Dictionary <ConsoleColor, COLORREF>();
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = GetBufferInfo(hConsoleOutput);

            colors.Add(ConsoleColor.Black, csbe.black);
            colors.Add(ConsoleColor.DarkBlue, csbe.darkBlue);
            colors.Add(ConsoleColor.DarkGreen, csbe.darkGreen);
            colors.Add(ConsoleColor.DarkCyan, csbe.darkCyan);
            colors.Add(ConsoleColor.DarkRed, csbe.darkRed);
            colors.Add(ConsoleColor.DarkMagenta, csbe.darkMagenta);
            colors.Add(ConsoleColor.DarkYellow, csbe.darkYellow);
            colors.Add(ConsoleColor.Gray, csbe.gray);
            colors.Add(ConsoleColor.DarkGray, csbe.darkGray);
            colors.Add(ConsoleColor.Blue, csbe.blue);
            colors.Add(ConsoleColor.Green, csbe.green);
            colors.Add(ConsoleColor.Cyan, csbe.cyan);
            colors.Add(ConsoleColor.Red, csbe.red);
            colors.Add(ConsoleColor.Magenta, csbe.magenta);
            colors.Add(ConsoleColor.Yellow, csbe.yellow);
            colors.Add(ConsoleColor.White, csbe.white);

            return(colors);
        }
Пример #4
0
        static bool ExportCurrentAsIni(string outputPath)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
            int    STD_OUTPUT_HANDLE             = -11;
            IntPtr hOut    = GetStdHandle(STD_OUTPUT_HANDLE);
            bool   success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);

            if (success)
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputPath))
                {
                    file.WriteLine("[table]");
                    for (int i = 0; i < 16; i++)
                    {
                        string line = IniSchemeParser.COLOR_NAMES[i];
                        line += " = ";
                        uint color = csbiex.ColorTable[i];
                        uint r     = color & (0x000000ff);
                        uint g     = (color & (0x0000ff00)) >> 8;
                        uint b     = (color & (0x00ff0000)) >> 16;
                        line += r + "," + g + "," + b;
                        file.WriteLine(line);
                    }
                }
            }
            else
            {
                Console.WriteLine("Failed to get console information.");
            }
            return(success);
        }
Пример #5
0
        /// <summary>
        /// Gets a collection of all 16 colors in the console buffer.
        /// </summary>
        /// <returns>Returns all 16 COLORREFs in the console buffer as a dictionary keyed by the COLORREF's alias
        /// in the buffer's ColorTable.</returns>
        public Dictionary <string, COLORREF> GetBufferColors()
        {
            Dictionary <string, COLORREF> colors = new Dictionary <string, COLORREF>();
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = GetBufferInfo(hConsoleOutput);

            colors.Add("black", csbe.black);
            colors.Add("darkBlue", csbe.darkBlue);
            colors.Add("darkGreen", csbe.darkGreen);
            colors.Add("darkCyan", csbe.darkCyan);
            colors.Add("darkRed", csbe.darkRed);
            colors.Add("darkMagenta", csbe.darkMagenta);
            colors.Add("darkYellow", csbe.darkYellow);
            colors.Add("gray", csbe.gray);
            colors.Add("darkGray", csbe.darkGray);
            colors.Add("blue", csbe.blue);
            colors.Add("green", csbe.green);
            colors.Add("cyan", csbe.cyan);
            colors.Add("red", csbe.red);
            colors.Add("magenta", csbe.magenta);
            colors.Add("yellow", csbe.yellow);
            colors.Add("white", csbe.white);

            return(colors);
        }
Пример #6
0
        static bool SetProperties(uint[] colorTable)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
            int    STD_OUTPUT_HANDLE             = -11;
            IntPtr hOut    = GetStdHandle(STD_OUTPUT_HANDLE);
            bool   success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);

            if (success)
            {
                csbiex.srWindow.Bottom++;
                for (int i = 0; i < 16; i++)
                {
                    csbiex.ColorTable[i] = colorTable[i];
                }
                SetConsoleScreenBufferInfoEx(hOut, ref csbiex);
            }
            if (success)
            {
                if (!quietMode)
                {
                    PrintTable();
                }
            }
            return(success);
        }
Пример #7
0
        public static void SetColor(uint r, uint g, uint b)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            csbe.cbSize = (int)Marshal.SizeOf(csbe);                    // 96 = 0x60
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7

            if (hConsoleOutput == INVALID_HANDLE_VALUE)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);

            if (!brc)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            csbe.blue = new COLORREF(r, g, b);
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;
            brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            if (!brc)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Пример #8
0
        private CONSOLE_SCREEN_BUFFER_INFO_EX GetBufferInfo(IntPtr hConsoleOutput)
        {
            var csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            csbe.cbSize = (int)Marshal.SizeOf(csbe); // 96 = 0x60

            if (hConsoleOutput == INVALID_HANDLE_VALUE)
            {
                throw CreateException(Marshal.GetLastWin32Error());
            }

            var brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);

            if (!brc)
            {
                throw CreateException(Marshal.GetLastWin32Error());
            }

            _currentColors     = (byte)(csbe.wAttributes & (ushort)InternalColor.ColorMask);
            _currentForeground = (byte)(csbe.wAttributes & (ushort)InternalColor.ForegroundMask);
            _currentBackground = (byte)(csbe.wAttributes & (ushort)InternalColor.BackgroundMask);

            if (!_haveReadDefaultColors)
            {
                _defaultColors         = (byte)(csbe.wAttributes & (ushort)InternalColor.ColorMask);
                _defaultForeground     = (byte)(csbe.wAttributes & (ushort)InternalColor.ForegroundMask);
                _defaultBackground     = (byte)(csbe.wAttributes & (ushort)InternalColor.BackgroundMask);
                _haveReadDefaultColors = true;
            }

            return(csbe);
        }
Пример #9
0
        public static CONSOLE_SCREEN_BUFFER_INFO_EX GetExtendedBufferInfo()
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX bufferInfo = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            bufferInfo.cbSize = (uint)Marshal.SizeOf(bufferInfo);
            HandleError(!WinApi.GetConsoleScreenBufferInfoEx(bufferHandle, ref bufferInfo));
            return(bufferInfo);
        }
Пример #10
0
 private static CONSOLE_SCREEN_BUFFER_INFO_EX GetConsoleScreenBufferInfoEx()
 {
     var hConsoleOutput = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);
     var info = new CONSOLE_SCREEN_BUFFER_INFO_EX();
     info.cbSize = (uint) Marshal.SizeOf(info);
     if (!NativeMethods.GetConsoleScreenBufferInfoEx(hConsoleOutput, ref info))
         throw new Win32Exception();
     return info;
 }
Пример #11
0
        static CONSOLE_SCREEN_BUFFER_INFO_EX GetScreenBufferInfoEx()
        {
            var result = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            result.cbSize = (int)Marshal.SizeOf(result);
            if (!GetConsoleScreenBufferInfoEx(GetOutputHandle(), ref result))
            {
                throw new Win32Exception();
            }
            return(result);
        }
Пример #12
0
        private void SetBufferInfo(IntPtr hConsoleOutput, CONSOLE_SCREEN_BUFFER_INFO_EX csbe)
        {
            csbe.srWindow.Bottom++;
            csbe.srWindow.Right++;

            bool brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);

            if (!brc)
            {
                throw CreateException(Marshal.GetLastWin32Error());
            }
        }
Пример #13
0
        private static CONSOLE_SCREEN_BUFFER_INFO_EX GetConsoleScreenBufferInfoEx()
        {
            var hConsoleOutput = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);
            var info           = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            info.cbSize = (uint)Marshal.SizeOf(info);
            if (!NativeMethods.GetConsoleScreenBufferInfoEx(hConsoleOutput, ref info))
            {
                throw new Win32Exception();
            }
            return(info);
        }
Пример #14
0
        static bool ExportCurrentAsIni(string outputPath)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create();
            IntPtr hOut    = GetStdOutputHandle();
            bool   success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);

            if (success)
            {
                try
                {
                    // StreamWriter can fail for a variety of file system reasons so catch exceptions and print message.
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputPath))
                    {
                        file.WriteLine("[table]");
                        for (int i = 0; i < 16; i++)
                        {
                            string line = IniSchemeParser.COLOR_NAMES[i];
                            line += " = ";
                            uint color = csbiex.ColorTable[i];
                            uint r     = color & (0x000000ff);
                            uint g     = (color & (0x0000ff00)) >> 8;
                            uint b     = (color & (0x00ff0000)) >> 16;
                            line += r + "," + g + "," + b;
                            file.WriteLine(line);
                        }

                        file.WriteLine();
                        file.WriteLine("[screen]");
                        var forgroundIndex  = csbiex.wAttributes & 0xF;
                        var backgroundIndex = csbiex.wAttributes >> 4;
                        file.WriteLine($"FOREGROUND = {IniSchemeParser.COLOR_NAMES[forgroundIndex]}");
                        file.WriteLine($"BACKGROUND = {IniSchemeParser.COLOR_NAMES[backgroundIndex]}");

                        file.WriteLine();
                        file.WriteLine("[popup]");
                        forgroundIndex  = csbiex.wPopupAttributes & 0xF;
                        backgroundIndex = csbiex.wPopupAttributes >> 4;
                        file.WriteLine($"FOREGROUND = {IniSchemeParser.COLOR_NAMES[forgroundIndex]}");
                        file.WriteLine($"BACKGROUND = {IniSchemeParser.COLOR_NAMES[backgroundIndex]}");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("Failed to get console information.");
            }
            return(success);
        }
        private CONSOLE_SCREEN_BUFFER_INFO_EX GetConsoleScreenBufferInfoEx()
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            csbe.cbSize = (int)Marshal.SizeOf(csbe); // 96 = 0x60

            bool brc = GetConsoleScreenBufferInfoEx(this._hConsoleOutput, ref csbe);

            if (!brc)
            {
                throw new SystemException("GetConsoleScreenBufferInfoEx->WinError: #" + Marshal.GetLastWin32Error());
            }
            return(csbe);
        }
        public static void AdjustColorPalette()
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX cinfo = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            cinfo.cbSize = (uint)Marshal.SizeOf(cinfo);
            IntPtr ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);

            GetConsoleScreenBufferInfoEx(ConsoleHandle, ref cinfo);

            cinfo.ColorTable[1] = new COLORREF(255, 128, 0);
            cinfo.ColorTable[4] = new COLORREF(198, 98, 251);
            cinfo.ColorTable[6] = new COLORREF(128, 64, 0);
            ++cinfo.srWindow.Bottom;
            ++cinfo.srWindow.Right;
            SetConsoleScreenBufferInfoEx(ConsoleHandle, ref cinfo);
        }
Пример #17
0
        private CONSOLE_SCREEN_BUFFER_INFO_EX GetBufferInfo(IntPtr hConsoleOutput)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            csbe.cbSize = (int)Marshal.SizeOf(csbe); // 96 = 0x60

            if (hConsoleOutput == INVALID_HANDLE_VALUE)
            {
                throw CreateException(Marshal.GetLastWin32Error());
            }

            bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);

            if (!brc)
            {
                throw CreateException(Marshal.GetLastWin32Error());
            }

            return(csbe);
        }
Пример #18
0
        /// <summary>
        /// Sets all 16 colors in the console buffer using colors supplied in a dictionary.
        /// </summary>
        /// <param name="colors">A dictionary containing COLORREFs keyed by the COLORREF's alias in the buffer's
        /// ColorTable.</param>
        public static void SetBatchBufferColors(Dictionary <ConsoleColor, COLORREF> colors)
        {
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); // 7
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = GetBufferInfo(hConsoleOutput);

            csbe.black       = colors[ConsoleColor.Black];
            csbe.darkBlue    = colors[ConsoleColor.DarkBlue];
            csbe.darkGreen   = colors[ConsoleColor.DarkGreen];
            csbe.darkCyan    = colors[ConsoleColor.DarkCyan];
            csbe.darkRed     = colors[ConsoleColor.DarkRed];
            csbe.darkMagenta = colors[ConsoleColor.DarkMagenta];
            csbe.darkYellow  = colors[ConsoleColor.DarkYellow];
            csbe.gray        = colors[ConsoleColor.Gray];
            csbe.darkGray    = colors[ConsoleColor.DarkGray];
            csbe.blue        = colors[ConsoleColor.Blue];
            csbe.green       = colors[ConsoleColor.Green];
            csbe.cyan        = colors[ConsoleColor.Cyan];
            csbe.red         = colors[ConsoleColor.Red];
            csbe.magenta     = colors[ConsoleColor.Magenta];
            csbe.yellow      = colors[ConsoleColor.Yellow];
            csbe.white       = colors[ConsoleColor.White];

            SetBufferInfo(hConsoleOutput, csbe);
        }
Пример #19
0
        /// <summary>
        /// Sets all 16 colors in the console buffer using colors supplied in a dictionary.
        /// </summary>
        /// <param name="colors">A dictionary containing COLORREFs keyed by the COLORREF's alias in the buffer's
        /// ColorTable.</param>
        public void SetBatchBufferColors(Dictionary <string, COLORREF> colors)
        {
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); // 7
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = GetBufferInfo(hConsoleOutput);

            csbe.black       = colors["black"];
            csbe.darkBlue    = colors["darkBlue"];
            csbe.darkGreen   = colors["darkGreen"];
            csbe.darkCyan    = colors["darkCyan"];
            csbe.darkRed     = colors["darkRed"];
            csbe.darkMagenta = colors["darkMagenta"];
            csbe.darkYellow  = colors["darkYellow"];
            csbe.gray        = colors["gray"];
            csbe.darkGray    = colors["darkGray"];
            csbe.blue        = colors["blue"];
            csbe.green       = colors["green"];
            csbe.cyan        = colors["cyan"];
            csbe.red         = colors["red"];
            csbe.magenta     = colors["magenta"];
            csbe.yellow      = colors["yellow"];
            csbe.white       = colors["white"];

            SetBufferInfo(hConsoleOutput, csbe);
        }
Пример #20
0
 internal void SetFieldValue(
     CONSOLE_SCREEN_BUFFER_INFO_EX @this,
     COLORREF value)
 {
     _fieldSetterFunc(@this, value);
 }
Пример #21
0
        public static int SetColor(ConsoleColor color, uint r, uint g, uint b)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            csbe.cbSize = (uint)(int)Marshal.SizeOf(csbe);              // 96 = 0x60
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7

            if (hConsoleOutput == INVALID_HANDLE_VALUE)
            {
                return(Marshal.GetLastWin32Error());
            }
            bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);

            if (!brc)
            {
                return(Marshal.GetLastWin32Error());
            }

            switch (color)
            {
            case ConsoleColor.Black:
                csbe.black = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkBlue:
                csbe.darkBlue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGreen:
                csbe.darkGreen = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkCyan:
                csbe.darkCyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkRed:
                csbe.darkRed = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkMagenta:
                csbe.darkMagenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkYellow:
                csbe.darkYellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Gray:
                csbe.gray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGray:
                csbe.darkGray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Blue:
                csbe.blue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Green:
                csbe.green = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Cyan:
                csbe.cyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Red:
                csbe.red = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Magenta:
                csbe.magenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Yellow:
                csbe.yellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.White:
                csbe.white = new COLORREF(r, g, b);
                break;
            }
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;
            brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            if (!brc)
            {
                return(Marshal.GetLastWin32Error());
            }
            return(0);
        }
Пример #22
0
        public static int SetColor(ConsoleColor color, uint r, uint g, uint b)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX currentScreen = new CONSOLE_SCREEN_BUFFER_INFO_EX();
            currentScreen.cbSize = (int)Marshal.SizeOf(currentScreen);                    // 96 = 0x60
            IntPtr handle = GetStdHandle(STD_OUTPUT_HANDLE);    // 7
            if (handle == INVALID_HANDLE_VALUE)
            {
                return Marshal.GetLastWin32Error();
            }
            bool brc = GetConsoleScreenBufferInfoEx(handle, ref currentScreen);
            if (!brc)
            {
                return Marshal.GetLastWin32Error();
            }

            //var a = currentScreen.GetType().GetRuntimeFields()
            //    .Single(x => { return x.Name.ToLower() == color.ToString().ToLower(); });
            // a.SetValue(currentScreen, new COLORREF(r, g, b));

            switch (color)
            {
                case ConsoleColor.Black:
                    currentScreen.black = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.DarkBlue:
                    currentScreen.darkBlue = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.DarkGreen:
                    currentScreen.darkGreen = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.DarkCyan:
                    currentScreen.darkCyan = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.DarkRed:
                    currentScreen.darkRed = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.DarkMagenta:
                    currentScreen.darkMagenta = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.DarkYellow:
                    currentScreen.darkYellow = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.Gray:
                    currentScreen.gray = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.DarkGray:
                    currentScreen.darkGray = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.Blue:
                    currentScreen.blue = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.Green:
                    currentScreen.green = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.Cyan:
                    currentScreen.cyan = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.Red:
                    currentScreen.red = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.Magenta:
                    currentScreen.magenta = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.Yellow:
                    currentScreen.yellow = new COLORREF(r, g, b);
                    break;

                case ConsoleColor.White:
                    currentScreen.white = new COLORREF(r, g, b);
                    break;
            }

            ++currentScreen.srWindow.Bottom;
            ++currentScreen.srWindow.Right;
            brc = SetConsoleScreenBufferInfoEx(handle, ref currentScreen);
            if (!brc)
            {
                return Marshal.GetLastWin32Error();
            }
            return 0;
        }
        private static void SetNewColorDefinition(ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe, ConsoleColor color, Color rgbColor)
        {
            var r = rgbColor.R;
            var g = rgbColor.G;
            var b = rgbColor.B;

            switch (color)
            {
            case ConsoleColor.Black:
                csbe.black = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkBlue:
                csbe.darkBlue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGreen:
                csbe.darkGreen = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkCyan:
                csbe.darkCyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkRed:
                csbe.darkRed = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkMagenta:
                csbe.darkMagenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkYellow:
                csbe.darkYellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Gray:
                csbe.gray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGray:
                csbe.darkGray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Blue:
                csbe.blue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Green:
                csbe.green = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Cyan:
                csbe.cyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Red:
                csbe.red = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Magenta:
                csbe.magenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Yellow:
                csbe.yellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.White:
                csbe.white = new COLORREF(r, g, b);
                break;
            }
        }
Пример #24
0
 [DllImport("kernel32.dll", SetLastError = true)] static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfo);
Пример #25
0
 internal static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput,
                                                          ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);
Пример #26
0
 public static extern bool SetConsoleScreenBufferInfoEx(
     IntPtr ConsoleOutput,
     CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfoEx
     );
 private static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbie);
Пример #28
0
 public static void SetExtendedBufferInfo(CONSOLE_SCREEN_BUFFER_INFO_EX bufferInfo)
 {
     HandleError(!WinApi.SetConsoleScreenBufferInfoEx(bufferHandle, ref bufferInfo));
 }
Пример #29
0
 internal COLORREF GetFieldValue(
     CONSOLE_SCREEN_BUFFER_INFO_EX @this)
 {
     return(_fieldGetterFunc(@this));
 }
Пример #30
0
        public static int SetColor(ConsoleColor color, uint r, uint g, uint b)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX currentScreen = new CONSOLE_SCREEN_BUFFER_INFO_EX();

            currentScreen.cbSize = (int)Marshal.SizeOf(currentScreen); // 96 = 0x60
            IntPtr handle = GetStdHandle(STD_OUTPUT_HANDLE);           // 7

            if (handle == INVALID_HANDLE_VALUE)
            {
                return(Marshal.GetLastWin32Error());
            }
            bool brc = GetConsoleScreenBufferInfoEx(handle, ref currentScreen);

            if (!brc)
            {
                return(Marshal.GetLastWin32Error());
            }

            //var a = currentScreen.GetType().GetRuntimeFields()
            //    .Single(x => { return x.Name.ToLower() == color.ToString().ToLower(); });
            // a.SetValue(currentScreen, new COLORREF(r, g, b));

            switch (color)
            {
            case ConsoleColor.Black:
                currentScreen.black = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkBlue:
                currentScreen.darkBlue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGreen:
                currentScreen.darkGreen = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkCyan:
                currentScreen.darkCyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkRed:
                currentScreen.darkRed = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkMagenta:
                currentScreen.darkMagenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkYellow:
                currentScreen.darkYellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Gray:
                currentScreen.gray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGray:
                currentScreen.darkGray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Blue:
                currentScreen.blue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Green:
                currentScreen.green = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Cyan:
                currentScreen.cyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Red:
                currentScreen.red = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Magenta:
                currentScreen.magenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Yellow:
                currentScreen.yellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.White:
                currentScreen.white = new COLORREF(r, g, b);
                break;
            }

            ++currentScreen.srWindow.Bottom;
            ++currentScreen.srWindow.Right;
            brc = SetConsoleScreenBufferInfoEx(handle, ref currentScreen);
            if (!brc)
            {
                return(Marshal.GetLastWin32Error());
            }
            return(0);
        }
Пример #31
0
        private void MapColor(ConsoleColor color, uint r, uint g, uint b)
        {
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();
            csbe.cbSize = (int)Marshal.SizeOf(csbe);                    // 96 = 0x60

            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7
            if (hConsoleOutput == INVALID_HANDLE_VALUE)
            {
                throw new ColorMappingException(Marshal.GetLastWin32Error());
            }

            bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            if (!brc)
            {
                throw new ColorMappingException(Marshal.GetLastWin32Error());
            }

            switch (color)
            {
                case ConsoleColor.Black:
                    csbe.black = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkBlue:
                    csbe.darkBlue = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkGreen:
                    csbe.darkGreen = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkCyan:
                    csbe.darkCyan = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkRed:
                    csbe.darkRed = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkMagenta:
                    csbe.darkMagenta = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkYellow:
                    csbe.darkYellow = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Gray:
                    csbe.gray = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkGray:
                    csbe.darkGray = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Blue:
                    csbe.blue = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Green:
                    csbe.green = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Cyan:
                    csbe.cyan = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Red:
                    csbe.red = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Magenta:
                    csbe.magenta = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Yellow:
                    csbe.yellow = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.White:
                    csbe.white = new COLORREF(r, g, b);
                    break;
            }

            csbe.srWindow.Bottom++;
            csbe.srWindow.Right++;

            brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            if (!brc)
            {
                throw new ColorMappingException(Marshal.GetLastWin32Error());
            }
        }
Пример #32
0
        private void MapColor(ConsoleColor color, uint r, uint g, uint b)
        {
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); // 7
            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = GetBufferInfo(hConsoleOutput);

            switch (color)
            {
            case ConsoleColor.Black:
                csbe.black = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkBlue:
                csbe.darkBlue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGreen:
                csbe.darkGreen = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkCyan:
                csbe.darkCyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkRed:
                csbe.darkRed = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkMagenta:
                csbe.darkMagenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkYellow:
                csbe.darkYellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Gray:
                csbe.gray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.DarkGray:
                csbe.darkGray = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Blue:
                csbe.blue = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Green:
                csbe.green = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Cyan:
                csbe.cyan = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Red:
                csbe.red = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Magenta:
                csbe.magenta = new COLORREF(r, g, b);
                break;

            case ConsoleColor.Yellow:
                csbe.yellow = new COLORREF(r, g, b);
                break;

            case ConsoleColor.White:
                csbe.white = new COLORREF(r, g, b);
                break;
            }

            SetBufferInfo(hConsoleOutput, csbe);
        }
Пример #33
0
 private static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);
Пример #34
0
        const int STD_OUTPUT_HANDLE = -11; // per WinBase.h

        #endregion Fields

        #region Methods

        public static int SetColor(int index, Color colour)
        {
            byte r = colour.R;
            byte g = colour.G;
            byte b = colour.B;

            CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();
            csbe.cbSize = (int)Marshal.SizeOf(csbe);                    // 96 = 0x60
            IntPtr hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    // 7
            if (hConsoleOutput == INVALID_HANDLE_VALUE)
            {
                return Marshal.GetLastWin32Error();
            }
            bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            if (!brc)
            {
                return Marshal.GetLastWin32Error();
            }

            switch ((ConsoleColor)index)
            {
                case ConsoleColor.Black:
                    csbe.black = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkBlue:
                    csbe.darkBlue = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkGreen:
                    csbe.darkGreen = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkCyan:
                    csbe.darkCyan = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkRed:
                    csbe.darkRed = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkMagenta:
                    csbe.darkMagenta = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkYellow:
                    csbe.darkYellow = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Gray:
                    csbe.gray = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.DarkGray:
                    csbe.darkGray = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Blue:
                    csbe.blue = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Green:
                    csbe.green = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Cyan:
                    csbe.cyan = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Red:
                    csbe.red = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Magenta:
                    csbe.magenta = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.Yellow:
                    csbe.yellow = new COLORREF(r, g, b);
                    break;
                case ConsoleColor.White:
                    csbe.white = new COLORREF(r, g, b);
                    break;
            }
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;

            brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            if (!brc)
            {
                return Marshal.GetLastWin32Error();
            }

            return 0;
        }
Пример #35
0
 public static extern bool SetConsoleScreenBufferInfoEx(IntPtr ConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfoEx);
Пример #36
0
 public static extern bool GetConsoleScreenBufferInfoEx(
     SafeFileHandle hConsoleOutput,
     ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfo
     );