#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 do is something more or less like this
You could also use the ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code (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
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 opened leaves the terminal as it was.
Where 31 is the text color (Red) and 43 the background color (Yellow)
I leave you the equivalence table
Text Color
Background color
You can define the color using the system function
Where background and foreground can contain values between 0-9 and AZ
The following code prints a text with blue background and green text
To see all the color options run