65.9K
CodeProject is changing. Read more.
Home

Get colors in your VB.NET console application

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.54/5 (12 votes)

Jul 18, 2003

viewsIcon

117509

downloadIcon

987

Use Windows APIs to write a console tool module, able to change your console background and foreground colors.

Sample Image - color_console.jpg

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 ...