Results 1 to 27 of 27

Thread: Console class, if anyone wants it

Threaded View

  1. #1

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

    Console class, if anyone wants it

    I couldn't find a compelete console class for VB, and most of the good ones are in C#. So I just converted some code to vb.net to add it to my library. It's mainly from a project called ExtendedConsole which is written in C#. But I made some changed.... and I think I combined some other programs from C# and VB6 to make this
    Also see the next post for the rest of the code

    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: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
  •  



Click Here to Expand Forum to Full Width