public static void TrySetConsoleColor(ConsoleColorFlags colorFlags) { // SetConsoleTextAttribute may fail if stdout is redirected to a sink other // than the cmd window (eg: VS's output window). try { SetConsoleColor(colorFlags); } catch (Win32Exception) { } }
// // Interface public static void SetConsoleColor(ConsoleColorFlags colorFlags) { // Set the color on the current stdout stream. const int stdout = -11; IntPtr hStdOut = Kernel32.GetStdHandle(stdout); if (hStdOut == IntPtr.Zero || hStdOut == new IntPtr(-1)) throw new Win32Exception(); if (!Kernel32.SetConsoleTextAttribute(hStdOut, colorFlags)) throw new Win32Exception(); }
public static void TrySetConsoleColor(ConsoleColorFlags colorFlags) { // don't do anything if 'black on black' if ( 0 == (Int32)colorFlags ) { return; } // SetConsoleTextAttribute may fail if stdout is redirected to a sink other // than the cmd window (eg: VS's output window). try { SetConsoleColor(colorFlags); } catch (Win32Exception) { } }
public static void TrySetConsoleColor(ConsoleColorFlags colorFlags) { // don't do anything if 'black on black' if (0 == (Int32)colorFlags) { return; } // SetConsoleTextAttribute may fail if stdout is redirected to a sink other // than the cmd window (eg: VS's output window). try { SetConsoleColor(colorFlags); } catch (Win32Exception) { } }
// // Interface public static void SetConsoleColor(ConsoleColorFlags colorFlags) { // Set the color on the current stdout stream. const int stdout = -11; IntPtr hStdOut = Kernel32.GetStdHandle(stdout); if (hStdOut == IntPtr.Zero || hStdOut == new IntPtr(-1)) { throw new Win32Exception(); } if (!Kernel32.SetConsoleTextAttribute(hStdOut, colorFlags)) { throw new Win32Exception(); } }
public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, ConsoleColorFlags wAttributes);