Source:Test/colors.cpp

From Codemotion
Jump to: navigation, search

Source:Test/colors.cpp

Description

Functions for console colors by NickDMax.

Code

Parent Directory: Source:Test
Plain Code: edit

  1. //*( These console outputs were made by NickDMax "WinConsole.c". All credits go to him for the console coloring.)
  2. // (http://www.dreamincode.net/code/snippet921.htm)
  3.  
  4. void ConPrint(char *CharBuffer, int len);
  5.  
  6. void ConPrintAt(int x, int y, char *CharBuffer, int len);
  7.  
  8. void gotoXY(int x, int y);
  9.  
  10. void ClearConsole();
  11.  
  12. void ClearConsole(int ForgC, int BackC);
  13.  
  14. void SetColor(int ForgC, int BackC); // Noble Gas = pink(-3) Metal= yellow(-2) N-M = teal(11) Metalloid = green(10)
  15.  
  16. void SetColor(int ForgC);
  17.  
  18. void HideCursor();
  19.  
  20. void ShowCursor();
  21.  
  22.  
  23. void ClearConsole(int ForgC, int BackC)
  24. {
  25.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  26.      //Get the handle to the current output buffer...
  27.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  28.      //This is used to reset the carat/cursor to the top left.
  29.      COORD coord = {0, 0};
  30.      //A return value... indicating how many chars were written
  31.      //   not used but we need to capture this since it will be
  32.      //   written anyway (passing NULL causes an access violation).
  33.      DWORD count;
  34.  
  35.      //This is a structure containing all of the console info
  36.      // it is used here to find the size of the console.
  37.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  38.      //Here we will set the current color
  39.      SetConsoleTextAttribute(hStdOut, wColor);
  40.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  41.      {
  42.           //This fills the buffer with a given character (in this case 32=space).
  43.           FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  44.  
  45.           FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
  46.           //This will set our cursor position for the next print statement.
  47.           SetConsoleCursorPosition(hStdOut, coord);
  48.      }
  49.      return;
  50. }
  51.  
  52. //This will clear the console.
  53. void ClearConsole()
  54. {
  55.      //Get the handle to the current output buffer...
  56.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  57.      //This is used to reset the carat/cursor to the top left.
  58.      COORD coord = {0, 0};
  59.      //A return value... indicating how many chars were written
  60.      //   not used but we need to capture this since it will be
  61.      //   written anyway (passing NULL causes an access violation).
  62.      DWORD count;
  63.      //This is a structure containing all of the console info
  64.      // it is used here to find the size of the console.
  65.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  66.      //Here we will set the current color
  67.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  68.      {
  69.           //This fills the buffer with a given character (in this case 32=space).
  70.           FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  71.           FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
  72.           //This will set our cursor position for the next print statement.
  73.           SetConsoleCursorPosition(hStdOut, coord);
  74.      }
  75.      return;
  76. }
  77.  
  78. //This will set the position of the cursor
  79. void gotoXY(int x, int y)
  80. {
  81.      //Initialize the coordinates
  82.      COORD coord = {x, y};
  83.      //Set the position
  84.      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  85.      return;
  86. }
  87.  
  88. //This will set the forground color for printing in a console window.
  89. void SetColor(int ForgC)
  90. {
  91.      WORD wColor; 
  92.      //We will need this handle to get the current background attribute
  93.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  94.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  95.  
  96.      //We use csbi for the wAttributes word.
  97.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  98.      {
  99.         //Mask out all but the background attribute, and add in the forgournd color
  100.           wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
  101.           SetConsoleTextAttribute(hStdOut, wColor);     
  102.      }
  103.      return;
  104. }
  105.  
  106. //This will set the forground and background color for printing in a console window.
  107. void SetColor(int ForgC, int BackC)
  108. {
  109.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);; 
  110.      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);     
  111.      return;
  112. }
  113.  
  114. //Direct console output
  115. void ConPrint(char *CharBuffer, int len)
  116. {
  117.     DWORD count;
  118.      WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
  119.      return;
  120. }
  121.  
  122. //Direct Console output at a particular coordinate.
  123. void ConPrintAt(int x, int y, char *CharBuffer, int len)
  124. {
  125.     DWORD count;
  126.      COORD coord = {x, y}; 
  127.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  128.     SetConsoleCursorPosition(hStdOut, coord); 
  129.      WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
  130.      return;
  131. }
  132.  
  133. //Hides the console cursor
  134. void HideCursor()
  135. {
  136.      CONSOLE_CURSOR_INFO cciCursor;
  137.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  138.  
  139.  
  140.      if(GetConsoleCursorInfo(hStdOut, &cciCursor))
  141.      {
  142.           cciCursor.bVisible=FALSE;
  143.      }
  144.      return;
  145. }
  146.  
  147. //Shows the console cursor
  148. void ShowCursor()
  149. {
  150.      CONSOLE_CURSOR_INFO cciCursor;
  151.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  152.  
  153.  
  154.      if(GetConsoleCursorInfo(hStdOut, &cciCursor))
  155.      {
  156.           cciCursor.bVisible=TRUE;
  157.      }
  158.      return;
  159. }
Personal tools