Microsoft KB Archive/319234

= How to change the foreground and background text colors in a console window in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 319234

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q319234



For a Microsoft C# .NET version of this article, see 319883.

IN THIS TASK
SUMMARY
 * Overview
 * Step-by-Step Demonstration
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article describes how to change the foreground and background colors of the text that is written to the console window by using Visual Basic .NET or Visual Basic 2005.

back to the top

Overview
To change the foreground and background colors of text that appears in a console program in Visual Basic .NET or in Visual Basic, use the SetConsoleTextAttribute Win32 function. This function sets the attributes of characters that are written to the screen buffer.

If you change these attributes at run time, the changes are valid while the console is open. This means that if you run the program from a command line in a console window that is already running, changes to the text attributes are valid for that console window for as long as the console window is open (even after your program quits). Therefore, a program must restore the original color attributes before the program ends. If you close and then reopen the console window, the attributes are reset to their default values.

This article demonstrates how to save the original settings of the console when the program starts, how to modify those color settings, and how to restore the colors to their original values as the program ends.

You can obtain the text attributes of the console window with the GetConsoleScreenBufferInfo function. This function fills an instance of the CONSOLE_SCREEN_BUFFER_INFO structure with information about the current output buffer information. The wAttribute parameter of this structure contains the color information for that output buffer. You can store this information and use it later to restore the colors: GetConsoleScreenBufferInfo(hConsoleHandle, ConsoleInfo) OriginalColors = ConsoleInfo.wAttributes The class that is used in this example has two methods. The TextColor method takes an integer as its parameter. This parameter contains the information that is used to define the foreground and background text colors. You can use any color combination that you can create by combining red, green, and blue: Public Sub TextColor(ByVal Colors As Integer) SetConsoleTextAttribute(hConsoleHandle, Colors) End Sub You use the ResetColor method to set the console's output buffer attributes back to the original values that you captured when the program began: Public Sub ResetColor SetConsoleTextAttribute(hConsoleHandle, OriginalColors) End Sub For more information about the functions that are used in this example, see the &quot;References&quot; section in this article.

back to the top

Step-by-Step Demonstration
 Create a new Visual Basic .NET or Visual Basic 2005 console application. Add a new class to your program by right-clicking your project in Solution Explorer, and then clicking Add Class on the Add menu.

Note In Visual Studio 2005, click Class on the Add menu.  Paste the following code to create the class. Make sure to replace all of the previous code in the class. Imports System.Runtime.InteropServices

Public Class Class1

Private hConsoleHandle As IntPtr Private ConsoleOutputLocation As COORD Private ConsoleInfo As CONSOLE_SCREEN_BUFFER_INFO Private OriginalColors As Integer

Private Const STD_OUTPUT_HANDLE As Integer = &HFFFFFFF5

Private Declare Auto Function GetStdHandle Lib &quot;kernel32.dll&quot; (ByVal nStdHandle As Integer) As IntPtr Private Declare Auto Function GetConsoleScreenBufferInfo Lib &quot;kernel32.dll&quot; (ByVal hConsoleOutput As IntPtr, _                                                                      ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer Private Declare Auto Function SetConsoleTextAttribute Lib &quot;kernel32&quot; (ByVal hConsoleOutput As IntPtr, ByVal wAttributes As Integer) As Long

Public Enum Foreground Blue = &H1 Green = &H2 Red = &H4 Intensity = &H8 End Enum

Public Enum Background Blue = &H10 Green = &H20 Red = &H40 Intensity = &H80 End Enum

 _ Private Structure COORD Dim X As Short Dim Y As Short End Structure

 _ Private Structure SMALL_RECT Dim Left As Short Dim Top As Short Dim Right As Short Dim Bottom As Short End Structure

 _ Private Structure CONSOLE_SCREEN_BUFFER_INFO Dim dwSize As COORD Dim dwCursorPosition As COORD Dim wAttributes As Integer Dim srWindow As SMALL_RECT Dim dwMaximumWindowSize As COORD End Structure

Sub New hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE) GetConsoleScreenBufferInfo(hConsoleHandle, ConsoleInfo) OriginalColors = ConsoleInfo.wAttributes End Sub

Public Sub TextColor(ByVal Colors As Integer) ' Set the text colors. SetConsoleTextAttribute(hConsoleHandle, Colors) End Sub

Public Sub ResetColor ' Restore the original colors. SetConsoleTextAttribute(hConsoleHandle, OriginalColors) End Sub

End Class   Paste the following code in your module. Make sure to replace all of the previous code in the module. Module Module1 Sub Main Dim TextChange As New Class1 Console.WriteLine(&quot;Original colors&quot;) Console.WriteLine(&quot;Press ENTER to start&quot;) Console.ReadLine TextChange.TextColor(Class1.Foreground.Green + Class1.Foreground.Intensity) Console.WriteLine(&quot;This text is green&quot;) Console.WriteLine(&quot;Press ENTER to change the colors again&quot;) Console.ReadLine TextChange.TextColor(Class1.Foreground.Red + Class1.Foreground.Blue + Class1.Foreground.Intensity) Console.WriteLine(&quot;Now the text is purple&quot;) Console.WriteLine(&quot;Press ENTER to change the colors again&quot;) Console.ReadLine TextChange.TextColor(Class1.Foreground.Blue + Class1.Foreground.Intensity + Class1.Background.Green + Class1.Background.Intensity) Console.WriteLine(&quot;Now the text is blue and the background is green&quot;) Console.WriteLine(&quot;Press ENTER to restore everything to the original colors&quot;) Console.ReadLine TextChange.ResetColor Console.WriteLine(&quot;Back to the original colors&quot;) Console.WriteLine(&quot;Press ENTER to end&quot;) Console.ReadLine End Sub End Module  Press F5 to compile and run the program.</li></ol>

back to the top

Troubleshooting
Make sure to pass a valid value as the parameter for the TextColor method. You can create a valid value by combining the values that you obtained from the Foreground and Background enumerations as they are defined in the class.

back to the top

<div class="references_section">