February 28, 2021
Hot Topics:

Writing Console-Mode Applications in Visual Basic

  • By Scott Lloyd
  • Send Email »
  • More Articles »

The console's title may not always be automatically set to what you would like it to be. This is easily remedied by a call to SetConsoleTitle. If you do not, it is set to the name of the process that created the console window. If you are in the Visual Basic, it sets to "VB6", which almost certainly won't do.

Private Declare Function _
SetConsoleTitle Lib "kernel32" Alias _
"SetConsoleTitleA" (ByVal lpConsoleTitle _
As String) As Long

This is fairly self-explanatory. Notice that this function is one of the very few that don't require you to pass a handle as an argument. All that is passed in this function is a string that contains the new title for the console window.

Making Your Output More Colorful

If you were to just call the WriteConsole function, your output would always appear in the same bland gray text. You can change the color of the text printed by subsequent calls to WriteOutput by calling the SetConsoleTextAttribute function.

Private Declare Function _
SetConsoleTextAttribute Lib "kernel32" _
(ByVal hConsoleOutput As Long, _
ByVal wAttributes As Long) As Long

' These values are passed in the wAttributes
' argument
Private Const FOREGROUND_BLUE = &H1
Private Const FOREGROUND_GREEN = &H2
Private Const FOREGROUND_RED = &H4
Private Const BACKGROUND_BLUE = &H10
Private Const BACKGROUND_GREEN = &H20
Private Const BACKGROUND_RED = &H40

This is a simple function. It takes the console window's output handle and one or more color values. You can combine these values by using Visual Basic's bitwise Or keyword. For example, to create a yellow value, you would define the following constant after the ones listed above.


Other possible colors include cyan, magenta, black, dark-gray, and light-gray (default). Experiment with different combinations using the bitwise Or operator.

Page 4 of 7

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date