Home > Windows API > How to change console application text color?

How to change console application text color?

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 https://nibuthomas.wordpress.com"; <<; endl; // This time change background color too SetColor( FOREGROUND_BLUE|BACKGROUND_RED|BACKGROUND_INTENSITY) cout << "Visit https://nibuthomas.wordpress.com" << endl; return 0; }[/sourcecode]

Advertisements
  1. Will Cooper
    March 31, 2009 at 2:21 am

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

    • Satish Singhal
      May 31, 2009 at 11:07 pm

      The header file to include will be windows.h

  1. February 19, 2009 at 11:01 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: