Page 1 of 1

Coloured Text in DOS

Posted: Tue Apr 06, 2010 2:56 pm
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;

}

Re: Coloured Text in DOS

Posted: Tue Apr 06, 2010 2:59 pm
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.

Re: Coloured Text in DOS

Posted: Tue Apr 06, 2010 3:02 pm
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:

Re: Coloured Text in DOS

Posted: Tue Apr 06, 2010 5:57 pm
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:

Re: Coloured Text in DOS

Posted: Tue Apr 06, 2010 6:25 pm
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.

Re: Coloured Text in DOS

Posted: Wed Apr 07, 2010 6:45 am
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.