console colors in .Net 3.5 (C#)


SUBMITTED BY: Guest

DATE: Nov. 21, 2013, 5:51 a.m.

FORMAT: Text only

SIZE: 1.4 kB

HITS: 787

  1. public enum ConsoleColor
  2. {
  3. black = 0,
  4. darkblue = 1,
  5. darkgreen = 2,
  6. darkaqua = 3,
  7. darkred = 4,
  8. purple = 5,
  9. brown = 6,
  10. grey = 7,
  11. darkgrey = 8,
  12. blue = 9,
  13. green = 10,
  14. aqua = 11,
  15. red = 12,
  16. pink = 13,
  17. yellow = 14,
  18. white = 15,
  19. }
  20. [ DllImport("kernel32.dll" )]
  21. private static extern long SetConsoleTextAttribute(int hConsoleOutput, int wScreenColors);
  22. [ DllImport("kernel32.dll" )]
  23. private static extern int GetStdHandle(long nStdHandle);
  24. private const int STD_OUTPUT_HANDLE = -11;
  25. public static void SetConsoleColors( ConsoleColor forecolor, ConsoleColor backcolor)
  26. {
  27. int hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  28. backcolor = ( ConsoleColor)(((int )backcolor == 0) ? 256 : ((int)backcolor * 16));
  29. SetConsoleTextAttribute(hConsole, (( int)forecolor | (int )backcolor));
  30. }
  31. public static void SetConsoleColors( ConsoleColor forecolor)
  32. {
  33. int hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  34. SetConsoleTextAttribute(hConsole, ( int)forecolor);
  35. }

comments powered by Disqus