Since I brought up C++ the other day I’ve had a few questions in email. Some of the common questions I get revolve around doing more interesting things with the console. By interesting I mean different colors, placing the cursor at specific parts of the screen and the like. You know, all those things we used to do the hard way back in the day before really GUI (Graphical User Interface) objects like we have today with Windows Forms or some of the objects available in Java with Swing. Well I have good news for you. Using a modern C++ compiler like the one in Visual Studio 2005 does not mean you can’t do all that fun stuff.
Today I decided to play with a bit of it. The object you want to use is the Console object and you want to have the IDE create a CLR Console application. The following code is fairly self-explanatory. Console::Title sets the title of the DOS/Console window that is opened. The code in the loop sets the background color to black and prints out two spaces to replace characters in row 10 and position i. Then a sort of arrow (foreground already set to red) is printed on a white background. Then a sleep. Note that you need to add “using namespace System::Threading;” after the line that says “using namespace System;” for the call to the Threading object to work. The
Console::Title = "Rocket Ship?";
Console::CursorTop = 10;
Console::ForegroundColor = ConsoleColor::Red;
for (i=0; i < 76; i++)
Console::BackgroundColor = ConsoleColor::Black;
Console::CursorLeft = i;
Console::BackgroundColor = ConsoleColor::White;
There are other methods available like Clear and Beep and more. Give it a try if you do interesting console applications with your students. Since this Console object is part of .NET you can do the same kind of things in console applications written in C# or Visual Basic as well.