Colouring A certain text in console ?

Following up the following thread :
http://www.cplusplus.com/forum/beginner/13816/


I want to ask if there is a certain command to make the the console output certain texts in a color other than the default .. not all the console but a certain line ..

thanks
Hrmm....
The only way I know of doing this is the system("color c") code or something...
Back when I used batch, I would do something like this(for C++, just put system("") around every line, put the line in the ""s.):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@echo off
title colortest
:start
cls
color a
echo This is line 1.
echo.
color b
echo This is line 2.
echo.
color c
echo This is line 3.
echo.
goto start

...
It would look really buggy, but it got the job done(only on windows :P).
It just refreshes as fast as it can, printing the same thing really fast repeatedly.
Let me know if that works for you.
The below goes for windows

There is no command that writes text with a specified color, but there are commands that can change the text apperance. You can eazily write a wrapper for your own printf or if you're using std overload the << operator with a custom made class so i'd look look like this:

cout<<color::red<<"Hai"<<endl;//No class

or

cout<<Format(color::red, "Hai");//Class Format

operator overloading in Cpp is mighty strong:P

As for the functions, google "console functions msdn"
Last edited on
Have you tried all the solutions given at http://www.cplusplus.com/forum/beginner/13816/ ?
Some of them do what you are trying
http://www.adrianxw.dk/SoftwareSite/Consoles/Consoles4.html
check that :)

this is one example on that site. compile it and see if it does what you want, if yes, read the rest about color on that site.

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
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED);
    cout << "Red     " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED |
                            BACKGROUND_INTENSITY);
    cout << "Red     " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN);
    cout << "Green   " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN |
                            BACKGROUND_INTENSITY);
    cout << "Green   " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE);
    cout << "Blue    " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE |
                            BACKGROUND_INTENSITY);
    cout << "Blue    " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN);
    cout << "Yellow  " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN |
                            BACKGROUND_INTENSITY);
    cout << "Yellow  " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE);
    cout << "Cyan    " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE |
                            BACKGROUND_INTENSITY);
    cout << "Cyan    " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE | 
                            BACKGROUND_RED);
    cout << "Magenta " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_BLUE | 
                            BACKGROUND_RED |
                            BACKGROUND_INTENSITY);
    cout << "Magenta " << endl;

    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE);
    cout << "White   " << flush;
    SetConsoleTextAttribute(hOut,
                            BACKGROUND_RED | 
                            BACKGROUND_GREEN | 
                            BACKGROUND_BLUE |
                            BACKGROUND_INTENSITY);
    cout << "White   " << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.