Beispiel #1
0
        /// <summary>
        /// Initializes new instance of ConsoleColorsHelper class
        /// </summary>
        public ConsoleManager()
        {
            // TODO: second instance created is crashing. Find out why and how to fix it / prevent. In the worst case - hidden control instance singleton
            this._hConsoleOutput = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE); // 7
            if (this._hConsoleOutput == NativeMethods.INVALID_HANDLE)
            {
                throw new SystemException("GetStdHandle->WinError: #" + Marshal.GetLastWin32Error());
            }

            this._closeColorFinder = new CloseColorFinder(this.GetCurrentColorset());
        }
Beispiel #2
0
        /// <summary>
        /// Replaces default (or previous...) single value of console color with new RGB value.
        /// </summary>
        /// <param name="color">Console named color</param>
        /// <param name="rgbColor">New RGB value to be used under this color name</param>
        public void ReplaceConsoleColor(ConsoleColor color, Color rgbColor)
        {
            var csbe = this.GetConsoleScreenBufferInfoEx();

            SetNewColorDefinition(ref csbe, color, rgbColor);

            // strange, needs to be done because window is shrunken somehow
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;

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

            if (!brc)
            {
                throw new SystemException("SetConsoleScreenBufferInfoEx->WinError: #" + Marshal.GetLastWin32Error());
            }

            this._closeColorFinder = new CloseColorFinder(this.GetCurrentColorset());
        }
Beispiel #3
0
        /// <summary>
        /// Replaces default (or previous...) values of console colors with new RGB values.
        /// </summary>
        /// <param name="mappings"></param>
        public void ReplaceConsoleColors(params Tuple <ConsoleColor, Color>[] mappings)
        {
            var csbe = this.GetConsoleScreenBufferInfoEx();

            foreach (var mapping in mappings)
            {
                SetNewColorDefinition(ref csbe, mapping.Item1, mapping.Item2);
            }

            // strange, needs to be done because window is shrunken somehow
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;

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

            if (!brc)
            {
                throw new SystemException("SetConsoleScreenBufferInfoEx->WinError: #" + Marshal.GetLastWin32Error());
            }

            this._closeColorFinder = new CloseColorFinder(this.GetCurrentColorset());
        }