Coloured Text - Console

How do you colour text in Win32 Console?

Page 1 of 1

4 Replies - 2501 Views - Last Post: 22 September 2008 - 11:57 PM Rate Topic: -----

#1 Shukumei  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 80
  • Joined: 22-September 08

Coloured Text - Console

Post icon  Posted 22 September 2008 - 08:47 PM

Hi, I seem to be having trouble finding a way to color text in my win32 Console program - I know how to change the colour of all the text in the window, how ever I just want to change the colour of specific words.
If anyone can help it would be greatly appreciated:)

Here is a small part of the coding I use to change all color in the window:
Sorry if my coding is sloppy - still learning,

bool Menu()
{
	char cMenu;
	char qMenu;

HANDLE hConsole;
	hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

do
{
Menu:
	system("CLS");
	gotoxy(35,13);
	SetConsoleTextAttribute
	(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
	cout << "Menu\n\n";
	gotoxy(30,15);
	cout << "(S)Start Game\n";



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Coloured Text - Console

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Coloured Text - Console

Posted 22 September 2008 - 09:20 PM

#include <stdio.h>
#include<iostream.h>
#include<conio.h>

int main(void) {
  cout << "regular " << '\033' << '[' << "boldface" << endl;
}


Was This Post Helpful? 0
  • +
  • -

#3 Shukumei  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 80
  • Joined: 22-September 08

Re: Coloured Text - Console

Posted 22 September 2008 - 09:27 PM

Thank you for your reply, however I have tried this approch and it only prints 'regular<[boldface' to the window - no actual change to the text that was already there :(
Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Coloured Text - Console

Posted 22 September 2008 - 11:29 PM

I did something like this once

void ChangeColour(WORD theColour)
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  
    SetConsoleTextAttribute(hConsole,theColour);        
}



Then in your program
int main()
{
   ChangeColour(FOREGROUND_GREEN);
   // codes and printf's etc...

   ChangeColour(FOREGROUND_BLUE);
   // more codes

}




Works for me hope it helped B)
Was This Post Helpful? 0
  • +
  • -

#5 Shukumei  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 80
  • Joined: 22-September 08

Re: Coloured Text - Console

Posted 22 September 2008 - 11:57 PM

Thank you very much it worked wonderfully
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1