Imports System
' need this to make API calls
Imports System.Runtime.InteropServices
Namespace PFitzsimons.ConsoleColour
''' <summary>
''' Static class for console colour manipulation.
''' </summary>
Public Class ConsoleColour
' constants for console streams
Const STD_INPUT_HANDLE As Integer = -10
Const STD_OUTPUT_HANDLE As Integer = -11
Const STD_ERROR_HANDLE As Integer = -12
<DllImportAttribute("Kernel32.dll")> _
Private Shared Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
' input, output, or error device
End Function
' handle to screen buffer
<DllImportAttribute("Kernel32.dll")> _
Private Shared Function SetConsoleTextAttribute(ByVal hConsoleOutput As IntPtr, ByVal wAttributes As Integer) As Boolean
' text and background colors
End Function
' colours that can be set
<Flags()> _
Public Enum ForeGroundColour
Black = 0
Blue = 1
Green = 2
Cyan = 3
Red = 4
Magenta = 5
Yellow = 6
Grey = 7
White = 8
End Enum
' class can not be created, so we can set colours
' without a variable
Private Sub New()
End Sub
Public Shared Function SetForeGroundColour() As Boolean
' default to a white-grey
Return SetForeGroundColour(ForeGroundColour.Grey)
End Function
Public Shared Function SetForeGroundColour(ByVal foreGroundColour As ForeGroundColour) As Boolean
' default to a bright white-grey
Return SetForeGroundColour(foreGroundColour, True)
End Function
Public Shared Function SetForeGroundColour(ByVal foreGroundColour As ForeGroundColour, ByVal brightColours As Boolean) As Boolean
' get the current console handle
Dim nConsole As IntPtr = GetStdHandle(STD_OUTPUT_HANDLE)
Dim colourMap As Integer
' if we want bright colours OR it with white
If brightColours Then
colourMap = CInt(foreGroundColour) Or CInt(ForeGroundColour.White)
Else
colourMap = CInt(foreGroundColour)
End If
' call the api and return the result
Return SetConsoleTextAttribute(nConsole, colourMap)
End Function
End Class
End Namespace