Example #1
0
 /// <summary>
 /// Sets the current console buffer's info
 /// </summary>
 public static void SetConsoleInfo(ref ConsoleScreenBufferInfoEx info)
 {
     if (!NativeMethods.SetConsoleScreenBufferInfoEx(CurrentOutputBuffer, ref info))
     {
         throw new Win32Exception(Marshal.GetLastWin32Error());
     }
 }
Example #2
0
        //Set every console color to it's default
        static ConsoleColors()
        {
            var csbe = new ConsoleScreenBufferInfoEx();

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

            GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            csbe.ColorTable = new[]
            {
                new Colorref(0, 0, 0),
                new Colorref(0, 0, 128),
                new Colorref(0, 128, 0),
                new Colorref(0, 128, 128),
                new Colorref(128, 0, 0),
                new Colorref(128, 0, 128),
                new Colorref(128, 128, 0),
                new Colorref(192, 192, 192),
                new Colorref(128, 128, 128),
                new Colorref(0, 0, 256),
                new Colorref(0, 256, 0),
                new Colorref(0, 256, 256),
                new Colorref(256, 0, 0),
                new Colorref(256, 0, 256),
                new Colorref(256, 256, 0),
                new Colorref(256, 256, 256)
            };
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;
            SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
            Console.Clear();
        }
Example #3
0
        public static int SetColor(int color, uint r, uint g, uint b)
        {
            var csbe = new ConsoleScreenBufferInfoEx();

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

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

            if (!brc)
            {
                return(Marshal.GetLastWin32Error());
            }
            csbe.ColorTable[color] = new Colorref(r, g, b);
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;
            if (!SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe))
            {
                return(Marshal.GetLastWin32Error());
            }
            return(0);
        }
Example #4
0
        public Coord GetCursorPosition()
        {
            ConsoleScreenBufferInfoEx consoleInfo = ConsoleScreenBufferInfoEx.Create();

            GetConsoleScreenBufferInfoEx(output.screenBuffer, ref consoleInfo);

            return(consoleInfo.cursorPosition);
        }
Example #5
0
        public static void SetConsoleScreenBuffer(ConsoleScreenBufferInfoEx screenBufferInfo)
        {
            bool brc = SetConsoleScreenBufferInfoEx(ConsoleOutputHandle, ref screenBufferInfo);

            if (!brc)
            {
                throw new System.Exception("SetConsoleScreenBufferInfoEx->WinError: #" + Marshal.GetLastWin32Error());
            }
        }
Example #6
0
        internal static ConsoleScreenBufferInfoEx GetConsoleScreenBuffer()
        {
            ConsoleScreenBufferInfoEx csbe = new ConsoleScreenBufferInfoEx();

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

            bool brc = GetConsoleScreenBufferInfoEx(ConsoleOutputHandle, ref csbe);

            if (!brc)
            {
                throw new System.Exception("GetConsoleScreenBufferInfoEx->WinError: #" + Marshal.GetLastWin32Error());
            }
            return(csbe);
        }
Example #7
0
        public static ConsoleScreenBufferInfoEx GetConsoleScreenBuffer()
        {
            ConsoleScreenBufferInfoEx csbe = new ConsoleScreenBufferInfoEx();

            csbe.cbSize = Marshal.SizeOf(csbe); // 96 = 0x60
            bool brc = GetConsoleScreenBufferInfoEx(ConsoleOutputHandle, ref csbe);

            if (!brc)
            {
                throw new System.Exception("GetConsoleScreenBufferInfoEx->WinError: #" + Marshal.GetLastWin32Error());
            }
            // work around a weird bug in windows ...
            // https://stackoverflow.com/questions/25274019/strange-setconsolescreenbufferinfoex-behavior
            ++csbe.srWindow.Bottom;
            ++csbe.srWindow.Right;
            return(csbe);
        }
Example #8
0
        public Output()
        {
            screenBuffer = CreateConsoleScreenBuffer(0xC0000000, 0x00000003, IntPtr.Zero, 1, IntPtr.Zero);
            SetConsoleScreenBufferSize(screenBuffer, new Coord(161, 60));
            SetConsoleActiveScreenBuffer(screenBuffer);

            Rect windowSize = new Rect(0, 0, 160, 59);

            SetConsoleWindowInfo(screenBuffer, true, ref windowSize);

            consoleInfo = ConsoleScreenBufferInfoEx.Create();
            GetConsoleScreenBufferInfoEx(screenBuffer, ref consoleInfo);
            SetConsoleTitle("Basic Commander");

            // Draw borders
            DrawLine(80, 0, 80, 57);
            DrawLine(0, 1, 161, 1);
            DrawLine(0, 57, 161, 57);
        }
