Command line colors

Is there a way to change the color only a certain part of a sentence? so like an example is... My named is named Buddy. Is there a way to make the word 'Buddy' a different color but leave the rest of the sentence white. (im using a windows command line to do this stuff)
Last edited on
Is there a way to do it? Yeah. Is there an existing windows function that does it? Not to my knowledge. Unless someone can point out a function that actually achieves that, you'll need to build a function that will parse through your message, inside which you can use tags to change color.

I did this when I put togethor a logical word wrapping system so I can drop it in a specific area in the console window since I had built a game engine to run on the console and it had a UI. I figured that while I was at it, I could add in a method to change the text color. The end result worked something like this:

1
2
3
 SendGameMessage(FullGreen, "Default message a bright green. $<HalfRed> Message"
			    "is now dark red. $<White> Message color is now white."
			    "$<Default> Back to bright green.");


Tags wouldn't be displayed, but they would change the color accordingly. The whole thing is about 280 lines long and relies on another library I built to do various console operations (such as change colors, set cursor size, set cursor position, etc.) so I can't really post a more detailed example. It's also very likely less subtle than it could be and would be full of silly for everybody to point and laugh at since it was a project I did roughly 2 months into programming. xD
so if i understand correctly you are saying i have to create my own function. I have no idea how to do that!?
Neither did I back when I made it. I just looked through MSDN for windows functions I would need for the acutal manipulation of the console such as SetConsoleTextAttribute(), and worked through the rest. Programming is largely, if not almost entirely, about figuring out solutions to a problem. If you work on it long enough, and use google when you need to, you should be able to make some progress. And once you do, things will get progressively easier as your problem solving skills are sharpened.
but this still doesnt answer my question. Im asking for a way to do it not for someone to tell me to read thousands of words and to figure it out myself.
The easiest way I can think of would be to do something like this:
1
2
3
4
5
cout << "Hello ";
// Set new color here
cout << "World";
// Set back to default color here
cout << "!" << endl;


But that will get very messy very fast. In order to change the color of a specific section within a whole string like "Hello World!" you'll have to create your own function, color tag system, etc. unless you can find something similar designed by someone else, because there's no Windows function to my knowledge which does specifically that. (If someone knows of one I would love to hear about it. xD)

Programming often requires reading thousands of words and figuring things out yourself. If you don't want to do that then I don't know what else to tell you.

ok thanks for your help. If anyone has made a way to do this please reply.
That's just for changing the text color (You might notice I posted SetConsoleTextAttribute() above). That has nothing to do with changing the color of a specific part of a sentence. You won't be able to change colors mid-string. You'll have to end your string, change the color, make a new string, end it, and change the color back.
Last edited on
well isnt that what u showed here:
The easiest way I can think of would be to do something like this:
1
2
3
4
5
cout << "Hello ";
// Set new color here
cout << "World";
// Set back to default color here
cout << "!" << endl;


plus i dont know what SetConsoleTextAttribute() means or how to use it.
Seems like the code is missing some arguments, you need to add a whole argument in order for your code to work.
well isnt that what u showed here


You can do that if you really want to, just be ready for it to get very cluttered.

Anyway, to use that function the first argument is a handle to the console screen buffer, and the second is the actual color attribute linked togethor with bitwise or (the single | ) . You should read up on handles, assuming you don't know much about them, if you do more windows programming. And learning about bitwise operators is useful too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <windows.h>

int main()
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

	std::cout << "Default console color" << std::endl;

	SetConsoleTextAttribute(hConsole, (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_GREEN));
	std::cout << "Bright purple text with dark green cell." << std::endl;

	SetConsoleTextAttribute(hConsole, (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN));
	std::cout << "Back to default console color" << std::endl;

	return 0;
}


The handle is basically a pointer, it's a handle for your program's instance of the console window, and in this case it's to the console's output.

the stuff in all caps is pre-defined by Microsoft, and are combined to make different colors.

Here's a couple links that may help some:
http://msdn.microsoft.com/en-us/library/ms686047(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms682088(v=vs.85).aspx#_win32_character_attributes



Topic archived. No new replies allowed.