How to paint the background of a text in the console

3
#include <iostream>
#include <windows.h>
#include <conio.h>

#define ARRIBA     72
#define IZQUIERDA  75
#define DERECHA    77
#define ABAJO      80

using namespace std;

int i=4;

void SetColor(int ForgC)
{
    WORD wColor;

    //This handle is needed to get the current background attribute
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    //csbi is used for wAttributes word

    CONSOLE_SCREEN_BUFFER_INFO csbi;

    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
    {
      //To mask out all but the background attribute, and to add the color
      wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0xF0);
      SetConsoleTextAttribute(hStdOut, wColor);
    }
 return;
}

void gotoxy(int x, int y)  // funcion que posiciona el cursos en la         coordenada (x,y)
{
    HANDLE hCon;
    COORD dwPos;

    dwPos.X = x;
    dwPos.Y = y;
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hCon,dwPos);
}

void seleccionarOpcion(char tecla){

gotoxy(4, i); cout<<" ";

if( tecla == ABAJO && i < 7 ){
    gotoxy(8, 9);
    cout<<"                          ";
    i++;
}
if( tecla == ARRIBA && i > 4){
    gotoxy(8, 9);
    cout<<"                          ";
    i--;
}

if( tecla == 13 ){
    gotoxy(8, 9);
    if( i == 4 ) cout<<"Eligio la primera opcion";
    if( i == 5 ) cout<<"Eligio la segunda opcion";
    if( i == 6 ) cout<<"Eligio la tercera opcion";
    if( i == 7 ) cout<<"Eligio la cuarta opcion";
}


   gotoxy(4, i); cout<<">";
}

void menu(char tecla){
seleccionarOpcion(tecla);

gotoxy(5, 4); cout<<" primera";
gotoxy(5, 5); cout<<" segunda";
gotoxy(5, 6); cout<<" tercera";
gotoxy(5, 7); cout<<" cuarta";
}


int main()
{
    char tecla;
    while(true){

    if( kbhit() )
        tecla = getch();
    else
        tecla = ' ';

    menu(tecla);

    Sleep(40);
}


system("pause>null");

return 0;
}

What I want to get to do is something like this

    
asked by Cristian Alvarez Hernandez 11.06.2016 в 03:29
source

2 answers

3

You could also use the ANSI escape codes link (The English version is more complete)

But it is important to indicate that the support may vary depending on the terminal used.

The sequence must start with an escape character

  

ESC [... 38; 2 ;;; ... m Text Color

     

ESC [... 48; 2 ;;; ... m Background Color

You can combine the text color with a background and print them

For example, the following code will print a text with a yellow background and red letters, the last [0m open leaves the terminal as it was.

printf("\x1b[0;31;43mhello world\x1b[0m");

Where 31 is the text color (Red) and 43 the background color (Yellow)

I leave the table of equivalences

Text Color

+=========+=======================+===================+
|  ANSI   | Terminfo Equivalencia |    Descripción    |
+=========+=======================+===================+
| [ 3 0 m | setaf 0               |  #0 - Negro       |
+---------+-----------------------+-------------------+
| [ 3 1 m | setaf 1               |  #1 - Rojo        |
+---------+-----------------------+-------------------+
| [ 3 2 m | setaf 2               |  #2 - Verde       |
+---------+-----------------------+-------------------+
| [ 3 3 m | setaf 3               |  #3 - Amarillo    |
+---------+-----------------------+-------------------+
| [ 3 4 m | setaf 4               |  #4 - Azul        |
+---------+-----------------------+-------------------+
| [ 3 5 m | setaf 5               |  #5 - Magenta     |
+---------+-----------------------+-------------------+
| [ 3 6 m | setaf 6               |  #6 - Cyan        |
+---------+-----------------------+-------------------+
| [ 3 7 m | setaf 7               |  #7 - Blanco      |
+---------+-----------------------+-------------------+
| [ 3 9 m | setaf 9               | Valor por defecto |
+---------+-----------------------+-------------------+

Background Color

+=========+=======================+====================+
|  ANSI   | Terminfo Equivalencia |    Descripción     |
+=========+=======================+====================+
| [ 4 0 m | setab 0               |  #0 - Negro        |
+---------+-----------------------+--------------------+
| [ 4 1 m | setab 1               |  #1 - Rojo         |
+---------+-----------------------+--------------------+
| [ 4 2 m | setab 2               |  #2 - Verde        |
+---------+-----------------------+--------------------+
| [ 4 3 m | setab 3               |  #3 - Amarillo     |
+---------+-----------------------+--------------------+
| [ 4 4 m | setab 4               |  #4 - Azul         |
+---------+-----------------------+--------------------+
| [ 4 5 m | setab 5               |  #5 - Magenta      |
+---------+-----------------------+--------------------+
| [ 4 6 m | setab 6               |  #6 - Cyan         |
+---------+-----------------------+--------------------+
| [ 4 7 m | setab 7               |  #7 - Blanco       |
+---------+-----------------------+--------------------+
| [ 4 9 m | setaf 9               |  Color por defecto |
+---------+-----------------------+--------------------+
    
answered by 11.06.2016 / 05:17
source
1

You can define the color using the system function

  

system ("color * background ** foreground *");

Where background and foreground can contain values between 0-9 and A-Z

The following code prints a text with a blue background and green text

system("color A1");
std::cout<<"Hello World"<<std::ends;

To see all the color options run

system("color %");
    
answered by 11.06.2016 в 03:37