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

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

IN THIS TASK

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

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to
    New, and then click Project.
  3. 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.
  4. In the Name text box, type
    ChangingColour.
  5. Open the ChangingColour.cpp file, and then replace the existing code with following code:
    #include "stdafx.h"
    #include "Windows.h"

    #using <mscorlib.dll>

    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"Original Colors");
    Console::WriteLine(S"Press Enter to Begin");
    Console::ReadLine();

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

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

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

    SetConsoleTextAttribute(hConsoleHandle, OriginalColors);
    Console::WriteLine(S"Back to Original Colors");
    Console::WriteLine(S"Press Enter to Terminate");
    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:

    1. Click Project, and then click <ProjectName> Properties.



      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.

    3. 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.

    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
  6. Press CTRL + F5 to build and to run the application.
back to the top

References

For more information about console functions, visit the following Microsoft Web site:back to the top
Propriedades

ID do Artigo: 816179 - Última Revisão: 26 de nov de 2007 - Revisão: 1

Comentários