1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
// Black text on White paper.cpp : main project file.
// Colored Text on Paper.cpp : main project file.
#include <iostream>
#include <string>
#include <conio.h> // For _kbhit()) _getch() use
#include <windows.h>
enum Colors{
black, // 0 text color - multiply by 16, for background colors
dark_blue, // 1
dark_green, // 2
dark_cyan, // 3
dark_red, // 4
dark_magenta, // 5
dark_yellow, // 6
light_gray, // 7
dark_gray, // 8
light_blue, // 9
light_green, // 10
light_cyan, // 11
light_red, // 12
light_magenta, // 13
light_yellow, // 14
white // 15
};
using std::cout;
using std::cin;
using std::endl;
using std::string;
void ClearScreen();
void WaitKey();
#define on , // So I can use the function - void text(text_color on background_color)
// To more easily remember which is text color vs background color
// My text color function. Use it if you wish.
void text(int text_color = 7 on int paper_color = 0)
{
// defaults to light_gray on black
int color_total = (text_color+(paper_color*16));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );
}
int main()
{
text(black on white); // Able to use names here, because of enum Colors{};
ClearScreen();// After setting color of background, use ClearScreen() to change to the attributes requested
cout << endl <<"This is black text on a white screen." << endl;
text(light_red on white);
cout << endl <<"And this is light red text on a white screen." << endl;
text(light_gray on white);
cout << endl << endl << "Press enter to exit.." << endl;
WaitKey();
ClearScreen();
cout << "\n\n\n\t\tHope you enjoyed.." << endl;
return 0;
}
void ClearScreen()
{
DWORD n;
DWORD size;
COORD coord = {0};
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
SetConsoleCursorPosition ( h, coord );
}
void WaitKey()
{
while (_kbhit()) _getch(); // Empty the input buffer
_getch(); // Wait for a key
while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
|