Color Question..

I have seen many forumers thread about the colors changing in c++, but i could not understand it, and i have google many site to know how can i put many color in one program that i made, but i dont get it, some say u need other software so that u can put many color in c++, let say, i want to make "hello world" in blue, and the next line "I'm aquos!" in red, what is the command? can't we do 2 color in c++? do we need another software to integrate with? or whats mean by winapi? because i seen many people said need winapi so u can create many color(i dont get what they say actually, forgive me if im getting the wrong message).

can some1 post the code for color for hello world and im aquos?


thanks again!
Yeah,
Lets solve this step-by-step. First the WinAPI stuff means Windows Application Programming Interface. This is a collection of functions and data types, used to aid programming for only the Windows OS.
Now so far I've not yet come accross any function in C++, that changes the console text colour but I've come accross one in the WinAPI. Just forgotting the name so you can look it up on google now that I've gjven you some insight. Also check Microsoft's MSDN website. They have good info on their functions.

HTH,
Aceix.
@Aquos

Here you go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Color Question.cpp : main project file.

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

enum Colors{
	black,          //  0 
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};


using namespace std;

#define on , // So I can use the function - void text(text_color on background_color)
             // To more easily remember which is text color vs background color

// My text color function. Use it if you wish.
void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color+(paper_color*16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );
}

int main()
{
	text(light_blue on black); // Use whatever two colors you wish. First color is text, second is background
		cout << "Hello World" << endl << endl;
	text(light_red on black);
		cout << "I'm Aquos" << endl;
	cin.clear();
	cin.sync();
	cin.get();
	return 0;
}
Last edited on
thanks guys!
More generally, some terminal applications do treat certain character (combinations) as formatting codes rather than as printable characters.
http://en.wikipedia.org/wiki/ANSI_escape_code

Windows terminal does not seem to be one of them.
#include <iostream>
#include <conio.h>
using namespace std;

int main(){
cout<<"Thanks!"<<endl;
_getch();
}
Topic archived. No new replies allowed.