Colored Hello World

I just want to know how to make a hello world program display in green, yellow, red, blue, and purple. Thanks for any help!
Last edited on
MSDN link:
http://msdn.microsoft.com/en-us/library/windows/desktop/ff485851(v=vs.85).aspx

i think what you want is something like a tutorial on WinAPI, i took this one myself:
http://www.winprog.org/tutorial/
No not HOW to make hello world! I want to know how to make it in green, yellow, red, blue, and purple! The reason I picked hello world is because I wanted something that everyone would know. I just want to know how to display text in color.
closed account (Dy7SLyTq)
what do you think he linked you to?
can you be more specific please.
is the phrase "Hello World." painted on the client area of a window?
is it written inside an edit-control?
or something else ?
eklavya sharma 2's article might be of interest?

Add color to your console 2
http://www.cplusplus.com/articles/Eyhv0pDG/

Andy
closed account (G309216C)
Hi,

Sorry, I must say that before you post these threads you must learn to do self-research.

As everyone above did offer few solutions which linked to websites. The easiest method is to use the system() function which basically executes shell-commands on the console.
Unlike the simplicity of usage, it will cause undefined behavior on few machines due to local dependencies and more.

Check the presented threads they have everything you need.

GL
Guys,

Please do not use system(). Use this function I use in all my color programs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void ccout(string outputstring, WORD COLOR) 
{
	WORD wOldColorAttrs;
	CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

	/*
	* First save the current color information
	*/
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
	wOldColorAttrs = csbiInfo.wAttributes;
	if (COLOR == 0x00) //pass this value to make ccout output with current color info
	{
		cout << outputstring;
	}
	SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE),COLOR); //Change 
//The color to the specified color
	cout << outputstring;
	SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE),wOldColorAttrs);
//change it back
return;
}


Here is a table of colors and their code:

f = bright white
e = yellow
a = bright green
b = bright blue
c = bright red
d = pink
0 = black
1 = dark blue
2 = green
3 = teal
4 = red
5 = purple
6 = dark yellow or mustard
7 = white
8 = grey
9 = blue

Example:

Blue text background Red = 0x49

I hope this answers your question

EDIT: make sure you include Windows.h and iostream
Last edited on
Thanks for the help guys! I figured it out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <windows.h>
using namespace std;

int main() {

 HANDLE hConsole;
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

int i = 2;
    SetConsoleTextAttribute (hConsole, i);
    cout<<"hello world!";


cin.get();
cin.get();
return 0;

}
so after all you wanted a console app ??
closed account (G309216C)
You see, the stupidity which comes around this forums is incredible.

This thread is a complete waste of time, there are these exact threads littered across the internet, a search away but all he does is post a thread which not only waste's time of members but also spreads junk across the forums.
You see, the stupidity which comes around this forums is incredible.


Yes. We might illustrate it with people who revive day old posts just to say how stupid they are.
Kudos to Modoran, AndyWestken, and Superdude for guessing that Megasnorlax wanted console coloring, I was thinking Windows all the way up to his last post.

@Megasnorlax - For console program questions you will get more relevant answers from either General C++ forums or from Beginner's forum. Windows Programming is a whole different ball-game. :)
Last edited on
closed account (G309216C)
I just wanted to say it because some question are posted which are useless.
@SpaceWorm:
You see, the stupidity which comes around this forums is incredible.

if i may disagree with you.

sometimes a person doesn't know what to type in Google to get the solution he wants.
sometimes a person doesn't even know what is the name of the method he's looking for.
in this case, how can he search for it ?

it won't hurt you if you just tell him how to search and where :)
I did indeed search for how to make text color. None of the things I saw quite matched what I wanted to do (making text in color is actually something I need to know for a larger project), so I decided to make a topic that matched my exact needs.
Topic archived. No new replies allowed.