color for text

hi

i need a simple example that how can i change the default text in console to another. i wantto change text color please help me.
Check out

http://en.wikipedia.org/wiki/ANSI_escape_code

For example

1
2
3
4
char blue[] = { 0x1b, '[', '1', ';', '3', '4', 'm', 0 };
char normal[] = { 0x1b, '[', '0', ';', '3', '9', 'm', 0 };

cout << blue << "This text should be blue" << normal << endl;
That only works for command shells that support that, such as most of them in Linux and UNIX (if not /all/ of them). :P

For Windows, you would need to do the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <windows.h>
using namespace std;

//returns the current attributes
WORD GetConsoleTextAttribute (HANDLE hCon)
{
  CONSOLE_SCREEN_BUFFER_INFO con_info;
  GetConsoleScreenBufferInfo(hCon, &con_info);
  return con_info.wAttributes;
}

int main (void)
{
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  const int saved_colors = GetConsoleTextAttribute(hConsole);

  SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  cout << "This text should be blue" << endl;
  SetConsoleTextAttribute(hConsole, saved_colors);
}


Windows is a pain. I'm not sure if it would be any easier with .NET if that is available...
Anyway, you can find the available attributes for Windows here:
http://msdn2.microsoft.com/en-us/library/ms682088(VS.85).aspx#_win32_character_attributes

(you can ignore the bit about DBCS, Double-Byte Character Sets, probably)

I hope this helps! ^_^

rpgfan


Edit: According to that Wikipedia article jsmith posted (thanks for that), you can do it in DOS-based versions of Windows (95, 98, ME and earlier) as long as ANSI.SYS is loaded. The same is true of NT-based versions. However, it wouldn't make your program very reliable if it wasn't loaded, would it? ^_^
Last edited on
A simple way to change text color to blue is to include this statement within the main: system( "color 09" );

Here are some more examples:

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
# include <iostream>
# include <windows.h> // Sleep( milliseconds );

using namespace std;

int main()
{
    
    cout << "\n\n\t\tText Color";
    
    system ( "color 01" );
    Sleep ( 2000 );
    
    system ( "color 02" );
    Sleep ( 2000 );
    
    system ( "color 03" );
    Sleep ( 2000 );
    
    system ( "color 04" );
    Sleep ( 2000 );
    
    system ( "color 05" );
    Sleep ( 2000 );
    
    system ( "color 06" );
    Sleep ( 2000 );
    
    system ( "color 07" );
    Sleep ( 2000 );
    
    system ( "color 08" );
    Sleep ( 2000 );
    
    system ( "color 09" );
    Sleep ( 2000 );
    
    system( "color 0A" );
    Sleep( 2000 );
    
    system( "color 0B" );
    Sleep( 2000 );
    
    system( "color 0C" );
    Sleep( 2000 );
    
    system( "color 0D" );
    Sleep( 2000 );
    
    system( "color 0E" );
    Sleep( 2000 );
    
    system( "color 0F" );
    Sleep( 2000 );
    
    system ( "cls" );
    
    system ( "color 01" );
    cout << "\n\n\t\tBackground Color";

    system ( "color 10" );
    Sleep( 2000 );
    
    system ( "color 20" );
    Sleep( 2000 );
    
    system ( "color 30" );
    Sleep( 2000 );
    
    system( "color 40" );
    Sleep( 2000 );
    
    system( "color 50" );
    Sleep( 2000 );
    
    system ( "color 60" );
    Sleep( 2000 );
    
    system( "color 70" );
    Sleep( 2000 );
    
    system( "color 80" );
    Sleep( 2000 );
    
    system( "color 90" );
    Sleep( 2000 );
    
 
    
 
 system ( "pause" );   
    
}
Last edited on
Topic archived. No new replies allowed.