text colour

I used this to change the background colour

system("color 08");

but I want to change the background of colour in such a way that only area over which text is written should be coloured....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <windows.h> // WinApi header

using namespace std; // std::cout, std::cin

int main()
{
HANDLE hConsole;
int k;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " Hello cplusplus.com" << endl;
}

cin.get(); // wait
return 0;
}
OK the program worked but I didnt get what thing control the color of text and what controlled the color of background.
the int k;is the combination of the text color and the background color. 10 means green text on black background.
what if I want to control both of them individually.
not working you have to choose one of the numbers in front of "Hello cplusplus.com" for your color combination. sorry it is no standard C++
Okay thanks for the admirable help.
no problem my friend
@Prashant Gupta PG

This may be of use to you..
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Colored Text on Paper.cpp : main project file.

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

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	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()
{
	string Color[16] ={"Black","Dark Blue","Dark Green","Dark Cyan","Dark Red","Dark Magenta","Dark Yellow","Light Gray",
		"Dark Gray","Light Blue","Light Green","Light Cyan","Light Red","Light Magenta","Light Yellow","White"};
	int i,j;

	for(j=0;j<16;j++)
	{
		for(i=0;i<16;i++)
		{
			if(j==i) // If text color is same as paper color
			{
				i++; // Skip to next color
			}
			if (i<16) // If not white on white, then continue
			{
				text(i on j);
				cout << Color[i] << " on " << Color[j] << " (Text color=" << i << "+Background color="<< j*16 << " (" << j << "*16)=" << i+(j*16) << ")" << endl;
			}
		}
	}
	text(); // Able to use names here, because of enum Colors{};

	cout << endl << endl << "Where a number is skipped, the text would be same color as the backgound.." << endl;
	cout << "To use color, add to your list of ";
	text(light_red on black);
	cout << "#include";
	text();
	cout << "'s, if not already used.." << endl;
	text(light_red on black);
	cout << "#include <windows.h>";
	text();
	cout << "." << endl << "Declare the following, afterwards.." << endl;
	text(light_red on black);
	cout << "HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);" << endl;
	text();
	cout << "and use the folowing, whenever you wish to change colors.." << endl;
	text(light_red on black);
	cout << "SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );" << endl;
	text();
	cout <<  "Or examine the '";
	text(light_red on black);
	cout << "void text(int text_color on int paper_color);";
	text();
	cout << "' function," << endl <<"at the top of this program. Works great, doesn't it ??" << endl;
	cout << "Just make sure to add '";
	text(light_red on black);
	cout << "#define on ,";
	text();
	cout << "', if you want to use the '";
	text(light_red on black);
	cout << "on";
	text();
	cout << "' word," << endl;
	cout << "instead of a comma, to separate the two color choices." << endl << endl;
	cout << "Press enter to exit.." << endl;
	cin.clear();
	cin.sync();
	cin.get();
	return 0;
}
that was awesome but one last thing

Can we make a function textcolor, like the one in conio which on typing textcolor(LIGHTGREEN + BLINK); or somehow gives a blinking light green output.Please do not make a code which blocks the program until it is blinking because i need to take input from user at the same time.
@Prashant Gupta PG

In order to get a BLINK, you have to over-write the words on the screen with the background color, use Sleep(seconds needed), and then reprint the words. I know BASIC had the blink command, but I have yet to find it in C++.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Blink words.cpp : main project file.

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

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	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;

void gotoXY(int, int);

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

#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()
{
	char input=' ';
		
	while (input==' ')
	{
		
		gotoXY(35,12);
		text(light_red on light_yellow);
		cout << " BLINK ";
		Sleep(500);
		text(light_yellow on light_yellow);
		gotoXY(35,12);
		cout << "       ";
		Sleep(500);
	};

	cin.clear();
	cin.sync();
	cin.get();
	return 0;
}

void gotoXY(int x, int y) 
{ 
CursorPosition.X = x; 
CursorPosition.Y = y; 
SetConsoleCursorPosition(console,CursorPosition); 
}
@whitenite1 thats a simple program but the problem arises in my case cuz i need to take an input from user at the same time when the text is blinking.As i mentioned above

Please do not make a code which blocks the program until it is blinking because i need to take input from user at the same time.

This program that u have made will not allow me to take any input from user.
@Prashant Gupta PG

If you can post the code, or send it to me by email, I'll see what I can do to add the blinking text where you specify.
I cant cuz the code is in progress but I can tell u what i'm trying to do.I'm designing a login screen on the top of which a text will keep blinking which says "LOGIN PAGE" and the will enter its account no and pass below it.After login the screen appears on the top right of which the user name will keep blinking and the user will carry out all its transaction from the options below.
With standard C++, it's not easy at all. The reason being is that when you have, for example, cin << myVariable; the program pauses to wait for input. This prevents anything else from happening. However, an alternative is to use threads that can "blink" text while you're waiting, but it must also set the cursor back to where it was. The biggest problem with this becomes that if the user attempts to type while a "blink" process is going on, the characters may not show up in the correct place and will give the illusion that things are wrong. I'd suggest abandoning the "blink" idea since it's not easy to implement even with non standard C++, let alone to blink while waiting for input.

There is some suggestions that might help (I haven't attempted it yet so I don't know how it works). I have read about an ANSI escape character used to blink, but I believe it might blink the title bar, all of the text, or do nothing at all. It might be worth looking into. The escape character is '\33g'. Just cout that and see how it affects your system. It might also flash characters (depending on the implementation of the console). You might want to do some Google searches to see what else is possible since what you're asking is going to be incredibly hard to create.

Again, I suggest that you just abandon the idea of blinking.
@Volatile Pulse

I wrote this program:

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    cout<<"PG";
    cout<<'\33g';
    cout<<"GP";
    getchar();
    return 0;
}


Instead of blinking it gives me an output

PG7015GP
I made a few mistakes, but I never tried to use the ANSI escape characters on Windows (I wrote it on Linux). I just found this out:
Wikipedia wrote:
The Win32 console does not support ANSI escape sequences at all. Software can manipulate the console with the ioctl-like Console API interlaced with the text output. Some software internally interprets ANSI escape sequences in text being printing and translates them to these calls[citation needed].


Essentially, on windows, that means you either need to use another source for viewing your code, or you can't use ANSI.

Here is a list of a whole bunch of different windows functions that can control the console: http://msdn.microsoft.com/en-us/library/ms682073.aspx

I will do some more research, but I don't believe you can get specific text to flash.
I have been wondering this thanks!
Topic archived. No new replies allowed.