Posts Tagged ‘Console Application’

How to change console application text color?

February 10, 2009 3 comments

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

Changing background color of a console application?

August 16, 2008 13 comments

Changing background color of a console application can be accomplished in two ways…

  1. Through console application API’s provided by Microsoft.
  2. Through color command in windows.

The second one is the easiest, let me show you how to do so…

// This will change the background to crimson and foreground color to white
const int NotUsed = system( "color 4F" );
int main()
  // Some code...
  return 0;

The color command works likewise. The first character, here ‘4’, is the background color and the second character, here ‘F’, is the foreground color. Both background and foreground can range from 0 to F.

Also note that to change only the foreground color call color command with one argument. system( “color A” ) call will change only the foreground color to “Bright green”. Note that if background color has been changed to something else other that the default one when your application was started, then background color on execution of above function call will default to that color.

Easiest way to try out the color command will be to use the command prompt, so open cmd and type in…

color 0F  -> Black background and white foreground

color 2A -> Dark green background and light green foreground

Further the help for the color command is available in windows and will be sufficient to help you out as its quite detailed, here is it…

C:\SomeUser>color /?
Sets the default console foreground and background colors.
COLOR [attr]
  attr        Specifies color attribute of console output

Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground.  Each digit
can be any of the following values:
    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White
If no argument is given, this command restores the color to what it was
when CMD.EXE started.  This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the same.

gotoxy in a console application in windows

August 7, 2008 Leave a comment

gotoxy! what’s that?

gotoxy was one of my favorite functions in turbo c++, it moves the input cursor in a console application/MS-DOS application from one location to another.

So is there an equivalent function in a windows console application which does the same? Yes, it’s called SetConsoleCursorPosition.

Sample code

As always with me here is a function which does this!

BOOL gotoxy( const WORD x, const WORD y )
   COORD xy;
   xy.X = x;
   xy.Y = y;
   return SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), xy );
int main()
   if( !gotoxy( 10, 10 ))
      std::cout < < "gotoxy failed :(, seems like coords are too big\n";       return 1;    }    return 0; }[/sourcecode] Result of calling "gotoxy", see how the pause message has shifted down and to the right.

Screenshot for gotoxy

Note that our gotoxy can fail if x or y is too big. A post from me will follow soon based on this function. It’s going to be about displaying a spining stick at the end of a progress message. You might have seen it if you have installed Linux.

Change console application title!

September 5, 2007 Leave a comment

It’s easy to change the title of a console application, just call SetConsoleTitle with the new title.


int main( int argc, char** argv)
     SetConsoleTitle("Nibu's console");
     return 0;

Well now the title changes, but you can see the old title for a moment before it changes to the new one (well at least I saw 😉 ), we can fix this problem by calling SetConsoleTitle before main is invoked.


// Change console title here
const int nNotUsed = SetConsoleTitle( "Nibu's console");  

int main( int argc, char** argv)
     return 0;