Microsoft KB Archive/816179

= How to change the foreground and the background text colors in a Console window by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 816179

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







For a Microsoft Visual Basic .NET version of this article, see 319234.

IN THIS TASK

 * SUMMARY
 * Introduction
 * Step-by-Step Example
 * REFERENCES



SUMMARY
This step-by-step article describes how to change the foreground colors and the background colors of the text that is written to the Console window by using Visual C++ .NET or Visual C++ 2005. This article describes how to save the original settings of the Console window while the program starts, how to modify the color settings, and how to restore the colors to their original values while the program quits.

back to the top

Introduction
To change the foreground colors and the background colors of the text that the Console window displays, use the SetConsoleTextAttribute Win32 application function. This function sets the attributes of the characters that are written to the screen buffer.

When you change these attributes at run time, the changes are valid as long as the Console window is open. If you close and reopen the Console window, the attributes are reset to their default values. If you run the program from a command line in a Console window that is already running, changes that you make to the text attributes are valid for that Console window as long as the window is open, even after your program quits. Therefore, the program restores the original color attributes before the program quits.

You can obtain the text attributes of the Console window by using the GetConsoleScreenBufferInfo function. This function fills an instance of the CONSOLE_SCREEN_BUFFER_INFO structure with information about the current output buffer settings. The wAttribute parameter of this structure contains the color information that defines the foreground colors and the background colors of the text. The possible colors are any color combination that you can create by combining red, green, and blue. OriginalColors = ConsoleInfo->wAttributes; SetConsoleTextAttribute(hConsoleHandle, color); You can use the SetConsoleTextAttribute method to reset the output buffer attributes of the Console window to the original values that are captured when the program starts running. SetConsoleTextAttribute(hConsoleHandle, OriginalColors); back to the top

Step-by-Step Example
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In Visual C++ .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.

In Visual C++ .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.

In Visual Studio 2005, click Visual C++ under Project Types, and then click CLR Console Application under Templates. In the Name text box, type ChangingColour .  Open the ChangingColour.cpp file, and then replace the existing code with following code:
 * 1) include &quot;stdafx.h&quot;
 * 2) include &quot;Windows.h&quot;


 * 1) using 

using namespace System;

int main {   HANDLE hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_SCREEN_BUFFER_INFO *ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO; GetConsoleScreenBufferInfo(hConsoleHandle, ConsoleInfo); WORD OriginalColors = ConsoleInfo->wAttributes;

Console::WriteLine(S&quot;Original Colors&quot;); Console::WriteLine(S&quot;Press Enter to Begin&quot;); Console::ReadLine;

SetConsoleTextAttribute(hConsoleHandle,FOREGROUND_GREEN); Console::WriteLine(S&quot;THIS TEXT IS GREEN&quot;); Console::WriteLine(S&quot;Press Enter to change colors again&quot;); Console::ReadLine;

SetConsoleTextAttribute(hConsoleHandle,FOREGROUND_RED); Console::WriteLine(S&quot;NOW THE TEXT IS RED&quot;); Console::WriteLine(S&quot;Press Enter to change colors again&quot;); Console::ReadLine;

SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE |FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_INTENSITY); Console::WriteLine(S&quot;NOW THE TEXT IS BLUE AND BACKGROUND OF IT IS GREEN&quot;); Console::WriteLine(S&quot;Press Enter change everything back to normal&quot;); Console::ReadLine;

SetConsoleTextAttribute(hConsoleHandle, OriginalColors); Console::WriteLine(S&quot;Back to Original Colors&quot;); Console::WriteLine(S&quot;Press Enter to Terminate&quot;); Console::ReadLine;

return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:  Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> Press CTRL + F5 to build and to run the application.</li></ol>

back to the top

<div class="references_section">