Coloured Text in DOS

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Coloured Text in DOS

Post by dream_coder »

Hey all, I have found some source code for setting up a function for printing coloured text in a DOS window. However Im unsure about the x, y co-ords.
The function has some parameters passed into it including the x-y coords, the way I want to use this function is like a standard cout, so it just prints certain text in a different colour. I understand this code looking at it with an explanation, however I am unsure about modifying it. I may be jumping the boat a bit here. But if I miss out the x- y coords will it still work. Im gonna try it anyway, but just wondering if an expert can give me some advice.
void DrawColorString(string szText, int X, int Y, WORD color)
{
HANDLE OutputH;
COORD position = {X, Y};

OutputH = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(OutputH, color);

SetConsoleCursorPosition(OutputH, position);

cout << szText;

}
Image
Image
Image
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Coloured Text in DOS

Post by lotios611 »

Code: Select all

void DrawColorString(string szText, WORD color)
{	
	HANDLE OutputH;						

	OutputH = GetStdHandle(STD_OUTPUT_HANDLE);			

	SetConsoleTextAttribute(OutputH, color);			
												
	cout << szText;										

}
This should work, I haven't tested it though.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Re: Coloured Text in DOS

Post by dream_coder »

Please ignore my last I have done it. Really need more faith in myself. Just first time I've changed other peoples functions without knowing weather it will work 100%. I feel quite good about myself. I know it aint much but to me it is, lol. :mrgreen:
Image
Image
Image
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Coloured Text in DOS

Post by eatcomics »

dream_coder wrote:Please ignore my last I have done it. Really need more faith in myself. Just first time I've changed other peoples functions without knowing weather it will work 100%. I feel quite good about myself. I know it aint much but to me it is, lol. :mrgreen:
its the little things that make it all worth while :mrgreen:
Image
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Re: Coloured Text in DOS

Post by dream_coder »

True. Just need to find a way to change the text back to the original color as that function only seems to let me change it to Red, Green or Blue. I have an idea about this. Going to try and implement it tomorrow night, will let you know if its a success or not. Just cant believe Im spending time on changing the colour of text when I could be doing more vital stuff for my Text RPG, still never mind, its a learning experience.
Image
Image
Image
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Re: Coloured Text in DOS

Post by dream_coder »

OK Ive just got the text to change back to its original color. WHich as a bit of a nightmare. I found a tutorial for it on MSDN but it used a totally differnt routine to the once I was using. So I had to bastardise the code a bit to get it to work with my function. Just need to read up a bit on pointers now, as the code uses it and Ive had a complete memory loss about them as it is years since Ive touched them. Also got the "->" which im not sure on. But gonna swot up a bit, as I dont like using code that I dont understand. Gonna post my function in the code snippets section.
Image
Image
Image
Post Reply