• Offizieller Beitrag

    Hi,
    hier mal ein Skriptbsp., wie man ein eigenes Consolenfenster erstellt und in die Console schreibt bzw. aus ihr liest.

    Wichtig! Läuft nur wenn es kompiliert wurde!
    [autoit]

    #cs
    Funktioniert nur, wenn es kompiliert wird!
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    Global $hConsoleOutput, $hConsoleInput

    [/autoit] [autoit][/autoit] [autoit]

    ; 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

    [/autoit] [autoit][/autoit] [autoit]

    ; 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')

    [/autoit] [autoit][/autoit] [autoit]

    ; 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')

    [/autoit] [autoit][/autoit] [autoit]

    ; Text aus Console in MsgBox verwenden
    MsgBox(0, '', "Dein Name: " & ConsoleReadLine() )

    [/autoit] [autoit][/autoit] [autoit]

    ; alles schließen
    CloseHandle($hConsoleOutPut)
    CloseHandle($hConsoleInPut)
    FreeConsole()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #region - Console Funcs

    [/autoit] [autoit][/autoit] [autoit]

    Func AllocConsole()
    Local $ret = DllCall("kernel32", "long", "AllocConsole")
    If $ret[0] = 0 Then Return SetError(1,0,0)
    Return 1
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func FreeConsole()
    DllCall("kernel32", "long", "FreeConsole")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func CloseHandle($hObject)
    DllCall("kernel32", "long", "CloseHandle", "long", $hObject)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    Func ConsoleWriteLine($sInput)
    _ConsoleWrite($sInput & @CRLF)
    If @error Then Return SetError(1,0,0)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _ConsoleWrite($sInput)
    WriteConsole($hConsoleOutPut, $sInput)
    If @error Then Return SetError(1,0,0)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    Func SetConsoleTitle($lpConsoleTitle)
    Local $ret = DllCall("kernel32", "long", "SetConsoleTitleA", "str", $lpConsoleTitle)
    If $ret[0] = 0 Then Return SetError(1,0,0)
    Return 1
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit]

    #endregion - Console Funcs

    [/autoit]