Sign in to follow this  
Followers 0
iCode

Line Wrap for AutoIt Edit Control (_EditCtrl_ToggleLineWrap)

4 posts in this topic

#1 ·  Posted (edited)

the concept comes from someone else here, though i forget who (if you, let me know and i'll be glad to give credit)
 
rather than toggling line wrapping on and off in an AutoIt multi-line edit control, which apparently is not doable, this function destroys and recreates the control, preserving various aspects of the original
 
it works by creating the new control with or without a horizontal scroll bar ($WS_HSCROLL)
 
i'm no pro, so critique is welcome :)

#include-once
#include <WinAPI.au3>
#include <GUIEdit.au3>
#include <WindowsConstants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _EditCtrl_ToggleLineWrap
; Version .......; 1.1 (29-Jan-2014)
; Description ...: toggle line wrapping for an AutoIt multi-line edit control
; Syntax ........: _EditCtrl_ToggleLineWrap($hWnd, $iCtrlID[, $iResize = $GUI_DOCKAUTO[, $iChars = 999999[, $sFont = ""[, $hexColor = 0x000000[, $hexBgColor = 0xFFFFFF]]]]])
; Parameters ....: $hWnd                - handle to parent window
;                  $iCtrlID             - edit control ID
;                  $iResize             - [optional] (integer) edit control resizing value for GUICtrlSetResizing() - def=$GUI_DOCKAUTO
;                  $iChars              - [optional] (integer) edit control max chars - def=999999
;                  $sFont               - [optional] (string) "|" seperated string 4 of parameters for GUICtrlSetFont() (size, weight, attribute, fontname) - def=""
;                  $hexColor              - [optional] (hex) font color - def=0x000000 (black)
;                  $hexBgColor          - [optional] (hex) edit control background color - def=0xFFFFFF (white)
; Return values .: -1 = Error
;                  : 0 = Wrapping off
;                  : 1 = Wrapping on
; Author ........: iCode
; Modified ......: 1-JAN-2014
; Remarks .......: tested with AutoIt 3.3.10.2
;                  : it appears that if the edit control is too close to the GUI border, it can be re-created with entirely wrong dimentions
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _EditCtrl_ToggleLineWrap($hWnd, $iCtrlID, $iResize = 1, $iChars = 999999, $sFont = "", $hexColor = 0x000000, $hexBgColor = 0xFFFFFF)

    Local $return, $aFont
    Local $string = GUICtrlRead($iCtrlID)
    Local $aCtrlPos = ControlGetPos($hWnd, "", $iCtrlID)
    If @error Or $aCtrlPos[2] < 1 Or $aCtrlPos[3] < 1 Then Return -1
    Local $hCtrl = GUICtrlGetHandle($iCtrlID)
    If $hCtrl = 0 Then Return -1
    Local $ctrlStyle = _WinAPI_GetWindowLong($hCtrl, $GWL_STYLE)
    If @error Then Return -1
    Local $ctrlExStyle = _WinAPI_GetWindowLong($hCtrl, $GWL_EXSTYLE)
    If @error = 10 Then $ctrlExStyle = 0
    Local $iMod = _GUICtrlEdit_GetModify($hCtrl)
    Local $aSel = _GUICtrlEdit_GetSel($hCtrl)
    Local $iLines = _GUICtrlEdit_GetFirstVisibleLine($hCtrl)
    GUICtrlDelete($iCtrlID)
    If BitAND($ctrlStyle, $WS_HSCROLL) Then
        $iCtrlID = GUICtrlCreateEdit("", $aCtrlPos[0], $aCtrlPos[1], $aCtrlPos[2], $aCtrlPos[3], $ctrlStyle - $WS_HSCROLL, $ctrlExStyle)
        $return = 1
    Else
        $iCtrlID = GUICtrlCreateEdit("", $aCtrlPos[0], $aCtrlPos[1], $aCtrlPos[2], $aCtrlPos[3], $ctrlStyle + $WS_HSCROLL, $ctrlExStyle)
        $return = 0
    EndIf

    GUICtrlSetResizing($iCtrlID, $iResize)
    GUICtrlSetBkColor($iCtrlID, $hexBgColor)
    GUICtrlSetColor($iCtrlID, $hexColor)
    If $sFont <> "" Then
        $aFont = StringSplit($sFont, "|")
        If $aFont[0] = 4 Then
            GUICtrlSetFont($iCtrlID, $aFont[1], $aFont[2], $aFont[3], $aFont[4])
        EndIf
    EndIf
    GUICtrlSetLimit($iCtrlID, $iChars)
    GUICtrlSetData($iCtrlID, $string)
    _GUICtrlEdit_LineScroll($iCtrlID, 0, $iLines)
    _GUICtrlEdit_SetSel($iCtrlID, $aSel[0], $aSel[1])
    _GUICtrlEdit_SetModify($iCtrlID, $iMod)

    Return $return

EndFunc

CHANGE LOG

29-Jan-2014

* fixed a bug that could cause incorrect extra style to be applied to edit control

1-Jan-2014

* restores the scroll position to the current line number

* very minor changes

29-Dec-2013

* swapped the return values - they were backwards

28-Dec-2013

* include Martins suggestion to restore character selection

* added check of edit control size to avoid problems if the control is less than 1x1 px

Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites



One thing I included which you might want to consider is to allow for some text being selected, and setting where the cursor is when the user switches wrapping.

$range = _GUICtrlEdit_GetSel($iCtrlID)
;delete the edit
 
;create the new edit
_GUICtrlEdit_SetSel($iCtrlID, $range[0], $range[1])

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

thank martin!

done

was it you who i probably scarfed this idea from?


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites

I don't think so because the script here I used the same idea I have never published.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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
Sign in to follow this  
Followers 0

  • Similar Content

    • dreamzboy
      By dreamzboy
      I have a list box control that allows me to see the output as the script runs.  Since I control those outputs, I want the message to display neatly inside the list box without scrolling all the way to the right to read the text.  I don't need anything fancy that calculates the length as I can just send a message from a new line.  Sound simple enough but I'm having trouble getting the function to issue @CRLF and indent properly.  I've already read through >this thread but it's not what I was looking for.
      This line did not enter a new line.
      GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & " Error - One or more fields doesn't match!" & @CRLF & " Remove white space and/or extra entry before trying again." & @CRLF) While this line only gave me 1 tab.
      GUICtrlSetData ($log_label, @TAB & @TAB & @TAB & " Remove white space and/or extra entry before trying again." & @CRLF) Also tried the below but no help either.  It complained about the $aTabs.  I declared $aTabs[1] = [100] at the top (commented out) when trying this method.
      GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & "  Error - One or more fields doesn't match!" & @CRLF) _GUICtrlListBox_SetTabStops ($log_label, $aTabs) _GUICtrlListBox_AddString ($log_label, @TAB & "  Remove white space and/or extra entry before trying again." & @CRLF) Here's my code.  All you have to do is click on the "Generate" button and you'll see what I mean.
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <ButtonConstants.au3> #include <GuiListBox.au3> #include <array.au3> #include <constants.au3> Opt ("TrayIconDebug", 1) Local $ribcl_temp = '', $xml_temp, $i, $j Local $hn_input, $ip_input, $pw_input Local $host_Count, $ip_Count, $pw_Count Local $msg, $success, $password, $hostname, $ip_address Local $log_label, $generate_btn, $clrLog_btn Local $test_str0 = "dl360g7-25" & @CRLF & "dl360g7-26" Local $test_str1 = "10.112.15.22" & @CRLF & "10.112.15.23" & @CRLF & "10.112.15.24" Local $test_str2 = "pass1" & @CRLF & "pass2" & @CRLF & "pass3" ;Local $aTabs[1] = [100] GUICreate ("Test", 600, 450) GUICtrlCreateLabel ("1. Enter hostname (ex: dl360g7-25):", 30, 70, 250, 20) GUICtrlSetFont (-1, 10) $hn_input = GUICtrlCreateEdit ("", 30, 90, 215, 70) GUICtrlSetData ($hn_input, $test_str0) GUICtrlCreateLabel ("2. Enter iLO static IP address:", 30, 180, 250, 20) GUICtrlSetFont (-1, 10) $ip_input = GUICtrlCreateEdit ("", 30, 200, 215, 70) GUICtrlSetData ($ip_input, $test_str1) GUICtrlCreateLabel ("3. Enter iLO default password:", 30, 290, 200, 20) GUICtrlSetFont (-1, 10) $pw_input = GUICtrlCreateEdit ("", 30, 310, 215, 70) GUICtrlSetData ($pw_input, $test_str2) $log_label = GUICtrlCreateList ("", 300, 90, 270, 292, BitOR($WS_BORDER, $WS_VSCROLL, $WS_HSCROLL)) $generate_btn = GUICtrlCreateButton ("Generate XML File", 55, 400, 150, 30) GUICtrlSetFont (-1, 10) GUICtrlCreateLabel ("version 2.0", 530, 425, 70) GUICtrlSetColor (-1, 0x0000cd) $clrLog_btn = GUICtrlCreateButton ("Clear Log", 385, 400, 100, 30) GUICtrlSetFont (-1, 10) GUISetState () Do $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exit Case $msg = $generate_btn ; Check entry simliarity on all fields $hostname = StringSplit (GUICtrlRead ($hn_input), @CRLF, 3) ;MsgBox (0, "Before", $hostname) $hostname = StringStripWS ($hostname, 3) ;MsgBox (0, "After", $hostname) $host_Count = UBound ($hostname) $ip_address = StringSplit (GUICtrlRead ($ip_input), @CRLF, 3) $ip_Count = UBound ($ip_address) $password = StringSplit (GUICtrlRead ($pw_input), @CRLF, 3) $pw_Count = UBound ($password) If ($host_Count = $ip_Count And $ip_Count = $pw_Count And $host_Count = $pw_Count) Then ;Msgbox($mb_ok,'TRUE', $ip_Count & @crlf & $host_Count & @crlf & $pw_Count) GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & " It works!") For $i = 0 To $host_Count - 1 ;MsgBox (0, "", $hostname[$i] GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & " " & $hostname[$i] & @CRLF) GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & " " & $ip_address[$i] & @CRLF) GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & " " & $password[$i] & @CRLF) Next Else ;Msgbox($mb_ok,'FALSE', $ip_Count & @crlf & $host_Count & @crlf & $pw_Count) GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & " Error - One or more fields doesn't match!" & @CRLF & " Remove white space and/or extra entry before trying again." & @CRLF) GUICtrlSetData ($log_label, @TAB & @TAB & @TAB & " Remove white space and/or extra entry before trying again." & @CRLF) Endif #cs Else GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & " Error - One or more fields doesn't match!") MsgBox (48, "Error", "One or more fields does not match! Please check the entries and try again.") EndIf #ce _GUICtrlListBox_UpdateHScroll ($log_label) Sleep (1000) Case $msg = $clrLog_btn GUICtrlSetData ($log_label, "") ; Clear log file EndSelect Until 0 Cheers,
      Dreamzboy
    • UEZ
      By UEZ
      Hi,

      I'm surprised about the behavior of controlsend to the edit control when using subclassing:


      #include <constants.au3> #include <windowsconstants.au3> #include <gdiplus.au3> #include <guiconstantsex.au3> _GDIPlus_Startup() Global Const $hGUI = GUICreate("Test", 602, 198, 192, 124) Global Const $idEdit = GUICtrlCreateEdit("", 0, 0, 601, 145) GUICtrlSetFont($idEdit, 10, 400, 0, "Lucida Console") Global Const $hEdit = GUICtrlGetHandle($idEdit) Global Const $idButton1 = GUICtrlCreateButton("Send 2 Edit", 8, 160, 75, 25) Global Const $idButton2 = GUICtrlCreateButton("Exit", 520, 160, 75, 25) Global Const $hBitmap = CreateCaretBitmap() Global Const $iBlinkTime_save = DllCall('user32.dll', 'int', 'GetCaretBlinkTime') Global Const $iBlinkTime = Int($iBlinkTime_save[0] * 0.9) Global Const $hCaretProc = DllCallbackRegister("CaretProc", "ptr", "hwnd;uint;long;ptr") Global Const $hOldCaretProc = _WinAPI_SetWindowLong($hEdit, $GWL_WNDPROC, DllCallbackGetPtr($hCaretProc)) GUISetState(@SW_SHOW) Global $hEditWndProc = DllCallbackRegister("EditWndProc", "long", "hwnd;uint;wparam;lparam") Global $hOldEditProc = _WinAPI_SetWindowLong($hEdit, $GWL_WNDPROC, DllCallbackGetPtr($hEditWndProc)) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idButton2 DllCall('user32.dll', 'int', 'SetCaretBlinkTime', 'uint', $iBlinkTime_save[0]) _WinAPI_SetWindowLong($hEdit, $GWL_WNDPROC, $hOldCaretProc) DllCallbackFree($hCaretProc) _WinAPI_DeleteObject($hBitmap) _WinAPI_SetWindowLong($hEdit, $GWL_WNDPROC, $hOldEditProc) DllCallbackFree($hEditWndProc) _GDIPlus_Shutdown() Exit Case $idButton1 ControlFocus($hGUI, "", $idEdit) ControlSend($hGUI, "", $idEdit, "This is a TEST" & @LF) EndSwitch WEnd Func EditWndProc($hWnd, $iMsg, $wParam, $lParam) Switch $iMsg Case $WM_KEYDOWN ConsoleWrite("A key was pressed" & @LF) EndSwitch Return _WinAPI_CallWindowProc($hOldEditProc, $hWnd, $iMsg, $wParam, $lParam) EndFunc ;==>EditWndProc Func CaretProc($hWnd, $iMsg, $wParam, $lParam) Local $iRet = _WinAPI_CallWindowProc($hOldCaretProc, $hWnd, $iMsg, $wParam, $lParam) Switch $iMsg Case $WM_SETFOCUS DllCall('user32.dll', 'int', 'CreateCaret', 'hwnd', $hWnd, 'ptr', $hBitmap, 'int', 0, 'int', 0) DllCall('user32.dll', 'int', 'ShowCaret', 'hwnd', $hWnd) DllCall('user32.dll', 'int', 'SetCaretBlinkTime', 'uint', $iBlinkTime) Case $WM_KILLFOCUS DllCall('user32.dll', 'int', 'HideCaret', 'hwnd', $hWnd) DllCall('user32.dll', 'int', 'DestroyCaret') DllCall('user32.dll', 'int', 'SetCaretBlinkTime', 'uint', $iBlinkTime_save[0]) EndSwitch Return $iRet EndFunc Func CreateCaretBitmap($iW = 9, $iH = 14, $iColor = 0xFFC0C0C0) Local Const $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0) Local Const $hBitmap = $aResult[6] Local Const $hCtx = _GDIPlus_ImageGetGraphicsContext($hBitmap) Local Const $hBrush = _GDIPlus_BrushCreateSolid($iColor) _GDIPlus_GraphicsFillRect($hCtx, 0, $iH - 3, $iW, $iH, $hBrush) _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hCtx) Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _GDIPlus_BitmapDispose($hBitmap) Return $hHBitmap EndFunc
      When you press the "Send 2 Edit" the string "This is a TEST" & @LF will be sent to the edit control but reversed!

      When you e.g. disable the 1st DllCallbackRegister() then it is working as expected.

      Any explanation for this behavior?

      Br,
      UEZ