gil900

_GUIConsole - Create and write to alternative console + Enter inputs through the console (new!)

6 posts in this topic

#1 ·  Posted (edited)

Hello,

 

Example 1:

example.png.1e93d1a64e2992c59e2b8d2ba10e

*^ Please ignore from the "concole". I fixed it

56bfaae5ae686_example2.png.de86aa0257abb

 

 

#include "_GUIConsole.au3"


; Set exit on exit key
HotKeySet('{ESC}','Exit1')


; Create the Console GUI
_GUIConsole_Create(Default,$COLOR_BLUE,$COLOR_WHITE)

Sleep(500)
; Print some text
_GUIConsole_Out('Some text 1 ...');,$iAddLines = 1,$sAddChar = @CRLF)

Sleep(500)
; Print some text
_GUIConsole_Out('Some text 2 ...')

Sleep(500)

_GUIConsole_Out('After this text there is 3 @CRLF',3)

Sleep(500)

_GUIConsole_Out('See?')

Sleep(1000)

_GUIConsole_Out('After this text there is 3 @TAB',3,@TAB)

Sleep(500)

_GUIConsole_Out('See?',2)

Sleep(1000)

_GUIConsole_Out('Now I will delete this console and open another with different colors...')

Sleep(1500)

; Delete the Console GUI
_GUIConsole_Delete()

Sleep(500)

; Create the Console GUI
_GUIConsole_Create('Just another title',$COLOR_RED,$COLOR_WHITE)

Sleep(500)


_GUIConsole_Out('As you can see, now console looks different.'&@CRLF&'Enjoy!')


While Sleep(100)
WEnd



#Region functions
Func Exit1()
    Exit
EndFunc

#EndRegion

 

Example 2 (NEW) :
56c6702791026_example33.png.8f9e3952b441

An example much better included here
56c78fb067639_sharedvarsexample.thumb.pn

Example 3 code:

#include "_GUIConsole.au3"
HotKeySet('{ESC}','Exit1')

_GUIConsole_Create()


While 1
    _GUIConsole_Out('Please enter value for $number1: ',0)
    $number1 = _GUIConsole_In()
    _GUIConsole_Out(' The value of $number1 is: '&$number1)

    _GUIConsole_Out('Please enter the value of $number2: ',0)
    $number2 = _GUIConsole_In()
    _GUIConsole_Out(' The value of $number2 is: '&$number2,2)


    _GUIConsole_Out('$number1+$number2 = '&$number1+$number2)
    _GUIConsole_Out('$number1-$number2 = '&$number1-$number2)
    _GUIConsole_Out('$number1*$number2 = '&$number1*$number2)
    _GUIConsole_Out('$number1/$number2 = '&$number1/$number2,2)


    While 1
        _GUIConsole_Out('To exit: press X, To reset press R :')
        Switch _GUIConsole_In()
            Case 'x', 'X'
                Exit
            Case 'r', 'R'
                 _GUIConsole_Clean()
                ExitLoop
            Case Else
                _GUIConsole_Out('You must enter X or R')

        EndSwitch
    WEnd

WEnd






Func Exit1()
    Exit
EndFunc

 

 

Example 3:

56c60a7bbb7e4_example22.png.939f407a1811

#include "_GUIConsole.au3"
#include "Array.au3"


HotKeySet('{ESC}','Exit1')

$haConsole1 = _GUIConsole_Create('Console 1 - Events of X',$COLOR_BLUE,$COLOR_WHITE) ; Create this console and set this to be the active one
$haConsole2 = _GUIConsole_Create('Console 2 - Events of Y',$COLOR_RED,$COLOR_WHITE) ; Create this console and set this to be the active one
$haConsole3 = _GUIConsole_Create('Console 3 - Events of Z',$COLOR_GREEN,$COLOR_WHITE) ; Create this console and set this to be the active one


Sleep(1000)

; Write something in the active console
; NOTE: If _GUIConsole_SetActiveConsole was not called before then the active console will be always the
; Last created one ($haConsole3)
_GUIConsole_Out('Event 1 ...')
Sleep(500)
_GUIConsole_Out('Event 2 ...')
Sleep(500)



; Write something in the $haConsole1
_GUIConsole_SetActiveConsole($haConsole1) ; To write something in another console you must first set the another
; Console to be the active so when you use _GUIConsole_Out(*) , the text will be printed in the another console.

_GUIConsole_Out('Event 1 ...')
Sleep(500)
_GUIConsole_Out('Event 2 ...')
Sleep(500)


Sleep(1000)

; Write some critical and important event in $haConsole2
;   OK, so the event we are going to print is very important one so we may want to print the event and also
;   to highlight this event by bringing the active console to top and flash the window.
_GUIConsole_SetActiveConsole($haConsole2)
_GUIConsole_Out("This is a very important event !!! Don't miss it!")
_GUIConsole_Out("Y just crashed!")
_GUIConsole_Flash($haConsole2) ; To highlight the console


While Sleep(100)
WEnd






Func Exit1()
    Exit
EndFunc


 

Functions:
 

Quote

 

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIConsole_Create
; Description ...: Create alternative GUI console (Similar to c++ console but better
; Syntax ........: _GUIConsole_Create([$sTitle = Default[, $hBkColor = Default[, $hTextColor = Default[, $x_size = 690[,
;                  $y_size = 298]]]]])
; Parameters ....: $sTitle              - [optional] The title of the console. Default is Default.
;                  $hBkColor            - [optional] The background color of the console. Default is Default.
;                  $hTextColor          - [optional] The color of the text in the console. Default is Default.
;                  $x_size              - [optional] The x size of the console. Default is 690.
;                  $y_size              - [optional] The y size of the console. Default is 298.
; Return values .: handle of the console (You need this if you create more then one console and you switch between them.
; Author ........: gil900
; Link ..........: https://www.autoitscript.com/forum/topic/180608-_guiconsole-create-and-write-to-alternative-console/
; Example .......: YES
; ===============================================================================================================================

 

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIConsole_Out
; Description ...: Print text data in the console
; Syntax ........: _GUIConsole_Out($sText[, $iAddLines = 1[, $sAddChar = @CRLF]])
; Parameters ....: $sText               - A string value.
;                  $iAddLines           - An integer value. Default is 1.
;                                        [optional] If set to 0, It will not add "ENTER" (@CRLF of something else you set in the next parameter)
;                                        after the text.
;                                        If set bigger then 0 then it will add $iAddLines times "ENTER"(or something else) after the text.
;                  $sAddChar            - [optional] Default is @CRLF. This variable matter if $iAddLines > 0. It will add $iAddLines times
;                                        $sAddChar ( @CRLF in this case if set to default)
; Return values .: None
; Author ........: gil900
; Link ..........: https://www.autoitscript.com/forum/topic/180608-_guiconsole-create-and-write-to-alternative-console/
; Example .......: YES
; ===============================================================================================================================


; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIConsole_In
; Description ...: Get input from the console
; Syntax ........: _GUIConsole_In()
; Parameters ....: $bCleanEnter                - [optional] If set to 1 then it will remove the enter that the user enter...
; Return values .: Return the text that entered in the console
; Author ........: gil900
; Example .......: YES
; ===============================================================================================================================


; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIConsole_Clean
; Description ...: Remove all text that printed in the console
; Syntax ........: _GUIConsole_Clean()
; Return values .: None
; Author ........: gil900
; Example .......: Yes
; ===============================================================================================================================


; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIConsole_Delete
; Description ...: Delete the Console GUI
; Syntax ........: _GUIConsole_Delete()
; Return values .: None
; Author ........: gil900
; Link ..........: https://www.autoitscript.com/forum/topic/180608-_guiconsole-create-and-write-to-alternative-console/
; Example .......: YES
; ===============================================================================================================================


; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIConsole_SetActiveConsole
; Description ...: Set in which console the data will be printed
; Syntax ........: _GUIConsole_SetActiveConsole($haConsole)
; Parameters ....: $haConsole           - The handle for the target console that returned from _GUIConsole_Create.
; Return values .: Failure: sets the @error the line number of the error.
; Author ........: gil900
; Link ..........: https://www.autoitscript.com/forum/topic/180608-_guiconsole-create-and-write-to-alternative-console/
; Example .......: YES
; ===============================================================================================================================


; #FUNCTION# ====================================================================================================================
; Name ..........: _GUIConsole_Flash
; Description ...: Flash this console - Useful if there is something important to see in the console
; Syntax ........: _GUIConsole_Flash($haConsole[, $iFlashes = 4[, $iDelay = 500]])
; Parameters ....: $haConsole           - The handle for the target console that returned from _GUIConsole_Create.
;                  $iFlashes            - [optional] The amount of times to flash the console. Default 4.
;                  $iDelay              - [optional] The time in milliseconds to sleep between each flash. Default 500 ms.
; Return values .: None
; Author ........: gil900
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: https://www.autoitscript.com/forum/topic/180608-_guiconsole-create-and-write-to-alternative-console/
; Example .......: YES
; ===============================================================================================================================

 

 

Download UDF:

_GUIConsole r3.1.rar

_GUIConsole r3.rar

* Added:  _GUIConsole_In(*) - The ability to enter input  through the console (See example 3)
* Changed: Name of _GUIConsole_Write changed to _GUIConsole_Out
* Added: GUIConsole_Clean

 

_GUIConsole - r2.rar

* Fixed: problem in the title when set to Default and the script is Compiled
* Added: The ability to create and work with more than console (See example 2)
* _GUIConsole_Flash

* You can now resize the console
 

_GUIConsole.au3

 

Hope you find it useful.
Enjoy :)

 

 

Edited by gil900
2 people like this

Share this post


Link to post
Share on other sites



Good!

Thank you for your share

Share this post


Link to post
Share on other sites

 

It looks like a beautiful thing.. Thank you.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

It's useful but I like the richedit console I made a while back for a large application I was working on. Went through and pieced it all together to work as a standalone .au3 file if you wanna take a look. Uses hex colors to highlight words (or even letters) using the color tags (<color=0xFF00FF>Hello</color> <color=0x00FF00>World</color>)

Example:

#include <File.au3>
#include <ButtonConstants.au3>
#include "GUICreateConsole.au3"

Global $hGUI = GUICreate("", 540, 400)
Global $btnSelectPic = GUICtrlCreateButton("Select Picture", 10, 10, 100, 30)
Global $btnRandom = GUICtrlCreateButton("Random properties", 10, 40, 100, 20)
Global $lblLabel = GUICtrlCreateLabel("", 120, 10, 480, 25)
GLobal $picPic = GUICtrlCreatePic("", 10, 70, 520, 320)
Global $file_path = ""
Global $drive = ""
Global $dir = ""
Global $filename = ""
Global $ext = ""
Global $font_names[] = ["Arial", "Segoe UI", "Consolas", "MS Sans Serif", "Courier New", "Georgia"]
Global $font_sizes[] = [10, 11, 13, 8, 9, 12]
Global $text_colors[] = [0xFF0000, 0xFFFFFF, 0xFFFF00, 0x00FFFF, 0x98F3CE]
Global $back_colors[] = [0x8B8B8B, 0x1F1F1F, 0xFBFA96, 0x000000, 0xFB96CA]
Global $trans[] = [255, 230, 210, 200, 190, 150]

GUISetState(@SW_SHOW, $hGUI)
ConsoleSetAttributes("Arial", 12, "+bo+it", "", "", 225)
GUIConsoleCreate()
ConsoleOut("Set console attributes before creating console!" & @CRLF)
ConsoleOut("Resetting Attributes" & @CRLF)
ConsoleSetAttributes("Consolas", 10, "-bo-it", "", "", 255)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            GUIConsoleDelete()
            GUIDelete($hGUI)
            Exit 0
        Case $btnSelectPic
            $file_path = FileOpenDialog("Select a picture", @UserProfileDir & "\Pictures\", "All (*.*)|Jpg (*.jpg)|Bmp (*.bmp)")
            If (Not @Error) Then
                _PathSplit($file_path, $drive, $dir, $filename, $ext )
                GUICtrlSetData($lblLabel, $file_path)
                ConsoleOut("Loading image " & $filename & $ext & @CRLF)
                If (GUICtrlSetImage($picPic, $file_path)) Then
                    ConsoleOut("Successfully loaded image" & @CRLF)
                Else
                    ConsoleOut("<color=0xFF0000>Failed</color> to load " & $filename & $ext & " ... ")
                    If ($ext <> "bmp" or $ext <> "jpg" or $ext <> "gif") Then ConsoleOut("<color=0x9EAC00>" & $ext & " is not supported</color>" & @CRLF, False)
                EndIf
            EndIf
        Case $btnRandom
            ConsoleSetAttributes($font_names[Random(0, UBound($font_names) -1, 1)], $font_sizes[Random(0, UBound($font_sizes) -1, 1)], "", _
            $text_colors[Random(0, UBound($text_colors) -1, 1)], $back_colors[Random(0, UBound($back_colors) -1, 1)], $trans[Random(0, UBound($trans) -1, 1)], 225)
            ConsoleOut("New attributes updated" & @CRLF)
    EndSwitch
WEnd

GUICreateConsole.au3

#include-once
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <String.au3>
#include <Color.au3>
#include <Date.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <File.au3>

#Region Global-Variables
Global Const $ANSI_CHARSET = 0
Global Const $BALTIC_CHARSET = 186
Global Const $CHINESEBIG5_CHARSET = 136
Global Const $DEFAULT_CHARSET = 1
Global Const $EASTEUROPE_CHARSET = 238
Global Const $GB2312_CHARSET = 134
Global Const $GREEK_CHARSET = 161
Global Const $HANGEUL_CHARSET = 129
Global Const $MAC_CHARSET = 77
Global Const $OEM_CHARSET = 255
Global Const $RUSSIAN_CHARSET = 204
Global Const $SHIFTJIS_CHARSET = 128
Global Const $SYMBOL_CHARSET = 2
Global Const $TURKISH_CHARSET = 162
Global Const $VIETNAMESE_CHARSET = 163
Global $__hWnd_console = Default
Global $__hWnd_parent = 0
Global $__hWnd_ctxt_menu = 0
Global $__use_parent = False
Global $__hWnd_richedit = 0
Global $__console_width = 600
Global $__console_height = 150
Global $__console_left = -1
Global $__console_top = -1
Global $__console_font_name = "Consolas"
Global $__console_font_size = 10
Global $__console_char_attributes = ""
Global $__console_txt_color = 0xC8C8C8
Global $__console_bk_color = 0x1F1F1F
Global $__console_transparency = 255
Global $__console_charset = $DEFAULT_CHARSET
Global $__rtf_console_text = ""
Global $__hWnd_wndproc = 0
Global $__ptr_procold = 0
Global $__idOpen = 0
Global $__idSave = 0
Global $__idSaveAs = 0
Global $__idClear = 0
Global $__idCopyPlain = 0
Global $__idCopyRtf = 0
Global $__idUndo = 0
Global Enum $idOpen = 1000, $idSave, $idSaveAs, $idClear, $idCopyPlain, $idCopyRtf, $idUndo
#EndRegion Global-Variables

; #FUNCTION# ====================================================================================================================
; Name ..........: GUIConsoleCreate
; Description ...: Create a RichEdit console that allows more formatting options
; Syntax ........: GUIConsoleCreate([$iWidth = Default[, $iHeight = Default[, $iLeft = Default[, $iTop = Default[,
;                   $hWndParent = Default]]]]])
; Parameters ....: $iWidth      - [optional] Width of console
;                  $iHeight     - [optional] Height of console
;                  $iLeft       - [optional] X position of console
;                  $iTop        - [optional] Y position of console
;                  $hWndParent  - [optional] Handle to the parent window
; Return values .: Handle to the console.
; Author ........: InunoTaishou
; ===============================================================================================================================

Func GUIConsoleCreate($iWidth = Default, $iHeight = Default, $iLeft = Default, $iTop = Default, $hWndParent = Default)
    If (WinExists($__hWnd_console)) Then
        ConsoleOut("Only one console allowed" & @CRLF)
        Return SetError(-1, -1, -1)
    EndIf

    If ($iWidth <> Default And $iWidth <> "") Then $iWidth = $__console_width
    If ($iHeight <> Default And $iHeight <> "") Then $iHeight = $__console_height
    If ($iLeft <> Default And $iLeft <> "") Then $iLeft = $__console_left
    If ($iTop <> Default And $iTop <> "") Then $iTop = $__console_top
    If ($hWndParent <> Default Or IsHWnd($hWndParent)) Then $__use_parent = True

    $__hWnd_parent = ($__use_parent ? $hWndParent : 0)
    $__hWnd_console = GUICreate("GUICreateConsole", $__console_width, $__console_height, $__console_left, $__console_top, BitOR($WS_POPUP, $WS_BORDER, $WS_SIZEBOX), -1, $__hWnd_parent)
    Local $__title_label = GUICtrlCreateLabel("", 0, 0, $__console_width + 1, 20, $SS_CENTER, $GUI_WS_EX_PARENTDRAG)
    $__hWnd_richedit = _GUICtrlRichEdit_Create($__hWnd_console, "", 0, 0, $__console_width + 1, $__console_height + 1, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY))

    $__hWnd_ctxt_menu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())

    $__idOpen = GUICtrlCreateMenuItem("Open", $__hWnd_ctxt_menu)
    $__idSave = GUICtrlCreateMenuItem("Save", $__hWnd_ctxt_menu)
    $__idSaveAs = GUICtrlCreateMenuItem("Save As...", $__hWnd_ctxt_menu)
    GUICtrlCreateMenuItem("", $__hWnd_ctxt_menu)
    $__idClear = GUICtrlCreateMenuItem("Clear", $__hWnd_ctxt_menu)
    $__idUndo = GUICtrlCreateMenuItem("Undo", $__hWnd_ctxt_menu)
    GUICtrlCreateMenuItem("", $__hWnd_ctxt_menu)
    $__idCopyPlain = GUICtrlCreateMenuItem("Copy Plain Text", $__hWnd_ctxt_menu)
    $__idCopyRtf = GUICtrlCreateMenuItem("Copy With Rtf Format", $__hWnd_ctxt_menu)
    _GUICtrlRichEdit_SetEventMask($__hWnd_richedit, $ENM_MOUSEEVENTS)
    #cs
    ; Use a WindowProc to handle the popup menu instead of a GUICtrlCreateContextMenu
        $__hWnd_ctxt_menu = _GUICtrlMenu_CreatePopup()
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "Open", $idOpen)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "Save", $idSave)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "Save As...", $idSaveAs)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "", 0)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "Clear", $idClear)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "Undo", $idUndo)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "", 0)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "Copy Plain Text", $idCopyPlain)
        _GUICtrlMenu_AddMenuItem($__hWnd_ctxt_menu, "Copy With Rtf Format", $idCopyRtf)

        $__hWnd_wndproc = DllCallbackRegister("__CONSOLE_WNDPROC", "ptr", "hwnd;uint;wparam;lparam")
        $__ptr_procold = _WinAPI_SetWindowLong($__hWnd_richedit, $GWL_WNDPROC, DllCallbackGetPtr($__hWnd_wndproc))
    #ce
    GUICtrlSetResizing($__title_label, $GUI_DOCKHEIGHT)
    GUICtrlSetBkColor($__title_label, $GUI_BKCOLOR_TRANSPARENT)
    GUIRegisterMsg($WM_SIZE, "__CONSOLE_WM_SIZE")
    GUIRegisterMsg($WM_GETMINMAXINFO, "__WM_GETMINMAXINFO")
    GUIRegisterMsg($WM_NOTIFY, "__CONSOLE_WM_NOTIFY")
    GUIRegisterMsg($WM_COMMAND, "__CONSOLE_WM_COMMAND")
    OnAutoItExitRegister("GUIConsoleDelete")

    ConsoleSetAttributes()
    ConsoleOut("<color=0xFF0000>[Console initialized]</color> : <color=0xC000FF>[You can drag the console from this top line]</color>" & @CRLF)

    GUISetState(@SW_SHOW, $__hWnd_console)
    Return $__hWnd_console
