Advertisement
ohusq

C++ Coloured console output.

Apr 29th, 2024 (edited)
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | Source Code | 0 0
  1. // color your text in Windows console mode
  2. // colors are 0=black 1=blue 2=green and so on to 15=white  
  3. // colorattribute = foreground + background * 16
  4. // to get red text on yellow use 4 + 14*16 = 228
  5. // light red on yellow would be 12 + 14*16 = 236
  6. // a Dev-C++ tested console application by  vegaseat  07nov2004
  7.  
  8. #include <iostream>
  9. #include <windows.h>   // WinApi header
  10.  
  11. using namespace std;    // std::cout, std::cin
  12.  
  13. int main()
  14. {
  15.   HANDLE  hConsole;
  16.     int k;
  17.    
  18.   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  19.  
  20.   // you can loop k higher to see more color choices
  21.   for(k = 1; k < 255; k++)
  22.   {
  23.     // pick the colorattribute k you want
  24.     SetConsoleTextAttribute(hConsole, k);
  25.     cout << k << " I want to be nice today!" << endl;
  26.   }
  27.  
  28.   cin.get(); // wait
  29.   return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement