📄 console.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "Console"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2005 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: Console
'
''
' Provides a mechanism for handling input and output for either a screen or Stream.
'
' @remarks Using the <b>Console</b> class allows input and output to a console window.
' By default, a VB6 application will create a new console window for the <b>Console</b>
' class to use. Most times it is desired to run an application in an already open
' console window. If a VB6 application is run from within an existing console window,
' a new window will be created again. To have the VB6 application to use the console
' window from which is it run, the EXE needs to be converted to a console application.
' This can be accomplished by hand using the <i>Link</i> program that is included with
' Visual Basic 6. In a console window use the following command:
' <p>link /edit /subsystem:console <i>EXE Name</i></p>
' <p>Replace <EXE> with the path and executable to be flagged as a console application.
' Be sure the <i>Link</i> and application are both in paths the console window can find.
' Once the command has been executed, the VB6 application will run inside the console
' window from which it was run. If no window existed, then a new one will be created.</p>
' <p>The Console can be used to display text on the screen, and retrieve
' user input from a keyboard. The input and output can be redirected to a new
' source to allow for reading and writing from a specified Stream.</p>
' <p><b>WARNING</b>: Do not close the console through with <b>Close Window</b> button while
' working in the IDE. This will cause the IDE to crash and all data will be lost.</p>
'
' <pre>
' Console.BackgroundColor = Blue
' Console.WriteLine "hello"
' Console.ResetColor
' </pre>
'
' @see ConsoleKeyInfo
'
Option Explicit
Private Declare Function AllocConsole Lib "kernel32.dll" () As Long
Private Declare Function FreeConsole Lib "kernel32.dll" () As Long
Private Declare Function GetConsoleOutputCP Lib "kernel32.dll" () As Long
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Declare Function SetConsoleCtrlHandler Lib "kernel32.dll" (ByVal HandlerRoutine As Long, ByVal Add As Long) As Long
Private Declare Function BeepAPI Lib "kernel32.dll" Alias "Beep" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Private Declare Function SetConsoleWindowInfo Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByVal bAbsolute As Long, ByRef lpConsoleWindow As SMALL_RECT) As Long
Private Declare Function GetStdHandle Lib "kernel32.dll" (ByVal nStdHandle As Long) As Long
Private Declare Function GetConsoleScreenBufferInfo Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Long
Private Declare Function SetConsoleScreenBufferSize Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByRef dwSize As Any) As Long
Private Declare Function CreateConsoleScreenBuffer Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As Any, ByVal dwFlags As Long, ByRef lpScreenBufferData As Any) As Long
Private Declare Function SetConsoleActiveScreenBuffer Lib "kernel32.dll" (ByVal hConsoleOutput As Long) As Long
Private Declare Function SetConsoleTitleA Lib "kernel32.dll" (ByVal lpConsoleTitle As String) As Long
Private Declare Function SetConsoleTitleW Lib "kernel32.dll" (ByVal lpConsoleTitle As Long) As Long
Private Declare Function GetConsoleTitleA Lib "kernel32.dll" (ByVal lpConsoleTitle As String, ByVal nSize As Long) As Long
Private Declare Function GetConsoleTitleW Lib "kernel32.dll" (ByVal lpConsoleTitle As Long, ByVal nSize As Long) As Long
Private Declare Function GetLargestConsoleWindowSize Lib "kernel32.dll" (ByVal hConsoleOutput As Long) As COORD
Private Declare Function SetConsoleTextAttribute Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Private Declare Function SetConsoleCursorPosition Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByRef dwCursorPosition As Any) As Long
Private Declare Function GetConsoleCursorInfo Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByRef lpConsoleCursorInfo As CONSOLE_CURSOR_INFO) As Long
Private Declare Function SetConsoleCursorInfo Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByRef lpConsoleCursorInfo As CONSOLE_CURSOR_INFO) As Long
Private Declare Function FillConsoleOutputCharacter Lib "kernel32.dll" Alias "FillConsoleOutputCharacterA" (ByVal hConsoleOutput As Long, ByVal cCharacter As Byte, ByVal nLength As Long, ByRef dwWriteCoord As Any, ByRef lpNumberOfCharsWritten As Long) As Long
Private Declare Function FillConsoleOutputAttribute Lib "kernel32.dll" (ByVal hConsoleOutput As Long, ByVal wAttribute As Long, ByVal nLength As Long, ByRef dwWriteCoord As Any, ByRef lpNumberOfAttrsWritten As Long) As Long
Private Declare Function ReadConsoleOutput Lib "kernel32.dll" Alias "ReadConsoleOutputA" (ByVal hConsoleOutput As Long, ByRef lpBuffer As CHAR_INFO, ByRef dwBufferSize As Any, ByRef dwBufferCoord As Any, ByRef lpReadRegion As SMALL_RECT) As Long
Private Declare Function WriteConsoleOutput Lib "kernel32.dll" Alias "WriteConsoleOutputA" (ByVal hConsoleOutput As Long, ByRef lpBuffer As CHAR_INFO, ByRef dwBufferSize As Any, ByRef dwBufferCoord As Any, ByRef lpWriteRegion As SMALL_RECT) As Long
Private Declare Function ReadConsoleInput Lib "kernel32.dll" Alias "ReadConsoleInputA" (ByVal hConsoleInput As Long, ByRef lpBuffer As INPUT_RECORD, ByVal nLength As Long, ByRef lpNumberOfEventsRead As Long) As Long
Private Declare Function SetConsoleOutputCP Lib "kernel32.dll" (ByVal wCodePageID As Long) As Long
Private Declare Function GetConsoleCP Lib "kernel32.dll" () As Long
Private Declare Function SetConsoleCP Lib "kernel32.dll" (ByVal wCodePageID As Long) As Long
Private Declare Function PeekConsoleInput Lib "kernel32.dll" Alias "PeekConsoleInputA" (ByVal hConsoleInput As Long, ByRef lpBuffer As INPUT_RECORD, ByVal nLength As Long, ByRef lpNumberOfEventsRead As Long) As Long
Private Declare Function GetConsoleMode Lib "kernel32.dll" (ByVal hConsoleHandle As Long, ByRef lpMode As Long) As Long
Private Declare Function SetConsoleMode Lib "kernel32.dll" (ByVal hConsoleHandle As Long, ByVal dwMode As Long) As Long
Private Const STD_OUTPUT_HANDLE As Long = -11&
Private Const STD_INPUT_HANDLE As Long = -10&
Private Const STD_ERROR_HANDLE As Long = -12&
Private Const CONSOLE_TEXTMODE_BUFFER As Long = 1
Private Const MAX_TITLE_LENGTH As Long = 24500
Private Const MAX_WINDOW_SIZE As Long = &H7FFF
Private Const KEY_EVENT As Long = &H1
Private Const NO_EVENT As Long = -1
Private Const ENABLE_PROCESSED_INPUT As Long = &H1
Private Type CHAR_INFO
Char As Integer
Attributes As Integer
End Type
Private Type CONSOLE_CURSOR_INFO
dwSize As Long
bVisible As Long
End Type
Private Type COORD
x As Integer
y As Integer
End Type
Private Type SMALL_RECT
Left As Integer
Top As Integer
Right As Integer
Bottom As Integer
End Type
Private Type CONSOLE_SCREEN_BUFFER_INFO
dwSize As COORD
dwCursorPosition As COORD
wAttributes As Integer
srWindow As SMALL_RECT
dwMaximumWindowSize As COORD
End Type
Private Type KEY_EVENT_RECORD
bKeyDown As Long
wRepeatCount As Integer
wVirtualKeyCode As Integer
wVirtualScanCode As Integer
uChar As Integer
dwControlKeyState As Long
End Type
Private Type INPUT_RECORD
EventType As Integer
KeyEvent As KEY_EVENT_RECORD
End Type
''
' A list of break types returned by the console.
'
' @param ControlC The CTRL+C key combination was pressed.
' @param ControlBreak The CTRL+Break key combination was pressed.
' @param CloseWindow The window is being closed.
' @param LogOffUser The user is logging off the system.
' @param ShutDownSystem The system is being shut down.
'
Public Enum ConsoleBreakType
ControlC = 0
ControlBreak = 1
CloseWindow = 2
LogOffUser = 5
ShutDownSystem = 6
End Enum
''
' The set of colors used to foreground and background
' color settings of the console.
'
' @param CurrentColor Used to indicate to use the current color settings.
' @param Black
' @param DarkBlue
' @param DarkGreen
' @param DarkCyan
' @param DarkRed
' @param DarkMagenta
' @param DarkYellow
' @param Gray
' @param DarkGray
' @param Blue
' @param Green
' @param Cyan
' @param Red
' @param Magenta
' @param Yellow
' @param White
'
Public Enum ConsoleColor
CurrentColor = -1
Black = 0
DarkBlue = 1
DarkGreen = 2
DarkCyan = 3
DarkRed = 4
DarkMagenta = 5
DarkYellow = 6
Gray = 7
DarkGray = 8
Blue = 9
Green = 10
Cyan = 11
Red = 12
Magenta = 13
Yellow = 14
White = 15
End Enum
Private mOwnsConsole As Boolean
Private mOutput As TextWriter
Private mInput As TextReader
Private mError As TextWriter
Private mOutputHandle As Long
Private mInputHandle As Long
Private mErrorHandle As Long
Private mKeyPressHistory As INPUT_RECORD
Private mOriginalOut As Boolean
Private mOriginalError As Boolean
''
' Returns the cause of the break event in the console
'
' @return The type of break that caused the interuption.
'
Public Property Get BreakType() As ConsoleBreakType
BreakType = modConsoleCallbacks.BreakType
End Property
''
' Returns if a break has been caused.
'
' @return If a break has been caused in the console.
' @remarks This property will be set to true when one of
' several actions occurs. The actions are defined by the
' enumeration <b>ConsoleBreakType</b>. No DoEvents need
' to be called for this property to be set. Simply check
' this property when necessary to respond to any of the events.
'
Public Property Get Break() As Boolean
Break = modConsoleCallbacks.Break
End Property
''
' Sets the break flag.
'
' @param RHS The break flag value.
' @remarks Set Break to false once it has been handled so the
' next console break can set it to true and be detected.
'
Public Property Let Break(ByVal RHS As Boolean)
modConsoleCallbacks.Break = RHS
End Property
''
' Returns the Error output currently being used by the Console.
'
' @return An object that implements the TextWriter interface.
' @remarks The default Error writer writes to the Console error output.
'
Public Property Get Error() As TextWriter
If mError Is Nothing Then
Dim writer As StreamWriter
Set writer = Cor.NewStreamWriter(OpenStandardError, Encoding.GetEncoding(GetConsoleOutputCP))
writer.AutoFlush = True
Set mError = writer
End If
Set Error = mError
End Property
''
' Returns the output writer currently being used by the Console.
'
' @return Returns an object that implements the TextWriter interface.
' @remarks The default output for the Console is to a Console window displayed
' on the screen. The output can be redirected to a new Stream using SetOut.
'
Public Property Get Out() As TextWriter
If mOutput Is Nothing Then
Dim writer As StreamWriter
Set writer = Cor.NewStreamWriter(OpenStandardOutput, Encoding.GetEncoding(GetConsoleOutputCP))
writer.AutoFlush = True
Set mOutput = writer
End If
Set Out = mOutput
End Property
''
' Returns the input source currently used by the Console.
'
' @return Returns an object that implements the TextReader interface
' @remarks The default source of the input is the Keyboard. This can
' be changed by using the SetIn function.
'
Public Property Get In_() As TextReader
If mInput Is Nothing Then Set mInput = Cor.NewStreamReader(OpenStandardInput, Encoding.GetEncoding(GetConsoleCP), False)
Set In_ = mInput
End Property
''
' Reads a character from the current input source.
'
' @return The next character read from the input source, or -1 if no more characters exists.
' @remarks The Read method does not return until the Return key is pressed.
' Once the function returns, it can be called repeatedly until all key presses
' upto the return key are retrieved. The return key is also returned
' as a carriage-return (13) followed by a line-feed (10).
' @include "..\Includes\ConsoleRead.txt"
Public Function Read() As Long
Read = In_.Read
End Function
''
' Reads all of the characters from the current input source until
' a NewLine break is reached.
'
' @return Returns all of the available characters upto a NewLine break,
' not including the NewLine characters.
' @remarks The ReadLine function will block and not return until a NewLine
' characters is pressed. This is usually the Return key.
' @include "..\Includes\ConsoleReadLine.txt"
Public Function ReadLine() As String
ReadLine = In_.ReadLine
End Function
''
' Reads a single key from the console input.
'
' @param Intercept Flags whether the pressed key should be displayed
' through the console output or not.
' @return A <b>ConsoleKeyInfo</b> object containing information about the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -