Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than kernel32, prefix the name with the module name and a period.
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;
internal COLORREF black;
internal COLORREF darkBlue;
internal COLORREF darkGreen;
internal COLORREF darkCyan;
internal COLORREF darkRed;
internal COLORREF darkMagenta;
internal COLORREF darkYellow;
internal COLORREF gray;
internal COLORREF darkGray;
internal COLORREF blue;
internal COLORREF green;
internal COLORREF cyan;
internal COLORREF red;
internal COLORREF magenta;
internal COLORREF yellow;
internal COLORREF white;
}
// 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;
Alternative Managed API:
Do you know one? Please contribute it!
public COLORREF color8;
public COLORREF color9;
public COLORREF colorA;
public COLORREF colorB;
Notes:
// Untested, added during 1-pass copy & paste of windows console fuctions
public COLORREF colorC;
public COLORREF colorD;
public COLORREF colorE;
public COLORREF colorF;
}
Tips & Tricks:
Please add some!
Sample Code:
// Copyright Alex Shvedov
// Modified by MercuryP with color specifications
// Use this code in any way you want
Alternative Managed API:
Do you know one? Please contribute it!
using System;
using System.Diagnostics; // for Debug
using System.Drawing; // for Color (add reference to System.Drawing assembly)
using System.Runtime.InteropServices; // for StructLayout
Notes:
// Untested, added during 1-pass copy & paste of windows console fuctions
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;
/*** 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;
} */
// Set a specific console color to an RGB color
// The default console colors used are gray (foreground) and black (background)
public static int SetColor(ConsoleColor consoleColor, Color targetColor)
{
return SetColor(consoleColor, targetColor.R, targetColor.G, targetColor.B);
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 SetColor(ConsoleColor color, uint r, uint g, uint b)
{
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)
{
if(hConsoleOutput == INVALID_HANDLE_VALUE) {
return Marshal.GetLastWin32Error();
}
bool brc = GetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
if (!brc)
{
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;
}
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;
}
public static int SetScreenColors(Color foregroundColor, Color backgroundColor)
{
int irc;
irc = SetColor(ConsoleColor.Gray, foregroundColor);
if (irc != 0) return irc;
irc = SetColor(ConsoleColor.Black, backgroundColor);
if (irc != 0) return irc;
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();
// Note: If you use SetScreenColors, the RGB values of gray and black are changed permanently for the console window.
// Using i.e. Console.ForegroundColor = ConsoleColor.Gray afterwards will switch the color to whatever you changed gray to
// It's best to use SetColor for the purpose of choosing the 16 colors you want the console to be able to display, then use
// Console.BackgroundColor and Console.ForegrondColor to choose among them.
}
}
}
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).