Feb 102009
 

Easy! call SetConsoleTextAttribute function with appropriate color codes as follows…

// A generic function call to set color for text being output
void SetColor( const int Color )
{
   SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), Color );
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
   // Only change foreground color
   SetColor( FOREGROUND_BLUE );
   cout << "Visit http://nibuthomas.wordpress.com"; <<; endl;

   // This time change background color too
   SetColor( FOREGROUND_BLUE|BACKGROUND_RED|BACKGROUND_INTENSITY)
   cout << "Visit http://nibuthomas.wordpress.com" << endl;

   return 0;
}
Share

  3 Responses to “How to change console application text color?”

  1. [...] How to change console application text color? « bits and bytes Easy! call SetConsoleTextAttribute function with appropriate color codes as follows… (tags: color c++ GUI consol) [...]

  2. INvalid unless you state which compiler and where the prototype for SetConsoleTextAttribute(… is Does not work ‘as-is’ in MS Vis C++ 6

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>