SetConsoleScreenBufferInfoEx (kernel32)
Last changed: MercuryP-75.183.164.142

.
Summary
TODO - a short description

C# Signature:

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool SetConsoleScreenBufferInfoEx(
        IntPtr ConsoleOutput,
        CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfoEx
        );

VB Signature:

Declare Function SetConsoleScreenBufferInfoEx Lib "kernel32.dll" (TODO) As TODO

User-Defined Types:

    [StructLayout(LayoutKind.Sequential)]
    public struct CONSOLE_SCREEN_BUFFER_INFO_EX
    {
        public uint cbSize;
        public COORD dwSize;
        public COORD dwCursorPosition;
        public short wAttributes;
        public SMALL_RECT srWindow;
        public COORD dwMaximumWindowSize;

        public ushort wPopupAttributes;
        public bool bFullscreenSupported;

        // Hack Hack Hack
        // Too lazy to figure out the array at the moment...
        //public COLORREF[16] ColorTable;
        public COLORREF color0;
        public COLORREF color1;
        public COLORREF color2;
        public COLORREF color3;

        public COLORREF color4;
        public COLORREF color5;
        public COLORREF color6;
        public COLORREF color7;

        public COLORREF color8;
        public COLORREF color9;
        public COLORREF colorA;
        public COLORREF colorB;

        public COLORREF colorC;
        public COLORREF colorD;
        public COLORREF colorE;
        public COLORREF colorF;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

// Untested, added during 1-pass copy & paste of windows console fuctions

Tips & Tricks:

Please add some!

Sample Code:

// Alex Shvedov

// Use this code in any way you want

using System;

using System.Diagnostics; // for Debug

using System.Drawing; // for Color (add reference to System.Drawing assembly)

using System.Runtime.InteropServices; // for StructLayout

internal class SetScreenColorsDemo {

    [StructLayout(LayoutKind.Sequential)]
    internal struct COORD {
        internal short X;
        internal short Y;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct SMALL_RECT {
        internal short Left;
        internal short Top;
        internal short Right;
        internal short Bottom;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct COLORREF {
        internal uint ColorDWORD;

        internal COLORREF(Color color) {
            ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
        }

        internal Color GetColor() {
            return Color.FromArgb((int)(0x000000FFU & ColorDWORD),
               (int)(0x0000FF00U & ColorDWORD) >> 8, (int)(0x00FF0000U & ColorDWORD) >> 16);
        }

        internal void SetColor(Color color) {
            ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct CONSOLE_SCREEN_BUFFER_INFO_EX {
        internal int cbSize;
        internal COORD dwSize;
        internal COORD dwCursorPosition;
        internal ushort wAttributes;
        internal SMALL_RECT srWindow;
        internal COORD dwMaximumWindowSize;
        internal ushort wPopupAttributes;
        internal bool bFullscreenSupported;
        internal COLORREF screenBackgroundColor;
        internal COLORREF color1;
        internal COLORREF color2;
        internal COLORREF color3;
        internal COLORREF color4;
        internal COLORREF color5;
        internal COLORREF color6;
        internal COLORREF screenTextColor;
        internal COLORREF color8;
        internal COLORREF color9;
        internal COLORREF colorA;
        internal COLORREF colorB;
        internal COLORREF colorC;
        internal COLORREF colorD;
        internal COLORREF colorE;
        internal COLORREF colorF;
    }

    const int STD_OUTPUT_HANDLE = -11;                                        // per WinBase.h
    internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);    // per WinBase.h

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX csbe);

    /***            C prototype:
    #include <Windows.h>
    #include <stdio.h>
    //#include <conio.h>        // for _getch()

    int wmain() {
        CONSOLE_SCREEN_BUFFER_INFOEX info;
        info.cbSize = sizeof(info);
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfoEx(hConsole, &info);
        // info.ColorTable[0] - "Screen Background" color
        // info.ColorTable[7] - "Screen Text" color
        info.ColorTable[0] = RGB(0, 0, 200);
        info.ColorTable[7] = RGB(200, 200, 0);
        ++info.srWindow.Bottom;                    // reasons for these is unknown
        ++info.srWindow.Right;
        SetConsoleScreenBufferInfoEx(hConsole, &info);
        puts("Press ENTER to exit...");
        char bbb[100];
        gets_s(bbb);
        return 0;
    }
*/

    public static int SetScreenColors(Color screenTextColor, Color screenBackgroundColor) {
        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();
        }
        csbe.screenTextColor.SetColor(screenTextColor);
        csbe.screenBackgroundColor.SetColor(screenBackgroundColor);
        ++csbe.srWindow.Bottom;
        ++csbe.srWindow.Right;
        brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
        if(!brc) {
            return Marshal.GetLastWin32Error();
        }
        return 0;
    }

    private static void Main(string[] args) {
        Color screenTextColor = Color.Red;
        Color screenBackgroundColor = Color.Blue;
        int irc = SetScreenColors(screenTextColor, screenBackgroundColor);
        Debug.Assert(irc == 0, "SetScreenColors failed, Win32Error code = " + irc + " = 0x" + irc.ToString("x"));

        Debug.WriteLine("LargestWindowHeight=" + Console.LargestWindowHeight + " LargestWindowWidth=" + Console.LargestWindowWidth);
        Debug.WriteLine("BufferHeight=" + Console.BufferHeight + " WindowHeight=" + Console.WindowHeight + " BufferWidth=" + Console.BufferWidth + " WindowWidth=" + Console.WindowWidth);
        //// these are relative to the buffer, not the screen:
        //Debug.WriteLine("WindowTop=" + Console.WindowTop + " WindowLeft=" + Console.WindowLeft);
        Debug.WriteLine("ForegroundColor=" + Console.ForegroundColor + " BackgroundColor=" + Console.BackgroundColor);
        Console.WriteLine("Some text in a console window");
        Console.BackgroundColor = ConsoleColor.Cyan;
        Console.ForegroundColor = ConsoleColor.Yellow;
        Debug.WriteLine("ForegroundColor=" + Console.ForegroundColor + " BackgroundColor=" + Console.BackgroundColor);
        Console.Write("Press ENTER to exit...");
        Console.ReadLine();
    }

}

Documentation