News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Miscellaneous Forum
 
ConsoleAttribute class InputMode addition

Reply

by Clayton Gulick
posted on 2003/12/18

I added some additional functionality to the console input class. I added support for InputMode which will allow you to change from the default line mode input to character mode input. This means you can read from the console character by character without having to wait for a carriage return.

vb class follows:

Option Strict
Option Explicit

'
' ConsoleAttributes class
' written in VB.NET Version: 1.0
' by The KPD-Team Date: 2002/01/16
' Copyright © 2002 Comments to: KPDTeam@allapi.net
' URL: http://www.allapi.net/
'
'
' You are free to use this class file in your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this code as source without prior written consent.
' This includes both posting samples on a web site or otherwise
' reproducing it in text or html format.
'
' Although much care has gone into the programming of this class
' file, The KPD-Team does not accept any responsibility for damage
' caused by possible errors in this class and/or by misuse of this
' class.
'

Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Imports Microsoft.VisualBasic

'/// <summary>The ConsoleAttributes class can change several attributes of your console window.</summary>
'/// <remarks>This class uses several Win32 API functions. Therefore, it requires at least Windows 95 or WindowsNT 3.1.</remarks>
'/// <example>
'/// The following example wil change the forecolor of te console, disable 'EchoInput', ask for a string and show that string.
'/// </example>
'/// <code>
'/// ConsoleAttributes.ForeColor = ConsoleAttributes.ConsoleColor.White
'/// Console.Write("Please enter your password: ")
'/// ConsoleAttributes.EchoInput = False
'/// Dim ThePass as String = Console.ReadLine()
'/// ConsoleAttributes.EchoInput = True
'/// ConsoleAttributes.ForeColor = ConsoleAttributes.ConsoleColor.Gray
'/// Console.WriteLine("")
'/// Console.WriteLine("The password you entered was: " & ThePass)
'/// </code>
Public Class ConsoleAttributes
<StructLayout(LayoutKind.Sequential)> _
Private Structure CONSOLE_CURSOR_INFO
Public dwSize As Integer
Public bVisible As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure COORD
Public x As Short
Public y As Short
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure SMALL_RECT
Public Left As Short
Public Top As Short
Public Right As Short
Public Bottom As Short
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure CONSOLE_SCREEN_BUFFER_INFO
Public dwSize As COORD
Public dwCursorPosition As COORD
Public wAttributes As Short
Public srWindow As SMALL_RECT
Public dwMaximumWindowSize As COORD
End Structure
' <API-DECLARES>
<DllImport("KERNEL32.DLL", EntryPoint:="SetConsoleTextAttribute", SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function SetConsoleTextAttribute (ByVal hConsoleOutput As Integer, ByVal wAttributes As Integer) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="GetStdHandle", SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetStdHandle (ByVal nStdHandle As Integer) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="SetConsoleCursorInfo", SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function SetConsoleCursorInfo (ByVal hConsoleOutput As Integer, ByRef lpConsoleCursorInfo as CONSOLE_CURSOR_INFO) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="GetConsoleMode", SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetConsoleMode (ByVal hConsoleHandle As Integer, ByRef lpConsoleCursorInfo as Integer) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="SetConsoleMode", SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function SetConsoleMode (ByVal hConsoleHandle As Integer, ByVal lpConsoleCursorInfo as Integer) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="SetConsoleTitleA", CharSet:=CharSet.ANSI, SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function SetConsoleTitle (ByVal lpConsoleTitle as String) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="GetConsoleTitleA", CharSet:=CharSet.ANSI, SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetConsoleTitle (ByVal lpConsoleTitle as StringBuilder, ByVal nSize As Integer) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="GetConsoleScreenBufferInfo", SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetConsoleScreenBufferInfo (ByVal hConsoleOutput as Integer, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="SetConsoleCursorPosition", SetLastError:=False, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function SetConsoleCursorPosition (ByVal hConsoleOutput as Integer, ByVal dwCursorPosition As COORD) As Integer
End Function
' </API-DECLARES>
Private Shared BacgroundColors() as Integer = {&H0, &H40, &H80 Or &H40, &H20, &H80 Or &H20, &H10, &H80 Or &H10, &H40 Or &H20, &H80 Or &H40 Or &H20, &H20 Or &H10, &H80 Or &H20 Or &H10, &H40 Or &H10, &H80 Or &H40 Or &H10, &H40 Or &H20 Or &H10, &H80 Or &H40 Or &H20 Or &H10}
Private Shared ForegroundColors() as Integer = {&H0, &H4, &H8 Or &H4, &H2, &H8 Or &H2, &H1, &H8 Or &H1, &H4 Or &H2, &H8 Or &H4 Or &H2, &H2 Or &H1, &H8 Or &H2 Or &H1, &H4 Or &H1, &H8 Or &H4 Or &H1, &H4 Or &H2 Or &H1, &H8 Or &H4 Or &H2 Or &H1}
'/// <summary>Enumerates all available colors for the forecolor or the backcolor of the console.</summary>
Public Enum ConsoleColor
Black
Red
LightRed
Green
LightGreen
Blue
LightBlue
Gold
Yellow
Cyan
LightCyan
DarkPurple
LightPurple
Gray
White
End Enum

Public Enum InputModeEnum
LINE
CHARACTER
End Enum

''' -----------------------------------------------------------------------------
''' <summary>
''' Returns or sets the input mode of the console. Default console behavior is
''' line input
''' </summary>
''' <value>A member of the InputModeEnum enumeration</value>
''' <remarks>
''' User code should be sure to reset the input mode back to line before exiting
''' </remarks>
''' <history>
''' [clayguli] 12/18/2003 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Shared Property InputMode() As InputModeEnum
Get
Dim Ret As Integer
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), Ret)
If (Ret And ENABLE_LINE_INPUT) > 0 Then
Return InputModeEnum.LINE
Else
Return InputModeEnum.CHARACTER
End If
End Get
Set(ByVal Value As InputModeEnum)

Dim Ret As Integer
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), Ret)
If Value = InputModeEnum.LINE Then
Ret = Ret Or ENABLE_LINE_INPUT
If m_EchoInput = True Then
Ret = Ret Or ENABLE_ECHO_INPUT
End If
Else
Ret = Ret And Not ENABLE_LINE_INPUT
'must disable echo input for char mode
Ret = Ret And Not ENABLE_ECHO_INPUT
End If
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), Ret)

End Set
End Property

'/// <summary>Specifies the <see cref ="ConsoleColor">color</see> of the console font.</summary>
'/// <value>The color of the font.</value>
Public Shared Property ForeColor() As ConsoleColor
Get
Return m_ForeColor
End Get
Set(ByVal Value As ConsoleColor)
m_ForeColor = Value
Try
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForegroundColors(Value) Or BacgroundColors(BackColor))
Catch
End Try
End Set
End Property
'/// <summary>Specifies the <see cref ="ConsoleColor">color</see> of the console background.</summary>
'/// <value>The color of the background.</value>
Public Shared Property BackColor() As ConsoleColor
Get
Return m_BackColor
End Get
Set(ByVal Value As ConsoleColor)
m_BackColor = Value
Try
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForegroundColors(ForeColor) Or BacgroundColors(Value))
Catch
End Try
End Set
End Property
'/// <summary>Specifies whether the cursor is visible or not.</summary>
'/// <value>A <see cref ="Boolean">Boolean</see> value that specifies the visibility of the cursor.</value>
Public Shared Property CursorVisible() As Boolean
Get
Return m_CursorVisible
End Get
Set(ByVal Value As Boolean)
m_CursorVisible = Value
ChangeCursor()
End Set
End Property
'/// <summary>Specifies whether the cursor is in overwrite-mode or not.</summary>
'/// <value>A <see cref ="Boolean">Boolean</see> value that specifies the mode of the cursor.</value>
'/// <remarks>In overwrite mode, the cursor size will be 50% of the character space instead of 25% in normal mode</remarks>
Public Shared Property OvrMode() As Boolean
Get
Return m_OvrMode
End Get
Set(ByVal Value As Boolean)
m_OvrMode = Value
ChangeCursor()
End Set
End Property
'/// <summary>Applies the current cursor settings.</summary>
Private Shared Sub ChangeCursor()
Dim CCI As CONSOLE_CURSOR_INFO
CCI.bVisible = System.Convert.ToInt32(CursorVisible)
CCI.dwSize = CType(IIf(OvrMode, 50, 25), Integer)

Try
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), CCI)
Catch
End Try
End Sub
'/// <summary>Specifies whether the console must echo the input or not.</summary>
'/// <value>A <see cref ="Boolean">Boolean</see> value that specifies the console must echo the input or not.</value>
'/// <remarks>EchoInput is often turned off when the program asks the user to type in a password.</remarks>
Public Shared Property EchoInput() As Boolean
Get
Return m_EchoInput
End Get
Set(ByVal Value As Boolean)
m_EchoInput = Value
Dim Ret As Integer
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), Ret)
If EchoInput Then
Ret = Ret Or ENABLE_ECHO_INPUT
Else
Ret = Ret And Not ENABLE_ECHO_INPUT
End If
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), Ret)
End Set
End Property
'/// <summary>Specifies the caption of the console.</summary>
'/// <value>A <see cref ="String">String</see> value that specifies the caption of the console.</value>
Public Shared Property Caption() As String
Get
Dim sb As New StringBuilder(256)
GetConsoleTitle(sb, 256)
Return sb.ToString
End Get
Set(ByVal Value As String)
SetConsoleTitle(Value)
End Set
End Property
'/// <summary>Specifies the current cursos position on the x axis in the console.</summary>
'/// <value>A <see cref ="Int16">Short</see> value that specifies the current cursos position on the x axis in the console.</value>
Public Shared Property CursorX() As Short
Get
Dim SBI As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), SBI)
Return SBI.dwCursorPosition.x
End Get
Set(ByVal Value As Short)
MoveCursor(Value, CursorY)
End Set
End Property
'/// <summary>Specifies the current cursos position on the y axis in the console.</summary>
'/// <value>A <see cref ="Int16">Short</see> value that specifies the current cursos position on the y axis in the console.</value>
Public Shared Property CursorY() As Short
Get
Dim SBI As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), SBI)
Return SBI.dwCursorPosition.y
End Get
Set(ByVal Value As Short)
MoveCursor(CursorX, Value)
End Set
End Property
'/// <summary>Moves the cursor to the specified location.</summary>
'/// <param name="x">Specifies the x value of the new location.</param>
'/// <param name="y">Specifies the y value of the new location.</param>
Public Shared Sub MoveCursor(ByVal x As Short, ByVal y As Short)
Dim Crd As COORD
Crd.x = x
Crd.y = y
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Crd)
End Sub
'/// <summary>Specifies the width (in characters) of the console window.</summary>
'/// <value>A <see cref ="Int32">Integer</see> value that specifies the width of the console window in characters.</value>
Public Shared ReadOnly Property WindowWidth() As Integer
Get
Dim SBI As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), SBI)
Return SBI.srWindow.Right - SBI.srWindow.Left + 1
End Get
End Property
'/// <summary>Specifies the height (in characters) of the console window.</summary>
'/// <value>A <see cref ="Int32">Integer</see> value that specifies the height of the console window in characters.</value>
Public Shared ReadOnly Property WindowHeight() As Integer
Get
Dim SBI As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), SBI)
Return SBI.srWindow.Bottom - SBI.srWindow.Top + 1
End Get
End Property
'Private constants
Private Const STD_OUTPUT_HANDLE As Integer = -11
Private Const STD_INPUT_HANDLE As Integer = -10
Private Const STD_ERROR_HANDLE As Integer = -12
Private Const ENABLE_ECHO_INPUT As Integer = &H4
Private Const ENABLE_LINE_INPUT As Integer = &H2


'Private variables
Private Shared m_ForeColor As ConsoleColor = ConsoleColor.Gray
Private Shared m_BackColor As ConsoleColor = ConsoleColor.Black
Private Shared m_CursorVisible As Boolean = True
Private Shared m_OvrMode As Boolean = False
Private Shared m_EchoInput As Boolean = True
End Class

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2003/12/18
Reply

Thanks, I've uploaded the new version of the class here: http://www.mentalis.org/soft/class.qpx?id=2

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.