Mit dieser Klasse kann man die Farbe der Konsole auf jede beliebige RGB-Farbe ändern. Durch den Aufruf von der statischen Methode SetColor weist man einem Enumwert eine neue Farbe hinzu.
Wenn man jetzt folgenden Code aufruft, wird man feststellen, dass der Text in Rot geschrieben wurde und nicht wie angegeben in Blau, da man den Wert für Blue mit dem neuen (einem Rotton) überschrieben hat.
ConsoleColorChanger.SetColor(ConsoleColor.Blue, 255, 0, 0);
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Hallo Welt");
Benötigte Namespaces:
using System;
using System.Runtime.InteropServices;
public static class ConsoleColorChanger
{
static 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);
public static int SetColor(System.ConsoleColor color, byte r, byte g, byte b)
{
CONSOLE_SCREEN_BUFFER_INFO_EX csbe = new CONSOLE_SCREEN_BUFFER_INFO_EX();
csbe.cbSize = (uint)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.ColorTable[(int)color] = new COLORREF(r, g, b);
++csbe.srWindow.Bottom;
++csbe.srWindow.Right;
brc = SetConsoleScreenBufferInfoEx(hConsoleOutput, ref csbe);
if (!brc)
{
return Marshal.GetLastWin32Error();
}
return 0;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
}
public struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
public struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}
[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;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public COLORREF[] ColorTable;
public static CONSOLE_SCREEN_BUFFER_INFO_EX Create()
{
return new CONSOLE_SCREEN_BUFFER_INFO_EX { cbSize = 96 };
}
}
[StructLayout(LayoutKind.Sequential)]
public struct COLORREF
{
public uint ColorDWORD;
public COLORREF(byte r, byte g, byte b)
{
ColorDWORD = (uint)r + (((uint)g) << 8) + (((uint)b) << 16);
}
}
Kommentare zum Snippet