How to highlight text (change color) in ANSI C

Asked

Viewed 1,650 times

2

Good evening to you... I’m wondering how to use the textcolor function in c but I couldn’t find it in the conio library... does anyone know where this function is located or if I have to create it how? thank you

  • This function should only work in turbo copier C or dev C. You will have to create a method that changes the color through the color options of the prompt.

  • You could post your code in the question?

  • so... I will apply this in several programs n has precise specific code a type the textcolor function of pascal that changes the color from Prox character to Prox textcolor function without affecting the already printed information

  • You want to change the color of all the printed text at the prompt?

  • only that I started from the line of execution

3 answers

2


If you are using the Operating System Windows, you can include the library windows.h and use:

typedef enum{BLACK,BLUE,GREEN,CYAN,RED,MAGENTA,BROWN,LIGHTGRAY,DARKGRAY,
LIGHTBLUE,LIGHTGREEN,LIGHTCYAN,LIGHTRED,LIGHTMAGENTA,YELLOW,WHITE} COLORS;

static int __BACKGROUND = BLACK;
static int __FOREGROUND = LIGHTGRAY;

void textcolor (int color)
{
    __FOREGROUND = color;
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
    color + (__BACKGROUND << 4));
}

You can also use the library curses.h:

start_color();      
init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1));
printw("Mensagem com cor!");
attroff(COLOR_PAIR(1));

1

Depending on the OS you use, you have different implementations. Systems that in MS-DOS, have the library conio.h, meanwhile systems Unix owns the curses.h.

Another way is to write directly the characters responsible for the colors of your text (In the case of systems Unix):

Effects

/*****************************EFECTS***************************************/
#define NONE        "\033[0m"
#define BOLD        "\033[1m"
#define HALFBRIGHT  "\033[2m"
#define UNDERSCORE  "\033[4m"
#define BLINK       "\033[5m"
#define REVERSE     "\033[7m"

/*****************************COLORS***************************************/
#define C_BLACK     "\033[30m"
#define C_RED       "\033[31m"
#define C_GREEN     "\033[32m"
#define C_YELLOW    "\033[33m"
#define C_BLUE      "\033[34m"
#define C_MAGENTA   "\033[35m"
#define C_CYAN      "\033[36m"
#define C_GRAY      "\033[37m"

/***************************BACKGROUNDS************************************/
#define BG_BLACK    "\033[40m"
#define BG_RED      "\033[41m"
#define BG_GREEN    "\033[42m"
#define BG_YELLOW   "\033[43m"
#define BG_BLUE     "\033[44m"
#define BG_MAGENTA  "\033[45m"
#define BG_CYAN     "\033[46m"
#define BG_GRAY     "\033[47m"

These combinations of characters can be used to clear the screen, send closing signals, position the cursor, among others.

Using effects

const char *string = "texto escrito em verde!";
printf("%s%s%s",C_GREEN,string,NONE);

The NONE serves to clear any styling done, it returns the text style to the terminal pattern.

-1

Depending on the Operating System, there is no implementation of the conio.h, basically depends on whether it was compiled into MS-DOS or not. Probably, the system you are using was not.

In that case, use #include <curses.h>, that will give you almost all the features that the conio.h. But if it is not installed, you may have to search for this file on the internet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.