PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VB.NET - extended console library-VBForums
Results 1 to 4 of 4

Thread: VB.NET - extended console library

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    VB.NET - extended console library

    I just made this a while ago to use for one of my programs. I'd posted it in the VB.net section but I thought it's good to post it here also. The code isn't 100% mine... I used some C# projects and some old vb6 projects and converted them to vb.net.... sorry cant remember what and which parts though

    I'm copy pasting this from this post in the vb.net section:

    Lets you:
    -change the foreground or background color of the text
    -change the background color of the console (call SetCLSColor, and then the Clear function)
    -Change or read the title of the console
    -clear a console window
    -change the input mode of the console (call SetMode - whether it should read the input line by line, or to read every key stroke one by one)
    -move the cursor to any random position
    -enable/disable showing what the user types in the console ( call EchoInput )
    -get the size of the console window (number of columns and rows)
    -get the current position of the cursor
    -change the cursor type (the carret)
    -close the console whenever you want (FreeConsole() )
    -create a console by calling this class

    There are two classes, one is just a helper class to call the windows api functions, the other is the console class. I'll post them as separate posts since the code is long.

    -see next 2 posts for code.
    -see 3rd post for a sample.


    See the sample project if you want to know how to use this class library. (Remember that you have to add a reference to ConsoleEx.dll to your project before you can use it!)

    Extended console - source code: class library project
    Extended console - dll file only
    sample project (includes the class library project)
    Last edited by MrPolite; Jun 5th, 2005 at 04:56 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Main class, ConsoleEx

    VB Code:
    1. ' Please do not remove :)
    2. ' Written by Kourosh Derakshan
    3. '
    4. Option Strict On
    5. Imports System.Runtime.InteropServices
    6. Imports System.Text
    7.  
    8. Public NotInheritable Class ConsoleEx
    9. #Region " Declarations "
    10.         Private hConsoleIn, hConsoleOut As IntPtr
    11.         Private conInfo As Win32Native.CONSOLE_INFO
    12.         Private cursorInfo As Win32Native.CURSOR_INFO
    13.         Private backColor As Integer
    14.         Private backgroundAttrib As Short
    15.  
    16.  
    17.         Enum InputMode
    18.             LineInput
    19.             EchoInput
    20.         End Enum
    21.  
    22.         Public Enum ConsoleColor
    23.             Black = 0
    24.             Blue = Win32Native.FOREGROUND_BLUE
    25.             Green = Win32Native.FOREGROUND_GREEN
    26.  
    27.             SkyBlue = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN
    28.  
    29.             Red = Win32Native.FOREGROUND_RED
    30.             Purple = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_RED
    31.             Brown = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_RED
    32.             White = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN + _
    33.                     Win32Native.FOREGROUND_RED
    34.             Gray = Win32Native.FOREGROUND_INTENSIFY
    35.             BlueForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_INTENSIFY
    36.             GreenForte = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_INTENSIFY
    37.             SkyBlueForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN + _
    38.                            Win32Native.FOREGROUND_INTENSIFY
    39.             RedForte = Win32Native.FOREGROUND_RED + Win32Native.FOREGROUND_INTENSIFY
    40.             PurpleForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_RED + _
    41.             Win32Native.FOREGROUND_INTENSIFY
    42.             Yellow = Win32Native.FOREGROUND_GREEN + Win32Native.FOREGROUND_RED + _
    43.             Win32Native.FOREGROUND_INTENSIFY
    44.             WhiteForte = Win32Native.FOREGROUND_BLUE + Win32Native.FOREGROUND_GREEN + _
    45.                          Win32Native.FOREGROUND_RED + Win32Native.FOREGROUND_INTENSIFY
    46.         End Enum
    47.  
    48.         Public Enum CursorType
    49.             Off
    50.             SingleLine
    51.             Block
    52.         End Enum
    53. #End Region
    54.  
    55.         Public Sub New()
    56.             ' Creates a console if there isn't already one loaded.
    57.             Win32Native.AllocConsole()
    58.  
    59.             ' Set the input handle
    60.             hConsoleIn = Win32Native.GetStdHandle(Win32Native.STD_INPUT_HANDLE)
    61.             ' Set the output handle
    62.             hConsoleOut = Win32Native.GetStdHandle(Win32Native.STD_OUTPUT_HANDLE)
    63.  
    64.  
    65.             ' Fill in the console information to conInfo
    66.             conInfo = New Win32Native.CONSOLE_INFO
    67.             updateConsoleInfo()
    68.  
    69.             ' Fill in the cursor information for this console
    70.             cursorInfo = New Win32Native.CURSOR_INFO
    71.             SetCursorType(CursorType.SingleLine)
    72.  
    73.             ' Set the standard background attribute
    74.             backgroundAttrib = CShort((CShort(ConsoleColor.Black) * &H10 + CShort(ConsoleColor.White)))
    75.         End Sub
    76.  
    77.         Protected Overrides Sub Finalize()
    78.             MyBase.Finalize()
    79.             FreeConsole()
    80.         End Sub
    81.  
    82.         Public Property Title() As String
    83.             Get
    84.                 Dim buffer As New StringBuilder(128)
    85.                 Win32Native.GetConsoleTitle(buffer, 128)
    86.                 Return buffer.ToString
    87.             End Get
    88.  
    89.             Set(ByVal Value As String)
    90.                 Win32Native.SetConsoleTitle(Value)
    91.             End Set
    92.         End Property
    93.  
    94.         Public ReadOnly Property Columns() As Integer
    95.             Get
    96.                 Return conInfo.MaxSize.x
    97.             End Get
    98.         End Property
    99.  
    100.         Public ReadOnly Property Rows() As Integer
    101.             Get
    102.                 Return conInfo.MaxSize.y
    103.             End Get
    104.         End Property
    105.  
    106.         ' X position of the cursor (or the carret) on the screen
    107.         Public ReadOnly Property CursorX() As Integer
    108.             Get
    109.                 updateConsoleInfo()
    110.                 Return conInfo.CursorPosition.x
    111.             End Get
    112.         End Property
    113.  
    114.         ' Y position of the cursor (or the carret) on the screen
    115.         Public ReadOnly Property CursorY() As Integer
    116.             Get
    117.                 updateConsoleInfo()
    118.                 Return conInfo.CursorPosition.y
    119.             End Get
    120.         End Property
    121.  
    122.         ' LineInput will let the console read a line at a time. EchoInput lets the console
    123.         ' read each key stroke, as the user types something (You have to use Console.Read ()
    124.         ' instead of Console.ReadLine() when in EchoInput.)
    125.         Public Sub SetMode(ByVal mode As InputMode)
    126.             Dim conMode As Integer
    127.             Win32Native.GetConsoleMode(hConsoleIn, conMode)
    128.  
    129.             If mode = InputMode.EchoInput Then
    130.                 conMode = conMode And Not (Win32Native.ENABLE_LINE_INPUT Or Win32Native.ENABLE_ECHO_INPUT)
    131.             Else
    132.                 conMode = conMode Or (Win32Native.ENABLE_LINE_INPUT Or Win32Native.ENABLE_ECHO_INPUT)
    133.             End If
    134.             Win32Native.SetConsoleMode(hConsoleIn, conMode)
    135.         End Sub
    136.  
    137.         Public Sub Clear()
    138.             Dim written As Integer = 0
    139.             Dim startCoord As New Win32Native.COORD
    140.             startCoord.x = 0 : startCoord.y = 0
    141.  
    142.             Win32Native.FillConsoleOutputCharacter(hConsoleOut, " "c, conInfo.MaxSize.x * conInfo.MaxSize.y, startCoord, written)
    143.             Win32Native.FillConsoleOutputAttribute(hConsoleOut, backgroundAttrib, conInfo.MaxSize.x * conInfo.MaxSize.y, startCoord, written)
    144.  
    145.             MoveCursor(1, 1)
    146.         End Sub
    147.  
    148.         ' Use only if the console is in LineInput mode. If value is true, then the things
    149.         ' that the user types will automatically show up in the console. (In EchoInput mode
    150.         ' nothing shows up automatically)
    151.         Public Sub EchoInput(ByVal value As Boolean)
    152.             Dim Ret As Integer
    153.             Win32Native.GetConsoleMode(hConsoleIn, Ret)
    154.             If value Then
    155.                 Ret = Ret Or (Win32Native.ENABLE_ECHO_INPUT)
    156.             Else
    157.                 Ret = Ret And Not (Win32Native.ENABLE_ECHO_INPUT)
    158.             End If
    159.             Win32Native.SetConsoleMode(hConsoleIn, Ret)
    160.         End Sub
    161.  
    162.         Public Sub SetColor(ByVal foreColor As ConsoleColor, ByVal backColor As ConsoleColor)
    163.             Me.backColor = CInt(backColor)
    164.             SetColor(foreColor)
    165.         End Sub
    166.  
    167.         Public Sub SetColor(ByVal foreColor As ConsoleColor)
    168.             Win32Native.SetConsoleTextAttribute(hConsoleOut, CInt(foreColor) + 16 * backColor)
    169.         End Sub
    170.  
    171.         Public Sub SetClsColor(ByVal backColor As ConsoleColor)
    172.                backgroundAttrib = CShort(backColor) * &H10S
    173.         End Sub
    174.  
    175.         Public Sub MoveCursor(ByVal x As Integer, ByVal y As Integer)
    176.             conInfo.CursorPosition.x = CShort(x - 1)
    177.             conInfo.CursorPosition.y = CShort(y - 1)
    178.             If cursorInfo.Visible Then
    179.                 Dim coord As Integer = conInfo.CursorPosition.x + conInfo.CursorPosition.y * &H10000
    180.                 Win32Native.SetConsoleCursorPosition(hConsoleOut, coord)
    181.             End If
    182.         End Sub
    183.  
    184.  
    185.         ' Change the cursor's shape and visibility state.
    186.         Public Sub SetCursorType(ByVal newType As CursorType)
    187.             '  Change the cursor type.
    188.             Select Case newType
    189.                 Case CursorType.Block
    190.                     cursorInfo.Size = 100
    191.                     cursorInfo.Visible = True
    192.  
    193.                 Case CursorType.SingleLine
    194.                     cursorInfo.Size = 10
    195.                     cursorInfo.Visible = True
    196.  
    197.                 Case CursorType.Off
    198.                     cursorInfo.Size = 100
    199.                     cursorInfo.Visible = False
    200.             End Select
    201.  
    202.             Win32Native.SetConsoleCursorInfo(hConsoleOut, cursorInfo)
    203.  
    204.             ' Move the cursor to its correct position.
    205.             MoveCursor(conInfo.CursorPosition.x, conInfo.CursorPosition.y)
    206.         End Sub
    207.  
    208.         ' Forces to close the console. Don't call if not necessary.
    209.         Public Sub FreeConsole()
    210.             ' I just added these myself. I don't if I really have to do this, but I thought
    211.             ' they are necessary. That's why it's in a Try-Catch block, because I don't
    212.             ' know if it may cause an error or not. But it shouldn't.
    213.             Try
    214.                 Win32Native.FreeConsole()
    215.                 Win32Native.CloseHandle(hConsoleIn)
    216.                 Win32Native.CloseHandle(hConsoleOut)
    217.                 Win32Native.FreeConsole()
    218.             Catch
    219.             End Try
    220.         End Sub
    221.  
    222.  
    223.         Private Sub updateConsoleInfo()
    224.             ' Fill in the console information to conInfo
    225.             Win32Native.GetConsoleScreenBufferInfo(hConsoleOut, conInfo)
    226.         End Sub
    227.     End Class
    Last edited by MrPolite; Jun 5th, 2005 at 04:42 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    helper class, Win32Native

    this class is used by the ConsoleEx class (previous post)

    VB Code:
    1. ' Please do not remove :)
    2. ' Written by Kourosh Derakshan
    3. '
    4. Friend Class Win32Native
    5.         ' Input and output handle
    6.         Friend Const STD_OUTPUT_HANDLE As Integer = -11&
    7.         Friend Const STD_INPUT_HANDLE As Integer = -10&
    8.         ' Echo Input would let the program to receive each key stroke.
    9.         Friend Const ENABLE_LINE_INPUT As Short = &H2S
    10.         Friend Const ENABLE_ECHO_INPUT As Short = &H4S
    11.  
    12.         ' Color codes
    13.         Friend Const FOREGROUND_BLUE As Integer = &H1&
    14.         Friend Const FOREGROUND_GREEN As Integer = &H2&
    15.         Friend Const FOREGROUND_RED As Integer = &H4&
    16.         Friend Const FOREGROUND_INTENSIFY As Integer = &H8&
    17.         Friend Const BACKGROUND_BLUE As Integer = &H10&
    18.         Friend Const BACKGROUND_GREEN As Integer = &H20&
    19.         Friend Const BACKGROUND_INTENSIFY As Integer = &H80&
    20.  
    21.  
    22.  
    23.         Public Enum BACKGROUNDCOLOR
    24.             bBLUE = &H10&   'BACKGROUND_BLUE
    25.             bGREEN = &H20&  'BACKGROUND_GREEN
    26.             bRED = &H40&    'BACKGROUND_RED
    27.             bBRIGHT = &H80& 'BACKGROUND_INTENSITY
    28.         End Enum
    29.  
    30.         <StructLayout(LayoutKind.Sequential)> _
    31.         Friend Structure COORD
    32.             Friend x, y As Short
    33.         End Structure
    34.  
    35.         <StructLayout(LayoutKind.Sequential)> _
    36.         Friend Structure RECT
    37.             Dim Left, Top, Right, Bottom As Short
    38.         End Structure
    39.  
    40.         <StructLayout(LayoutKind.Sequential)> _
    41.         Friend Structure CONSOLE_INFO
    42.             Friend Size As COORD
    43.             Friend CursorPosition As COORD
    44.             Friend Attribute As Short
    45.             Friend Window As RECT
    46.             Friend MaxSize As COORD
    47.         End Structure
    48.  
    49.         <StructLayout(LayoutKind.Sequential)> _
    50.         Friend Structure CURSOR_INFO
    51.             Friend Size As Integer
    52.             Friend Visible As Boolean
    53.         End Structure
    54.  
    55.  
    56.  
    57.  
    58.  
    59.         <DllImport("kernel32")> _
    60.                Public Shared Sub SetConsoleTitle(ByVal lpTitleStr As String)
    61.         End Sub
    62.  
    63.         <DllImport("kernel32")> _
    64.         Public Shared Sub GetConsoleTitle(ByVal lpBuff As StringBuilder, ByVal buffSize As Integer)
    65.         End Sub
    66.  
    67.         <DllImport("kernel32")> _
    68.                Public Shared Function SetConsoleTextAttribute(ByVal hConsoleOutput As IntPtr, ByVal wAttributes As Integer) As Integer
    69.         End Function
    70.  
    71.         <DllImport("kernel32")> _
    72.         Public Shared Function FillConsoleOutputCharacter(ByVal Handle As IntPtr, ByVal uChar As Char, _
    73.                                                           ByVal Len As Integer, ByVal start As COORD, ByRef written As Integer) As Integer
    74.         End Function
    75.  
    76.         <DllImport("kernel32")> _
    77.         Public Shared Function FillConsoleOutputAttribute(ByVal Handle As IntPtr, ByVal att As Short, _
    78.                                                           ByVal Len As Integer, ByVal start As COORD, _
    79.                                                           ByRef writted As Integer) As Boolean
    80.         End Function
    81.  
    82.  
    83.         <DllImport("kernel32")> _
    84.         Public Shared Sub GetConsoleScreenBufferInfo(ByVal Handle As IntPtr, _
    85.                                                      ByRef info As CONSOLE_INFO)
    86.         End Sub
    87.  
    88.         <DllImport("kernel32")> _
    89.         Public Shared Function SetConsoleCursorInfo(ByVal Handle As IntPtr, ByRef info As CURSOR_INFO) As Boolean
    90.         End Function
    91.  
    92.         <DllImport("kernel32")> _
    93.         Public Shared Function SetConsoleCursorPosition(ByVal handle As IntPtr, ByVal coord As Integer) As Boolean
    94.         End Function
    95.  
    96.         <DllImport("kernel32")> _
    97.         Public Shared Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
    98.         End Function
    99.  
    100.         <DllImport("kernel32")> _
    101.         Public Shared Sub GetConsoleMode(ByVal hConsoleHandle As IntPtr, ByRef lpMode As Integer)
    102.         End Sub
    103.  
    104.         <DllImport("kernel32")> _
    105.         Public Shared Sub SetConsoleMode(ByVal hConsoleHandle As IntPtr, ByVal dwMode As Integer)
    106.         End Sub
    107.  
    108.         <DllImport("kernel32")> _
    109.         Public Shared Function CloseHandle(ByVal hObject As IntPtr) As Integer
    110.         End Function
    111.  
    112.         <DllImport("kernel32")> _
    113.         Public Shared Function AllocConsole() As Integer
    114.         End Function
    115.  
    116.         <DllImport("kernel32")> _
    117.         Public Shared Function FreeConsole() As Integer
    118.         End Function
    119.  
    120.     End Class
    Last edited by MrPolite; Jun 5th, 2005 at 04:42 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    sample project...

    put this in a console project. you need to copy past the code from the last two posts in Module1 also .


    VB Code:
    1. ' Please do not remove :)
    2. ' Written by Kourosh Derakshan
    3. '
    4. Module Module1
    5.  
    6.  
    7.     Sub Main()
    8.         Dim conEx As New ConsoleEx
    9.  
    10.         Console.WriteLine("Hello world... changing the title")
    11.         conEx.Title = "New Title!"
    12.         Console.WriteLine("The new title is: " & conEx.Title)
    13.         Console.WriteLine()
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.         Console.WriteLine("Type something. Everything will be shown in upper-case. Press enter to stop:")
    21.         ' Change the input mode to receive every key stroke
    22.         conEx.SetMode(ConsoleEx.InputMode.EchoInput)
    23.         Do
    24.             Dim asciiCode As Integer
    25.             Dim aChar As Char
    26.  
    27.             ' Note that I'm not calling the Readline method, I'm calling the Read method in this mode
    28.             asciiCode = CInt(Console.Read())  ' Returns the ascii code of the entered character
    29.  
    30.             ' 13 is the code for enter
    31.             If asciiCode = 13 Then
    32.                 Exit Do
    33.             End If
    34.  
    35.             ' Convert the asciicode to its character
    36.             aChar = Chr(asciiCode)
    37.             aChar = Char.ToUpper(aChar)
    38.  
    39.             Console.Write(aChar)
    40.         Loop
    41.         Console.WriteLine()
    42.         Console.WriteLine("Press anykey to clear the screen...")
    43.         Console.Read()
    44.  
    45.         ' Change back the input-mode to line input
    46.         conEx.SetMode(ConsoleEx.InputMode.LineInput)
    47.         conEx.Clear()
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.         conEx.SetCursorType(ConsoleEx.CursorType.Off)
    55.         Console.WriteLine("Cursor turned off ...")
    56.         Console.ReadLine()
    57.         Console.WriteLine()
    58.  
    59.         conEx.SetCursorType(ConsoleEx.CursorType.Block)
    60.         Console.WriteLine("Block-type cursor...")
    61.         Console.ReadLine()
    62.         Console.WriteLine()
    63.  
    64.         Console.WriteLine("Cursor restored...")
    65.         conEx.SetCursorType(ConsoleEx.CursorType.SingleLine)
    66.         Console.ReadLine()
    67.         conEx.Clear()
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74.         Console.WriteLine("Turning off the echo, type something and press enter...")
    75.         conEx.EchoInput(False)
    76.         Dim tmpStr As String = Console.ReadLine()
    77.         Console.WriteLine("You typed: " & tmpStr)
    78.         conEx.EchoInput(True)
    79.  
    80.         conEx.SetMode(ConsoleEx.InputMode.EchoInput)
    81.         Console.WriteLine("Press any key to continue...")
    82.         Console.Read()
    83.         conEx.SetMode(ConsoleEx.InputMode.LineInput)
    84.         conEx.Clear()
    85.  
    86.  
    87.  
    88.  
    89.  
    90.         ' (1 is the smallest position, not 0)
    91.         conEx.MoveCursor(1, 10)
    92.         conEx.SetColor(ConsoleEx.ConsoleColor.Red)
    93.         Console.WriteLine("RED")
    94.         conEx.SetColor(ConsoleEx.ConsoleColor.Blue)
    95.         Console.WriteLine("Type in blue: ")
    96.         Console.ReadLine()
    97.  
    98.         ' Original color
    99.         conEx.SetColor(ConsoleEx.ConsoleColor.White)
    100.  
    101.  
    102.         conEx.SetMode(ConsoleEx.InputMode.EchoInput)
    103.         Console.WriteLine("Press any key to continue...")
    104.         Console.Read()
    105.     End Sub
    106.  
    107. End Module
    Last edited by MrPolite; Jun 5th, 2005 at 04:43 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width