Advertisement
dmilicev

console_window_colors_v2.c

Feb 10th, 2020
279
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None
  1. /*
  2.  
  3.     console_window_colors_v2.c
  4.  
  5.  code of each color
  6.  
  7. Name         | Value
  8.              |
  9. Black        |   0
  10. Blue         |   1
  11. Green        |   2
  12. Cyan         |   3
  13. Red          |   4
  14. Magenta      |   5
  15. Brown        |   6
  16. Light Gray   |   7
  17. Dark Gray    |   8
  18. Light Blue   |   9
  19. Light Green  |   10
  20. Light Cyan   |   11
  21. Light Red    |   12
  22. Light Magenta|   13
  23. Yellow       |   14
  24. White        |   15
  25.  
  26.  
  27.     You can find all my C programs at Dragan Milicev's pastebin:
  28.  
  29.     https://pastebin.com/u/dmilicev
  30.  
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <windows.h>
  35.  
  36. void ClearConsoleToColors(int ForgC, int BackC)
  37. {
  38.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  39.      ///Get the handle to the current output buffer...
  40.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  41.      ///This is used to reset the carat/cursor to the top left.
  42.      COORD coord = {0, 0};
  43.      ///A return value... indicating how many chars were written
  44.      ///   not used but we need to capture this since it will be
  45.      ///   written anyway (passing NULL causes an access violation).
  46.      DWORD count;
  47.      ///This is a structure containing all of the console info
  48.      /// it is used here to find the size of the console.
  49.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  50.      ///Here we will set the current color
  51.      SetConsoleTextAttribute(hStdOut, wColor);
  52.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  53.      {
  54.           ///This fills the buffer with a given character (in this case 32=space).
  55.           FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  56.           FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
  57.           ///This will set our cursor position for the next print statement.
  58.           SetConsoleCursorPosition(hStdOut, coord);
  59.      }
  60.      return;
  61. } //ClearConsoleToColors()
  62.  
  63. void SetColorAndBackground(int ForgC, int BackC)
  64. {
  65.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
  66.      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
  67.      return;
  68. } //SetColorAndBackground()
  69.  
  70.  
  71. int main(void)
  72. {
  73.     int fColor, bColor;
  74.     char str[100];
  75.  
  76.     //ClearConsoleToColors(15,15);
  77.  
  78.     for( bColor=0; bColor<=15; bColor++)
  79.     {
  80.         SetColorAndBackground(0,bColor);
  81.         printf("B%-2d: ", bColor);
  82.  
  83.         for( fColor=0; fColor<=15; fColor++)
  84.         {
  85.             SetColorAndBackground(fColor,bColor);
  86.             printf(" F%-2d", fColor);
  87.             //sprintf(str, "\n This is a test. (f,b) = (%2d,%2d) ", fColor, bColor);
  88.             //fputs(str, stdout);       // print string str without '\n'
  89.         }
  90.         SetColorAndBackground(7,0);
  91.         printf("\n");
  92.     }
  93.  
  94.     SetColorAndBackground(7,0);
  95.     return 0;
  96.  
  97. } // main()
Advertisement
RAW Paste Data Copied
Advertisement