Printing colored text to the console

 

Module Module1

 

    Private Declare Auto Function SetConsoleTextAttribute Lib "kernel32.dll" (ByVal ConsoleOutput As IntPtr, ByVal Attributes As Integer) As Boolean

    Private Declare Auto Function GetStdHandle Lib "kernel32.dll" (ByVal Device As Integer) As Integer

 

    Const GREY As Integer = &H7

    Const WHITE As Integer = &HF

    Const GREEN As Integer = &HA

    Const RED As Integer = &HC

    Const YELLOW As Integer = &HE

 

    Sub Main()

        Print(YELLOW, "This is Yellow")

        Print(WHITE, "This is White")

        Print(GREEN, "This is Green")

        Print(RED, "This is Red")

        Print(GREY, "Press a key to exit...")

        Console.ReadKey()

    End Sub

 

    Private Sub Print(ByVal intColor As Integer, ByVal Text As String)

        Const STD_OUTPUT_HANDLE As Integer = -11

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), intColor)

        Console.WriteLine(Text)

    End Sub

 

End Module

Hit Counter