EndFunc   ;==>GUIConsoleCreate

; #FUNCTION# ====================================================================================================================
; Name ..........: GUIConsoleDelete
; Description ...: Delete the created console.
; Syntax ........: GUIConsoleDelete()
; Parameters ....:
; Return values .: If an error occurs returns an array, otherwise returns 0.
;                  [0]  = 0 if failed to delete the RichEdit control.
;                  [1]  = 1 if destroying a RichEdit control belonging to another application.
;                  [2]  = 0 if failed to destroy the console GUI.
; Remarks .......: If you created the Console under a parent, call this before deleting your GUI.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func GUIConsoleDelete()
    OnAutoItExitUnRegister("GUIConsoleDelete")
    GUIRegisterMsg($WM_SIZE, "")
    GUIRegisterMsg($WM_GETMINMAXINFO, "")
    GUIRegisterMsg($WM_NOTIFY, "")
    GUIRegisterMsg($WM_COMMAND, "")
    ;DllCallbackFree($__hWnd_wndproc)
    Local $__rect_console = WinGetPos($__hWnd_console)
    If (Not @error) Then
        $__console_width = $__rect_console[2]
        $__console_height = $__rect_console[3]
        $__console_left = $__rect_console[0]
        $__console_top = $__rect_console[1]
    EndIf
    Local $__richedit_destroy = _GUICtrlRichEdit_Destroy($__hWnd_richedit)
    Local $__richedit_error = @error
    Local $__console_delete = GUIDelete($__hWnd_console)
    Local $__delete_return = ""
    $__use_parent = False

    If (Not $__richedit_destroy) Then $__delete_return &= $__richedit_destroy & '|' & $__richedit_error & '|'
    If (Not $__console_delete) Then $__delete_return &= $__console_delete

    Return (StringInStr($__delete_return, '|') ? SetError(1, 0, StringSplit($__delete_return, '|', $STR_NOCOUNT)) : 0)
