ColorEcho/Console.au3

135 lines
4.5 KiB
Plaintext

#cs
Funktioniert nur, wenn es kompiliert wird!
#ce
Local Const $FOREGROUND_BLUE = 0x1
Local Const $FOREGROUND_GREEN = 0x2
Local Const $FOREGROUND_RED = 0x4
Local Const $BACKGROUND_BLUE = 0x10
Local Const $BACKGROUND_GREEN = 0x20
Local Const $BACKGROUND_RED = 0x40
Local Const $BACKGROUND_INTENSITY = 0x80
Local Const $BACKGROUND_SEARCH = 0x20
Local Const $FOREGROUND_INTENSITY = 0x8
Local Const $FOREGROUND_SEARCH = 0x10
Local Const $ENABLE_LINE_INPUT = 0x2
Local Const $ENABLE_ECHO_INPUT = 0x4
Local Const $ENABLE_MOUSE_INPUT = 0x10
Local Const $ENABLE_PROCESSED_INPUT = 0x1
Local Const $ENABLE_WINDOW_INPUT = 0x8
Local Const $ENABLE_PROCESSED_OUTPUT = 0x1
Local Const $ENABLE_WRAP_AT_EOL_OUTPUT = 0x2
Local Const $STD_OUTPUT_HANDLE = -11
Local Const $STD_INPUT_HANDLE = -10
Local Const $STD_ERROR_HANDLE = -12
Local Const $INVALID_HANDLE_VALUE = -1
Global $hConsoleOutput, $hConsoleInput
; Console starten und Handle für In / Out holen
If AllocConsole() Then
$hConsoleOutPut = GetStdHandle($STD_OUTPUT_HANDLE)
If $hConsoleOutPut = $INVALID_HANDLE_VALUE Then MsgBox(0, 'Fehler', "STDOUT-Handle nicht möglich")
$hConsoleInPut = GetStdHandle($STD_INPUT_HANDLE)
If $hConsoleOutPut = $INVALID_HANDLE_VALUE Then MsgBox(0, 'Fehler', "STDIN-Handle nicht möglich")
Else
MsgBox(0, 'Fehler', "Console konnte nicht erstellt werden!")
EndIf
; Titel setzen und Textattribute gelbe Schrift auf blauem Grund
SetConsoleTitle('Meine Test-Console')
If @error Then MsgBox(0, 'Fehler', 'Titel fehlgeschlagen')
SetConsoleTextAttribute($hConsoleOutPut, BitOR($FOREGROUND_RED,$FOREGROUND_GREEN,$FOREGROUND_INTENSITY,$BACKGROUND_BLUE))
If @error Then MsgBox(0, 'Fehler', 'Attribute fehlgeschlagen')
; Text in Console schreiben
ConsoleWriteLine("Hallo Welt!")
If @error Then MsgBox(0, 'Fehler', 'Schreibe Zeile fehlgeschlagen')
_ConsoleWrite("Bitte gib deinen Namen ein: ")
If @error Then MsgBox(0, 'Fehler', 'Schreiben fehlgeschlagen')
; Text aus Console in MsgBox verwenden
MsgBox(0, '', "Dein Name: " & ConsoleReadLine() )
; alles schließen
CloseHandle($hConsoleOutPut)
CloseHandle($hConsoleInPut)
FreeConsole()
#region - Console Funcs
Func AllocConsole()
Local $ret = DllCall("kernel32", "long", "AllocConsole")
If $ret[0] = 0 Then Return SetError(1,0,0)
Return 1
EndFunc
Func FreeConsole()
DllCall("kernel32", "long", "FreeConsole")
EndFunc
Func CloseHandle($hObject)
DllCall("kernel32", "long", "CloseHandle", "long", $hObject)
EndFunc
Func GetStdHandle($nStdHandle)
Local $ret = DllCall("kernel32", "long", "GetStdHandle", "long", $nStdHandle)
If $ret[0] = 0 Then Return SetError(1,0,0)
Return $ret[0]
EndFunc
Func WriteConsole($hConsoleOutPut, $sText)
Local $lpBuffer = DllStructCreate("char[" & StringLen($sText) & "]"), $lpNumberOfCharsWritten = DllStructCreate('int')
DllStructSetData($lpBuffer, 1, $sText)
Local $ret = DllCall("kernel32", "long", "WriteConsoleA", "long", $hConsoleOutPut, "ptr", DllStructGetPtr($lpBuffer), _
"long", StringLen($sText), "ptr", DllStructGetPtr($lpNumberOfCharsWritten), "long", 0)
If $ret[0] = 0 Then Return SetError(1,0,0)
Return $ret[0]
EndFunc
Func ConsoleWriteLine($sInput)
_ConsoleWrite($sInput & @CRLF)
If @error Then Return SetError(1,0,0)
EndFunc
Func _ConsoleWrite($sInput)
WriteConsole($hConsoleOutPut, $sInput)
If @error Then Return SetError(1,0,0)
EndFunc
Func ReadConsole($hConsoleInPut, $ConsoleReadLine)
Local $lpBuffer = DllStructCreate("char[80]"), $lpNumberOfCharsRead = DllStructCreate('int')
DllStructSetData($lpBuffer, 1, $ConsoleReadLine)
Local $ret = DllCall("kernel32", "long", "ReadConsoleA", "long", $hConsoleInPut, "ptr", DllStructGetPtr($lpBuffer), _
"long", StringLen($ConsoleReadLine), "ptr", DllStructGetPtr($lpNumberOfCharsRead), 'long', 0)
If $ret[0] = 0 Then Return SetError(1,0,0)
Return DllStructGetData($lpBuffer, 1)
EndFunc
Func ConsoleReadLine()
Local $ZeroPos, $ConsoleReadLine = ''
For $i = 0 To 79
$ConsoleReadLine &= Chr(0)
Next
$ConsoleReadLine = ReadConsole($hConsoleInPut, $ConsoleReadLine)
$ZeroPos = StringInStr($ConsoleReadLine, Chr(0))
If $ZeroPos > 0 Then $ConsoleReadLine = StringLeft($ConsoleReadLine, $ZeroPos - 3)
Return $ConsoleReadLine
EndFunc
Func SetConsoleTitle($lpConsoleTitle)
Local $ret = DllCall("kernel32", "long", "SetConsoleTitleA", "str", $lpConsoleTitle)
If $ret[0] = 0 Then Return SetError(1,0,0)
Return 1
EndFunc
Func SetConsoleTextAttribute($hConsoleOutPut, $wAttributes)
Local $ret = DllCall("kernel32", "long", "SetConsoleTextAttribute", "long", $hConsoleOutPut, "long", $wAttributes)
If $ret[0] = 0 Then Return SetError(1,0,0)
Return 1
EndFunc
#endregion - Console Funcs