Get colors in your VB.NET console application






4.54/5 (12 votes)
Jul 18, 2003

117509

987
Use Windows APIs to write a console tool module, able to change your console background and foreground colors.
Introduction
How can i use colors to make my console application look pretty with VB.NET ? I was writing a remoting server when this idea came ... So i've made this little module ! It was not really difficult to do but i think it could save your time ;)
How to use it ?
You can either change background and foregroud ...
ConsoleTools.SetConsoleColors(ConsoleColor.Green, ConsoleColor.DarkBlue)
... or just foreground :
ConsoleTools.SetConsoleColors(ConsoleColor.Pink)
How does it run ?
I just use kernel api's SetConsoleTextAttribute (to change colors) and GetStHandle (to retrieve console output handles). An enumeration has been created to have bases colors codes : enumeration values must be directly used to change foreground color, and enumeration values * 16 (mask) must be used to change foreground color.
So you just have now to use SetConsoleColors function (which use kernel's api) to change your console colors ...