Chapter 21 - Bells & Whistles

21.1 Color in Windows Console and Mac Terminal

You may use ANSI escape sequences to add colored text on Windows 10 Command Prompt and macOS Terminal.

For newer versions of macOS, you shouldn't have to configure anything for them to work.

For Windows 10, you may need to enable the ANSI escape sequences since they are disabled by default. It can be enabled with the following registry modification (run from the command prompt as administrator):

reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f

Windows and Mac colors
#include <iostream>
using namespace std;

string CLS = "\033[2J\033[1;1H";
string Red = "\033[31;1m";
string Green = "\033[32;1m";
string Yellow = "\033[33;1m";
string Blue = "\033[34;1m";
string Purple = "\033[35;1m";
string Cyan = "\033[36;1m";
string White = "\033[37;1m";
string Default = "\033[0m"; // default gray color & reset background to black

string WhiteOnRed = "\033[41;1m";
string WhiteOnGreen = "\033[42;1m";
string WhiteOnYellow = "\033[43;1m";
string WhiteOnBlue = "\033[44;1m";
string WhiteOnPurple = "\033[45;1m";
string WhiteOnCyan = "\033[46;1m";


int main()
{
    cout << Red << " Red " << Green << " Green " << Yellow << " Yellow " << Blue << " Blue " << endl;
    cout << Purple<<" Purple " << Cyan << " Cyan " << White << " White " << Default << " Default " << endl;
    cout << WhiteOnRed<<" White on Red "<<WhiteOnGreen<<" White on Green "<<WhiteOnYellow<<" White on Yellow " << endl;
    cout << WhiteOnBlue<<" White on Blue "<<WhiteOnPurple<<" White on Purple "<<WhiteOnCyan<<" White on Cyan " << endl;
    cout << Default;  // reset background to black after printing inverse colors

    return 0;
}
 Red Green Yellow Blue
 Purple Cyan White Normal
 White on Red  White on Green  White on Yellow
 White on Blue  White on Purple  White on Cyan

 

21.2 Color in Windows Console Program using windows.h

If you are doing a console program in Windows, then you can change the color of the text using the windows.h library.  Below is a setcolor function you can use to change to some popular colors.

Windows.h colors
#include <iostream>
#include <windows.h>
using namespace std;

void setcolor(string C)
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    if (C=="gray" || C=="grey" || C=="default")
        SetConsoleTextAttribute(h, 7);
    else if (C=="blue")
        SetConsoleTextAttribute(h, 9);
    else if (C=="green")
        SetConsoleTextAttribute(h, 10);
    else if (C=="cyan")
        SetConsoleTextAttribute(h, 11);
    else if (C=="red")
        SetConsoleTextAttribute(h, 12);
    else if (C=="purple")
        SetConsoleTextAttribute(h, 13);
    else if (C=="yellow")
        SetConsoleTextAttribute(h, 14);
    else if (C=="white")
        SetConsoleTextAttribute(h, 15);
}

int main()
{
   setcolor("default");  cout << "default\n";
   setcolor("blue");     cout << "blue\n";
   setcolor("green");    cout << "green\n";
   setcolor("cyan");     cout << "cyan\n";
   setcolor("red");      cout << "red\n";
   setcolor("purple");   cout << "purple\n";
   setcolor("yellow");   cout << "yellow\n";
   setcolor("white");    cout << "white\n";
   return 0;
}
default
blue
green
cyan
red
purple
yellow
white


21.3  Sound in Windows Code::Blocks Console Program

If you are doing a console program in Code::Blocks using Windows, you can play a wav (not mp3) file as shown below.  The cricket.wav file must be in the same folder as your exe - it is not embedded into the exe.  You must first make a change in the Code::Blocks settings:   Select Settings, Compiler, Linker settings.  Under Other linker options, paste the following:  -lwinmm

Windows Code::Blocks sound

// Paste this under Settings->Compiler->Linker settings->Other linker options: -lwinmm
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;

int main()
{
   // cricket.wav must be in the same folder at the cpp and exe
   mciSendString("open cricket.wav alias MY_SND3", NULL, 0, NULL);
   mciSendString("play MY_SND3", NULL, 0, NULL);
   system("pause"); // pause program because sound stops when program ends
}

Output
Press any key to continue.


21.4  Cursor Movement in Windows Console Program

You may move the cursor to an x,y location in a Windows console program using the gotoxy function below.  The top left position is 0,0.

Windows cursor movement

#include <iostream>
#include <windows.h>
using namespace std;

void gotoxy(int x, int y)
{
    COORD p = {x, y};
    SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p);
}

int main()
{
    gotoxy(0,0);   cout << "0,0";
    gotoxy(40,0);  cout << "40,0";
    gotoxy(0,10);  cout << "0,10";
    gotoxy(40,10); cout << "40,10";
}

Output
0,0                                     40,0









0,10                                    40,10