Example #9
0
        public ConsoleEngine(int w, int h, uint fontSize, string title) : base(w, h, title)
        {
            Console.CursorVisible = false;
            Console.Title         = title;
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w + 1, h);

            //DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MINIMIZE, MF_BYCOMMAND);
            DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MAXIMIZE, MF_BYCOMMAND);
            DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_SIZE, MF_BYCOMMAND);

            IntPtr stdHandle = GetStdHandle(-11);
            ConsoleScreenBufferInfoEx bufferInfo = new ConsoleScreenBufferInfoEx();

            bufferInfo.cbSize = (uint)Marshal.SizeOf(bufferInfo);
            GetConsoleScreenBufferInfoEx(stdHandle, ref bufferInfo);
            ++bufferInfo.srWindow.Right;
            ++bufferInfo.srWindow.Bottom;
            SetConsoleScreenBufferInfoEx(stdHandle, ref bufferInfo);

            SetFont((short)fontSize);

            AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => { SetFont(16); };
        }
Example #10
0
 internal static extern bool GetConsoleScreenBufferInfoEx(
     SafeFileHandle hConsoleOutput,
     ref ConsoleScreenBufferInfoEx consoleScreenBufferInfo
     );
Example #11
0
 private static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref ConsoleScreenBufferInfoEx csbe);
Example #12
0
 private static extern bool GetConsoleScreenBufferInfoEx(
     IntPtr hConsoleOutput,
     ref ConsoleScreenBufferInfoEx csbe);
Example #13
0
 internal static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref ConsoleScreenBufferInfoEx csbe);
Example #14
0
        /// <summary>
        /// Initializes static members of the <see cref="ConsoleHelper"/> class.
        /// </summary>
        static ConsoleHelper()
        {
            if (Environment.UserInteractive)
            {
                try
                {
                    // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                    // - If we arent a console, CursorLeft will throw, otherwise we want IsConsole to be set to true.
                    IsConsole = Console.CursorLeft >= int.MinValue;
                }
                catch (IOException)
                {
                    // Try to attach to parent process's console window
                    IsConsole = AttachConsole(0xFFFFFFFF);
                }
            }
            else
            {
                IsConsole = false;
            }

            if (IsConsole)
            {
                try
                {
                    ConsoleScreenBufferInfoEx csbe = new ConsoleScreenBufferInfoEx();
                    csbe.BufferSize = Marshal.SizeOf(csbe);                // 96 = 0x60
                    IntPtr hConsoleOutput = GetStdHandle(StdOutputHandle); // 7
                    if (hConsoleOutput == _invalidHandleValue)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                    bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
                    if (!brc)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    _consoleColors = new Dictionary <ConsoleColor, Color>
                    {
                        { ConsoleColor.Black, csbe.Black.GetColor() },
                        { ConsoleColor.DarkBlue, csbe.DarkBlue.GetColor() },
                        { ConsoleColor.DarkGreen, csbe.DarkGreen.GetColor() },
                        { ConsoleColor.DarkCyan, csbe.DarkCyan.GetColor() },
                        { ConsoleColor.DarkRed, csbe.DarkRed.GetColor() },
                        { ConsoleColor.DarkMagenta, csbe.DarkMagenta.GetColor() },
                        { ConsoleColor.DarkYellow, csbe.DarkYellow.GetColor() },
                        { ConsoleColor.Gray, csbe.Gray.GetColor() },
                        { ConsoleColor.DarkGray, csbe.DarkGray.GetColor() },
                        { ConsoleColor.Blue, csbe.Blue.GetColor() },
                        { ConsoleColor.Green, csbe.Green.GetColor() },
                        { ConsoleColor.Cyan, csbe.Cyan.GetColor() },
                        { ConsoleColor.Red, csbe.Red.GetColor() },
                        { ConsoleColor.Magenta, csbe.Magenta.GetColor() },
                        { ConsoleColor.Yellow, csbe.Yellow.GetColor() },
                        { ConsoleColor.White, csbe.White.GetColor() }
                    };
                }
                catch
                {
                    _consoleColors = _defaultConsoleColors;
                }
            }
            else
            {
                _consoleColors = _defaultConsoleColors;
            }

            // Create reverse lookup
            _consoleColorsReverse     = _consoleColors.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
            _consoleColorReverseCache = new ConcurrentDictionary <Color, ConsoleColor>(_consoleColorsReverse);
        }