EndFunc   ;==>GUIConsoleDelete

; #FUNCTION# ====================================================================================================================
; Name ..........: ConsoleSetAttributes
; Description ...: Delete the created console.
; Syntax ........: ConsoleSetAttributes([$sFontName = Default[, $iFontSize = Default[, $sCharAttributes = Default[,
;                   $iTxtColor = Default[, $iBkColor = Default[, $iTransparency = Default[, $iCharset = Default]]]]])
; Parameters ....: $sFontName       - [optional] Name of font.
;                  $iFontSize       - [optional] Font size.
;                  $sCharAttributes - [optional] RichEdit character attributes (See _GUICtrlRichEdit_SetCharAttributes for more info).
;                  $iTxtColor       - [optional] Hex color to set text.
;                  $iBkColor        - [optional] Hex color to set the background.
;                  $iTransparency   - [optional] A number in the range 0 - 255. The lower the number, the more transparent the window
;                                                   will become. 255 = Solid, 0 = Invisible.
;                  $iCharset        - [optional] Character set to use (See _GUICtrlRichEdit_SetFont for more info).
; Return values .: None.
; Remarks .......: Sets the console attributes of selected text or, if none selected, sets text inserted at insertion point.
;                   Function can be called before creating a console.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func ConsoleSetAttributes($sFontName = Default, $iFontSize = Default, $sCharAttributes = Default, $iTxtColor = Default, $iBkColor = Default, $iTransparency = Default, $iCharset = Default)
    If ($sFontName <> Default And $sFontName <> "") Then $__console_font_name = $sFontName
    If ($iFontSize <> Default And $iFontSize <> "") Then $__console_font_size = $iFontSize
    If ($sCharAttributes <> Default And $sCharAttributes <> "") Then $__console_char_attributes = $sCharAttributes
    If ($iTxtColor <> Default And $iTxtColor <> "") Then $__console_txt_color = $iTxtColor
    If ($iBkColor <> Default And $iBkColor <> "") Then $__console_bk_color = $iBkColor
    If ($iTransparency > 255 Or $iTransparency < 0) Then $iTransparency = 255
    If ($iTransparency <> Default And $iTransparency <> "") Then $__console_transparency = $iTransparency
    If ($iCharset <> Default And $iCharset <> "") Then $__console_charset = $iCharset
    If ($__hWnd_richedit) Then
        Local Const $__console_rtf_format = StringReplace(_GUICtrlRichEdit_StreamToVar($__hWnd_richedit, True), "\par", "", -1)
        GUISetBkColor($__console_bk_color, $__hWnd_console)
        WinSetTrans($__hWnd_console, "", $__console_transparency)
        _GUICtrlRichEdit_SetBkColor($__hWnd_richedit, $__console_bk_color)
        _GUICtrlRichEdit_SetCharColor($__hWnd_richedit, $__console_txt_color)
        _GUICtrlRichEdit_SetCharAttributes($__hWnd_richedit, $__console_char_attributes)
        _GUICtrlRichEdit_SetFont($__hWnd_richedit, $__console_font_size, $__console_font_name, $__console_charset)
        _GUICtrlRichEdit_StreamFromVar($__hWnd_richedit, $__console_rtf_format)
    EndIf
EndFunc   ;==>ConsoleSetAttributes

; #FUNCTION# ====================================================================================================================
; Name ..........: ConsoleOut
; Description ...: Append text to console
; Syntax ........: ConsoleOut([$sMsg[, $bTimeStamp = True[, $iTxtColor = $__console_txt_color]]])
; Parameters ....: $sMsg        - [optional] String to appenend. Function does not automatically appened a newline.
;                  $bTimeStamp  - [optional] Add timestamp to beginning of the text.
;                  $iTxtColor   - [optional] Hex color for text for this message.
; Return values .: None.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func ConsoleOut(Const ByRef $sMsg, Const $bTimeStamp = True, Const $iTxtColor = $__console_txt_color)
    Local $__split_message
    Local $__message_text_color = $iTxtColor
    Local $__text_to_append = ""

    If ($bTimeStamp) Then _GUICtrlRichEdit_AppendTextColored('[' & _NowTime(5) & "] ", $iTxtColor, $__console_char_attributes)

    If (StringInStr($sMsg, "<color=")) Then
        $__split_message = StringSplit($sMsg, "", $STR_CHRSPLIT)
        For $i = 1 To $__split_message[0]
            If ($__split_message[$i] = '<') Then
                If ($__split_message[$i + 1] = 'c') Then
                    If (StringLen($__text_to_append)) Then
                        _GUICtrlRichEdit_AppendTextColored($__text_to_append, $__message_text_color, $__console_charset)
                        $__text_to_append = ""
                    EndIf

                    $__message_text_color = ""
                    $i += 7

                    While ($__split_message[$i] <> '>')
                        $__message_text_color &= $__split_message[$i]
                        $i += 1
                    WEnd

                    ContinueLoop
                ElseIf ($__split_message[$i + 1] = '/') Then
                    If (StringLen($__text_to_append)) Then
                        _GUICtrlRichEdit_AppendTextColored($__text_to_append, $__message_text_color, $__console_charset)
                        $__text_to_append = ""
                        $i += 7
                        $__message_text_color = $iTxtColor
                        ContinueLoop
                    EndIf
                EndIf
            Else
                $__text_to_append &= $__split_message[$i]
            EndIf
        Next

        If (StringLen($__text_to_append)) Then
            _GUICtrlRichEdit_AppendTextColored($__text_to_append, $__message_text_color, $__console_charset)
            $__text_to_append = ""
        EndIf
    Else
        _GUICtrlRichEdit_AppendTextColored($sMsg, $__message_text_color, $__console_charset)
    EndIf
EndFunc   ;==>ConsoleOut

; #FUNCTION# ====================================================================================================================
; Name ..........: ConsoleSetFromFile
; Description ...: Sets console data from path specified.
; Syntax ........: ConsoleSetFromFile(Const ByRef $sFileName)
; Parameters ....: $sFileName   - Full path to file to set console.
; Return values .: True if success, False otherwise and sets @Error to the same @Error from _GUICtrlRichEdit_StreamFromFile.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func ConsoleSetFromFile(Const ByRef $sFileName)
    ConsoleClear()
    Local $__stream_from_file = _GUICtrlRichEdit_StreamFromFile($__hWnd_richedit, $sFileName)
    If (Not $__stream_from_file) Then
        $__stream_from_file = @Error
        ConsoleOut((@Error = 1021 ? "Cannot open file" : @Error = 1022 ? "File is empty" : @Error = 700 ? "Attempt to stream in too many characters" : "") & @CRLF)
        Return SetError($__stream_from_file, 0, False)
    EndIf
    Return 1
EndFunc   ;==>ConsoleSetFromFile

; #FUNCTION# ====================================================================================================================
; Name ..........: ConsoleSaveToFile
; Description ...: Save console data to file specified.
; Syntax ........: ConsoleSaveToFile(Const ByRef $sFileName)
; Parameters ....: $sFileName   - Full path to file. If extension is rtf saves in rtf format, otherwise saves as plain text.
; Return values .: True if saved successfully. Sets @Error to -1 if FileOpen fails.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func ConsoleSaveToFile(ByRef $sFileName)
    Local $__plain_console_text = _GUICtrlRichEdit_GetText($__hWnd_richedit)
    Local $__drive = ""
    Local $__dir = ""
    Local $__file_name = ""
    Local $__file_ext = ""
    Local $__split_path = _PathSplit($sFileName, $__drive, $__dir, $__file_name, $__file_ext)
    If ($__file_ext = "") Then
        $sFileName &= ".txt"
        $__file_ext = "txt"
    EndIf
    Local $__hWnd_file = FileOpen($sFileName, $FO_OVERWRITE + $FO_CREATEPATH)

    $__rtf_console_text = StringReplace(_GUICtrlRichEdit_StreamToVar($__hWnd_richedit, True), "\par", "", -1)

    If ($__hWnd_file = -1) Then
        ConsoleOut("Failed to save to file " & $__file_name & '.' & $__file_ext & @CRLF)
        Return SetError(-1, 0, False)
    Else
        FileWrite($__hWnd_file, ($__file_ext = "rtf" ? $__rtf_console_text : $__plain_console_text))
        FileClose($__hWnd_file)
    EndIf

    Return True
EndFunc   ;==>ConsoleSaveToFile

; #FUNCTION# ====================================================================================================================
; Name ..........: ConsoleClear
; Description ...: Clears console.
; Syntax ........: ConsoleClear(Const $bRtfReturn = False)
; Parameters ....: $bRtfReturn      - Set to true will return the RTF formatted text, False returns plain text.
; Return values .: The RTF text or plain text, depending on the $bRtfReturn value.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func ConsoleClear(Const $bRtfReturn = False)
    Local $__plain_console_text = _GUICtrlRichEdit_GetText($__hWnd_richedit)
    $__rtf_console_text = StringReplace(_GUICtrlRichEdit_StreamToVar($__hWnd_richedit, True), "\par", "", -1)
    _GUICtrlRichEdit_SetText($__hWnd_richedit, "")
    Return ($bRtfReturn ? $__rtf_console_text : $__plain_console_text)
EndFunc   ;==>ConsoleClear

#Region Internal-Functions
; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlRichEdit_AppendTextColored
; Description ...: Appends colored text to console
; Syntax ........: _GUICtrlRichEdit_AppendTextColored(Const ByRef $__sText, Const ByRef $__iColor, Const ByRef $__char_attribute)
; Parameters ....: $__sText             - Text to append.
;                  $__iColor            - Hex color to set the text.
;                  $__char_attribute    - Sets the character attributes.
; Return values .: None.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func _GUICtrlRichEdit_AppendTextColored(Const ByRef $__sText, Const ByRef $__iColor, Const ByRef $__char_attribute)
    _GUICtrlRichEdit_SetTextColor(_ColorSetCOLORREF(_ColorGetRGB($__iColor)))
    _GUICtrlRichEdit_SetFont($__hWnd_richedit, $__console_font_size, $__console_font_name)
    _GUICtrlRichEdit_SetCharAttributes($__hWnd_richedit, $__char_attribute)
    _GUICtrlRichEdit_AppendText($__hWnd_richedit, $__sText)
    _GUICtrlRichEdit_ScrollLines($__hWnd_richedit, 1)
EndFunc   ;==>_GUICtrlRichEdit_AppendTextColored

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlRichEdit_SetTextColor
; Description ...: If any text is highlighted it changes the highlighted text's color, otherwise changes all text color at the
;                   current caret position.
; Syntax ........: _GUICtrlRichEdit_SetTextColor(Const ByRef $__iColor)
; Parameters ....: $__iColor    - Hex color to set the text.
; Return values .: None.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func _GUICtrlRichEdit_SetTextColor(Const ByRef $__iColor)
    Local $__tCharFormat = DllStructCreate($tagCHARFORMAT)

    DllStructSetData($__tCharFormat, 1, DllStructGetSize($__tCharFormat))
    DllStructSetData($__tCharFormat, 2, $CFM_COLOR)
    DllStructSetData($__tCharFormat, 6, $__iColor)

    _SendMessage($__hWnd_richedit, $EM_SETCHARFORMAT, $SCF_SELECTION, DllStructGetPtr($__tCharFormat))
EndFunc   ;==>_GUICtrlRichEdit_SetTextColor

; #FUNCTION# ====================================================================================================================
; Name ..........: __CONSOLE_WM_SIZE
; Description ...: Resizes the RichEdit control to match the Console Window when it is being resized.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func __CONSOLE_WM_SIZE($hWndFrom, $iMsg, $wParam, $lParam)
    #forceref $hWndFrom, $iMsg, $wParam, $lParam
    Local $__iWidth = _WinAPI_LoWord($lParam)
    Local $__iHeight = _WinAPI_HiWord($lParam)

    Switch ($hWndFrom)
        Case $__hWnd_console
            _WinAPI_MoveWindow($__hWnd_richedit, 0, 0, $__iWidth + 1, $__iHeight + 1)
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>__CONSOLE_WM_SIZE

; #FUNCTION# ====================================================================================================================
; Name ..........: __WM_GETMINMAXINFO
; Description ...: Prevents Console Window from going below 400px wide and 50px tall.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func __WM_GETMINMAXINFO($hWndFrom, $iMsg, $wParam, $lParam)
    #forceref $hWndFrom, $iMsg, $wParam, $lParam
    Local $tagMaxinfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int", $lParam)

    Switch ($hWndFrom)
        Case $__hWnd_console
            DllStructSetData($tagMaxinfo, 7, 400) ; min X
            DllStructSetData($tagMaxinfo, 8, 50) ; min Y
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>__WM_GETMINMAXINFO

; #FUNCTION# ====================================================================================================================
; Name ..........: __CONSOLE_WM_NOTIFY
; Description ...: WM_NOTIFY function to track popup menu.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func __CONSOLE_WM_NOTIFY($hWndFrom, $iMsg, $wParam, $lParam)
    #forceref $iMsg, $wParam
    Local $__tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $__iCode = DllStructGetData($__tNMHDR, "Code")
    Local $__hWnd = HWnd(DllStructGetData($__tNMHDR, "hWndFrom"))
    Local $__tMsgFilter = DllStructCreate($tagMSGFILTER, $lParam)

    Switch $__hWnd
        Case $__hWnd_richedit
            Select
                Case $__iCode = $EN_MSGFILTER
                    If DllStructGetData($__tMsgFilter, "msg") = $WM_RBUTTONUP Then
                        _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($__hWnd_ctxt_menu), $hWndFrom)
                    EndIf
            EndSelect
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>__CONSOLE_WM_NOTIFY

; #FUNCTION# ====================================================================================================================
; Name ..........: __CONSOLE_WM_COMMAND
; Description ...: WM_COMMAND function for menu items.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func __CONSOLE_WM_COMMAND($hWndFrom, $iMsg, $wParam, $lParam)
    Local $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word
    Local $iCode = BitShift($wParam, 16) ; Hi Word
    Local static $__current_file_path
    Local $__user_input_path

    Switch ($hWndFrom)
        Case $__hWnd_console
            Switch ($iIDFrom)
                Case $__idOpen
                    $__user_input_path = FileOpenDialog("Select console output", @ScriptDir & "\", "All (*.*)|Rtf (*.rtf)|Text Files (*.txt)")
                    If (@error) Then
                        ConsoleOut((@error = 1 ? "File selection failed" : "Bad file filter") & @CRLF)
                    Else
                        $__current_file_path = $__user_input_path
                        ConsoleSetFromFile($__current_file_path)
                    EndIf
                Case $__idSave
                    If ($__current_file_path = "") Then
                        $__user_input_path = FileSaveDialog("Save console output", @ScriptDir & "\", "All (*.*)|Rtf (*.rtf)|Text Files (*.txt)")
                        If (@error) Then
                            ConsoleOut((@error = 1 ? "File selection failed" : "Bad file filter") & @CRLF)
                        Else
                            $__current_file_path = $__user_input_path
                        EndIf
                    EndIf
                    ConsoleSaveToFile($__current_file_path)
                Case $__idSaveAs
                    $__user_input_path = FileSaveDialog("Save console output", @ScriptDir & "\", "All (*.*)|Rtf (*.rtf)|Text Files (*.txt)")
                    If (@error) Then
                        ConsoleOut((@error = 1 ? "File selection failed" : "Bad file filter") & @CRLF)
                    Else
                        $__current_file_path = $__user_input_path
                        ConsoleSaveToFile($__current_file_path)
                    EndIf
                Case $__idClear
                    ConsoleClear()
                Case $__idCopyPlain
                    ClipPut(_GUICtrlRichEdit_GetText($__hWnd_richedit))
                Case $idCopyRtf
                    ClipPut(StringReplace(_GUICtrlRichEdit_StreamToVar($__hWnd_richedit, True), "\par", "", -1))
                Case $__idUndo
                    _GUICtrlRichEdit_StreamFromVar($__hWnd_richedit, $__rtf_console_text)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>__CONSOLE_WM_COMMAND

; #FUNCTION# ====================================================================================================================
; Name ..........: __CONSOLE_WNDPROC
; Description ...: WindowProc that can be used for the popup menu.
; Remarks .......: This does the same thing as WM_COMMAND and WM_NOTIFY. I just like the other context menu better, no other reason.
; Author ........: InunoTaishou
; ===============================================================================================================================
Func __CONSOLE_WNDPROC($hWndFrom, $iMsg, $wParam, $lParam)
    Local Static $__current_file_path = ""
    Local $__user_input_path = ""

    Switch ($hWndFrom)
        Case $__hWnd_richedit
            Switch ($iMsg)
                Case $WM_RBUTTONUP
                    _GUICtrlMenu_TrackPopupMenu($__hWnd_ctxt_menu, $hWndFrom)
                    Return 0
                Case $WM_COMMAND
                    Switch $wParam
                        Case $idOpen
                            $__user_input_path = FileOpenDialog("Select console output", @ScriptDir & "\", "All (*.*)|Rtf (*.rtf)|Text Files (*.txt)")
                            If (@error) Then
                                ConsoleOut((@error = 1 ? "File selection failed" : "Bad file filter") & @CRLF)
                            Else
                                $__current_file_path = $__user_input_path
                                ConsoleSetFromFile($__current_file_path)
                            EndIf
                        Case $idSave
                            If ($__current_file_path = "") Then
                                $__user_input_path = FileSaveDialog("Save console output", @ScriptDir & "\", "All (*.*)|Rtf (*.rtf)|Text Files (*.txt)")
                                If (@error) Then
                                    ConsoleOut((@error = 1 ? "File selection failed" : "Bad file filter") & @CRLF)
                                Else
                                    $__current_file_path = $__user_input_path
                                EndIf
                            EndIf
                            ConsoleSaveToFile($__current_file_path)
                        Case $idSaveAs
                            $__user_input_path = FileSaveDialog("Save console output", @ScriptDir & "\", "All (*.*)|Rtf (*.rtf)|Text Files (*.txt)")
                            If (@error) Then
                                ConsoleOut((@error = 1 ? "File selection failed" : "Bad file filter") & @CRLF)
                            Else
                                $__current_file_path = $__user_input_path
                                ConsoleSaveToFile($__current_file_path)
                            EndIf
                        Case $idClear
                            ConsoleClear()
                        Case $idCopyPlain
                            ClipPut(_GUICtrlRichEdit_GetText($__hWnd_richedit))
                        Case $idCopyRtf
                            ClipPut(StringReplace(_GUICtrlRichEdit_StreamToVar($__hWnd_richedit, True), "\par", "", -1))
                        Case $idUndo
                            ;_GUICtrlRichEdit_StreamFromVar($__hWnd_richedit, $__rtf_console_text)
                            _GUICtrlRichEdit_Undo($__hWnd_richedit)
                    EndSwitch
            EndSwitch
    EndSwitch
    Return _WinAPI_CallWindowProc($__ptr_procold, $hWndFrom, $iMsg, $wParam, $lParam)
EndFunc   ;==>__CONSOLE_WNDPROC
#EndRegion Internal-Functions

Here's what it looks like

bZQhPlR.png

Edited by InunoTaishou
1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now