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

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

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.

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 "References" section in this article.


Step-by-Step Demonstration

  1. Create a new Visual Basic .NET or Visual Basic 2005 console application.
  2. 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.
  3. 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 "kernel32.dll" (ByVal nStdHandle As Integer) As IntPtr
    Private Declare Auto Function GetConsoleScreenBufferInfo Lib "kernel32.dll" (ByVal hConsoleOutput As IntPtr, _
    ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
    Private Declare Auto Function SetConsoleTextAttribute Lib "kernel32" (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

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure COORD
    Dim X As Short
    Dim Y As Short
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure SMALL_RECT
    Dim Left As Short
    Dim Top As Short
    Dim Right As Short
    Dim Bottom As Short
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    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
  4. 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("Original colors")
    Console.WriteLine("Press ENTER to start")
    Console.ReadLine()
    TextChange.TextColor(Class1.Foreground.Green + Class1.Foreground.Intensity)
    Console.WriteLine("This text is green")
    Console.WriteLine("Press ENTER to change the colors again")
    Console.ReadLine()
    TextChange.TextColor(Class1.Foreground.Red + Class1.Foreground.Blue + Class1.Foreground.Intensity)
    Console.WriteLine("Now the text is purple")
    Console.WriteLine("Press ENTER to change the colors again")
    Console.ReadLine()
    TextChange.TextColor(Class1.Foreground.Blue + Class1.Foreground.Intensity + Class1.Background.Green + Class1.Background.Intensity)
    Console.WriteLine("Now the text is blue and the background is green")
    Console.WriteLine("Press ENTER to restore everything to the original colors")
    Console.ReadLine()
    TextChange.ResetColor()
    Console.WriteLine("Back to the original colors")
    Console.WriteLine("Press ENTER to end")
    Console.ReadLine()
    End Sub
    End Module
  5. Press F5 to compile and run the program.

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.


References

For additional information about console functions, visit the following Microsoft Web site:

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

319239 HOW TO: Clear the Console Window with Visual Basic .NET
Eigenschappen

Artikel-id: 319234 - Laatst bijgewerkt: 26 nov. 2007 - Revisie: 1

Feedback