How might someone be able to...

How might someone be able to color a specific line if text?
I already know about system("color 4A"); but I want only a specific line to be colored a color.
By the way, MERRY CHRISTMAS!
Last edited on
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
#include <windows.h>
#include <iostream>

//Think about wrapping WinAPI calls to throw exceptions
//instead of constant erorcode checking
int main()
{
    // Get handle to STDOUT.
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdout == INVALID_HANDLE_VALUE)
        return EXIT_FAILURE;

    // Save the current text colors.
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
        return EXIT_FAILURE;
    WORD wOldColorAttrs = csbiInfo.wAttributes;

    // Set the text attributes to draw red text on black background.
    if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_INTENSITY))
        return EXIT_FAILURE;
    std::cout << "HI\n";

    // Set the text attributes to draw green text on black background.
    if (! SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN | FOREGROUND_INTENSITY))
        return EXIT_FAILURE;
    std::cout << "Hi again!\n";

    // Restore the original text colors.
    SetConsoleTextAttribute(hStdout, wOldColorAttrs);
}

EDIT: Windows only. For *nix based OS, lookup documentation on its API
Last edited on
Thanks, now how would you make the highlight it in Brown? Sorry, I don't know how to do this stuff yet.
Last edited on
You will need to add the background flag.

f (! SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN | BACKGROUND_RED | FOREGROUND_INTENSITY))

Here is information on the attribute flags btw:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Attribute	Meaning
FOREGROUND_BLUE	Text color contains blue.
FOREGROUND_GREEN	Text color contains green.
FOREGROUND_RED	Text color contains red.
FOREGROUND_INTENSITY	Text color is intensified.
BACKGROUND_BLUE	Background color contains blue.
BACKGROUND_GREEN	Background color contains green.
BACKGROUND_RED	Background color contains red.
BACKGROUND_INTENSITY	Background color is intensified.
COMMON_LVB_LEADING_BYTE	Leading byte.
COMMON_LVB_TRAILING_BYTE	Trailing byte.
COMMON_LVB_GRID_HORIZONTAL	Top horizontal.
COMMON_LVB_GRID_LVERTICAL	Left vertical.
COMMON_LVB_GRID_RVERTICAL	Right vertical.
COMMON_LVB_REVERSE_VIDEO	Reverse foreground and background attributes.
COMMON_LVB_UNDERSCORE	Underscore.
 


ps you can mix and match colors/intensities.
Last edited on
What about Brown? How could you get brown? I'm trying to color the trunk in a Christmas tree.
Last edited on
You could try red and green? Just mix and match until you get what you want. Try using loops to get every possible choice.

You will want to mix and match these
1
2
3
4
5
6
7
8
FOREGROUND_BLUE	Text color contains blue.
FOREGROUND_GREEN	Text color contains green.
FOREGROUND_RED	Text color contains red.
FOREGROUND_INTENSITY	Text color is intensified.
BACKGROUND_BLUE	Background color contains blue.
BACKGROUND_GREEN	Background color contains green.
BACKGROUND_RED	Background color contains red.
BACKGROUND_INTENSITY	Background color is intensified.


Which means there are 8! possibilities


By the way you should just use SFML :P or even MS Paint
Last edited on
Ok, so how can you color the text of a string?
Last edited on
Change color, output letter, change color, output letter...
I mean save the color of the words and change to that color every time that word it displayed...
Wrap it in a function or even in class:
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
#include <windows.h>
#include <iostream>

class colored_text
{
public:
	colored_text(std::string txt, WORD ColorAttrs) :
		text(std::move(txt)), color(ColorAttrs) {}
	friend std::ostream& operator<<(std::ostream& out, const colored_text& text);
private:
	std::string text;
	WORD color;
	static HANDLE hout;
};

HANDLE colored_text::hout = GetStdHandle(STD_OUTPUT_HANDLE);

std::ostream& operator<<(std::ostream& out, const colored_text& text)
{
	CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    GetConsoleScreenBufferInfo(colored_text::hout, &csbiInfo);
    WORD wOldColorAttrs = csbiInfo.wAttributes;
    SetConsoleTextAttribute(colored_text::hout, text.color);
    out << text.text;
    SetConsoleTextAttribute(colored_text::hout, wOldColorAttrs);
    return out;
}

int main()
{
	colored_text word("I am colored", BACKGROUND_GREEN | BACKGROUND_RED | FOREGROUND_BLUE);
	std::cout << "Hi " << word << '\n' << word << " Other text";
}
http://puu.sh/dBMta/9b2ee3dc99.png

Thanks for all the help!
MERRY CHRISTMAS!
Last edited on
Topic archived. No new replies allowed.