How to change the display colors of an application written in C

Table of contents:

How to change the display colors of an application written in C
How to change the display colors of an application written in C
Anonim

It is possible to highlight the text or graphic shapes of a program displayed in a console by changing their color. This is a fairly straightforward process to implement in a program written in C, because the functions that must be used are part of the language's standard libraries. You will be able to change the color of anything that can be displayed in a console, whether it is text or graphics.

Steps

Part 1 of 2: change the output color of text

Get Color in C Program Step 1

Step 1. Include the standard input and output library

The header of this library must be placed at the start of a C source program. The functions it defines will allow you, among other things, to modify the color of the text displayed by your program. Add the following line at the start of your source code:

#include

Get Color in C Program Step 2

Step 2. Include the console input and output library

The functions described in this library will make it easier to capture user actions on the keyboard. Add this header file below the line containing the stdio.h library:

#include #include

Get Color in C Program Step 3

Step 3. Set the color of the text to display with the textcolor () function

You can use this function to vary the display color of the output text. Colors should be expressed in your source code in uppercase letters and English or a numeric value that represents them:

#include #include main () {textcolor (RED); // You could replace "RED" with "4", but it's less understandable}

Color Numerical value
BLACK (black) 0
BLUE (blue) 1
GREEN (green) 2
CYAN (cyan) 3
RED (red) 4
MAGENTA (magenta) 5
BROWN (brown) 6
LIGHTGRAY (light gray) 7
DARKGRAY (dark gray) 8
LIGHTBLUE (light blue) 9
LIGHTGREEN (light green) 10
LIGHTCYAN (light cyan) 11
LIGHTRED (light red) 12
LIGHTMAGENTA (light magenta) 13
YELLOW (yellow) 14
WHITE (white) 15

Colors other than these exist, but their availability depends on the graphics drivers installed and the mode used. The names of the colors must be expressed in capital letters and in English

Get Color in C Program Step 4

Step 4. Add text to display and complete the source code

Use the cprintf () function to display the text in the color of your choice. You can use the getch () function at the end of your source code to close the program following a user action on the keyboard.

#include #include main () {textcolor (RED); // You can replace "RED" with "4", but it's less understandable cprintf ("Hello everyone!"); getch (); return 0; }

Part 2 of 2: change the color of a drawing

Get Color in C Program Step 5

Step 1. Include the standard library of graphics functions

It contains functions for drawing objects and setting their colors. You will need to include it at the start of your source program:

#include

Get Color in C Program Step 6

Step 2. Include the console input and output library

The functions described in this library will make it easier to capture user actions on the keyboard. Add this header file below the line containing the graphics.h library:

#include #include

Get Color in C Program Step 7

Step 3. Initialize the variables defining the graphics modes and drivers

You will need to do this before using the object drawing functions, so that your program can access your PC's graphics drivers. You will be able to delimit a graphics area in which your program will draw objects.

#include #include main () {int gd = DETECT, gm; initgraph (& gd, & gm, "C: \ TC \ BGI"); // Replace this with the path of your compiler}

Get Color in C Program Step 8

Step 4. Define the color of the object to be drawn

You will need to use the setcolor () function, which is responsible for setting the color of the object you want to draw. This function must be inserted in your source file before the code used to draw the graphic object:

#include #include main () {int gd = DETECT, gm; initgraph (& gd, & gm, "C: \ TC \ BGI"); setcolor (BLUE); // You can replace "BLUE" with "1", but it's less understandable}

Get Color in C Program Step 9

Step 5. Code the drawing of the object of your choice

We will take here an example of drawing a simple rectangle using the rectangle () function. You can also draw any object described in the graphics.h header file with the color defined previously.

#include #include main () {int gd = DETECT, gm; initgraph (& gd, & gm, "C: \ TC \ BGI"); setcolor (BLUE); rectangle (50, 50, 100, 100); // These numbers define the upper left corner and the lower right corner of the rectangle}

Get Color in C Program Step 10

Step 6. Finish your program and test it

Add the getch () function and close the graphics area before exiting the program. The role of this function is to wait for a character to be typed on the keyboard, and in this specific case, to generate the order to close the program. Save your source file, compile it, and try it out.

#include #include main () {int gd = DETECT, gm; initgraph (& gd, & gm, "C: \ TC \ BGI"); setcolor (BLUE); rectangle (50, 50, 100, 100); getch (); closegraph (); return 0; }

Examples

// Bonjour.c in color: main source file. #include // Delete this line if you are using any other compiler than MS Visual Studio Express. #include // Library used for the WaitKey () function #include #include #include using namespace std; HANDLE console = GetStdHandle (STD_OUTPUT_HANDLE); // This is necessary for SetConsoleTextAttribute () void WaitKey (); int main () {int len ​​= 0, x, y = 240; // 240 = Black on white background string text = '' Hello everyone, how are you? ''; len = text.length (); cout << endl << endl << endl << '' \ t \ t ''; // We go 3 lines down and 2 tabs to the right to center for (x = 0; x <len; x ++) {SetConsoleTextAttribute (console, y); // We prepare the color for the next print cout << text [x]; y ++; // We add 1 to y for a new color if (y> 254) // There are 255 colors. The color 255 displays white on a white background. We skip the step, because invisible y = 240; // if y> 254, we start the black cycle on a white background Sleep (250); // A pause between each letter} SetConsoleTextAttribute (console, 15); // Application of white color on a black background WaitKey (); // Wait for a key action to exit the program} void WaitKey () {cout << endl << endl << endl << '' \ t \ t \ tPress a key ''; while (_kbhit ()) _getch (); // We empty the input buffer _getch (); // Wait for the keystroke while (_kbhit ()) _getch (); // We empty the input buffer (some keys send two messages)}

#include #include main () {int gd = DETECT, gm, drawing_color; char a [100]; initgraph (& gd, & gm, '' C: \ TC \ BGI ''); drawing_color = getcolor (); sprintf (a, '' Current drawing color =% d '', drawing_color); outtextxy (10, 10, a); getch (); closegraph (); return 0; }

Popular by topic