Example #15
0
        /// <summary>
        /// Initializes static members of the <see cref="ConsoleHelper"/> class.
        /// </summary>
        static ConsoleHelper()
        {
            if (Environment.UserInteractive)
                try
                {
                    // ReSharper disable once ConditionIsAlwaysTrueOrFalse 
                    // - If we arent a console, CursorLeft will throw, otherwise we want IsConsole to be set to true.
                    IsConsole = Console.CursorLeft >= int.MinValue;
                }
                catch (IOException)
                {
                    // Try to attach to parent process's console window
                    IsConsole = AttachConsole(0xFFFFFFFF);
                }
            else
                IsConsole = false;

            if (IsConsole)
                try
                {
                    ConsoleScreenBufferInfoEx csbe = new ConsoleScreenBufferInfoEx();
                    csbe.BufferSize = Marshal.SizeOf(csbe); // 96 = 0x60
                    IntPtr hConsoleOutput = GetStdHandle(StdOutputHandle); // 7
                    if (hConsoleOutput == _invalidHandleValue)
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
                    if (!brc)
                        throw new Win32Exception(Marshal.GetLastWin32Error());

                    _consoleColors = new Dictionary<ConsoleColor, Color>
                    {
                        { ConsoleColor.Black, csbe.Black.GetColor() },
                        { ConsoleColor.DarkBlue, csbe.DarkBlue.GetColor() },
                        { ConsoleColor.DarkGreen, csbe.DarkGreen.GetColor() },
                        { ConsoleColor.DarkCyan, csbe.DarkCyan.GetColor() },
                        { ConsoleColor.DarkRed, csbe.DarkRed.GetColor() },
                        { ConsoleColor.DarkMagenta, csbe.DarkMagenta.GetColor() },
                        { ConsoleColor.DarkYellow, csbe.DarkYellow.GetColor() },
                        { ConsoleColor.Gray, csbe.Gray.GetColor() },
                        { ConsoleColor.DarkGray, csbe.DarkGray.GetColor() },
                        { ConsoleColor.Blue, csbe.Blue.GetColor() },
                        { ConsoleColor.Green, csbe.Green.GetColor() },
                        { ConsoleColor.Cyan, csbe.Cyan.GetColor() },
                        { ConsoleColor.Red, csbe.Red.GetColor() },
                        { ConsoleColor.Magenta, csbe.Magenta.GetColor() },
                        { ConsoleColor.Yellow, csbe.Yellow.GetColor() },
                        { ConsoleColor.White, csbe.White.GetColor() }
                    };
                }
                catch
                {
                    _consoleColors = _defaultConsoleColors;
                }
            else
                _consoleColors = _defaultConsoleColors;

            // Create reverse lookup
            _consoleColorsReverse = _consoleColors.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
            _consoleColorReverseCache = new ConcurrentDictionary<Color, ConsoleColor>(_consoleColorsReverse);
        }
Example #16
0
 public static extern bool SetConsoleScreenBufferInfoEx(
     IntPtr ConsoleOutput,
     ConsoleScreenBufferInfoEx ConsoleScreenBufferInfoEx
     );
Example #17
0
 public static extern bool GetConsoleScreenBufferInfoEx(
     IntPtr hConsoleOutput,
     ref ConsoleScreenBufferInfoEx ConsoleScreenBufferInfo
     );
Example #18
0
		internal static extern bool SetConsoleScreenBufferInfoEx(
			IntPtr consoleOutput,
			ConsoleScreenBufferInfoEx consoleScreenBufferInfoEx);
Example #19
0
		internal static extern bool GetConsoleScreenBufferInfoEx(
			IntPtr hConsoleOutput,
			ref ConsoleScreenBufferInfoEx consoleScreenBufferInfo);