_GUICtrlEdit: bestimmte Zeichen verbieten; Eingabe bestimmter Zeichen verhindern

  • Versucht man in Windows eine Datei umzubenennen und gibt ein unzulässiges Sonderzeichen ein, wird die Eingabe des Zeichens verhindert und ein Tooltipp mit den verbotenen Zeichen wird angezeigt.

    Diese Funktionsweise habe ich für ein Editfeld nachgebaut. Vielleicht kann es jemand brauchen.


    Für das folgende Beispielscript habe ich die Beispielfunktion von _GUICtrlEdit_Create abgeändert.

    [autoit]

    #include <GuiEdit.au3>
    #include <WinAPI.au3> ; used for Lo/Hi word
    #include <WindowsConstants.au3>
    #include <GUIConstantsEx.au3>

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

    $Debug_Ed = False ; Check ClassName being passed to Edit functions, set to True and use a handle to another control to see it work

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

    Global $hEdit
    Global Const $restricted = "\ / : * "" < > |"

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

    _Example1()

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

    Func _Example1()
    Local $hGUI

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

    ; Create GUI
    $hGUI = GUICreate("Edit Create", 400, 300)
    $hEdit = _GUICtrlEdit_Create($hGUI, "This is a test" & @CRLF & "Another Line", 2, 2, 394, 268)
    GUISetState()

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

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

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

    _GUICtrlEdit_AppendText($hEdit, @CRLF & "Append to the end?")

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

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
    EndFunc ;==>_Example1

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

    Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit
    If Not IsHWnd($hEdit) Then $hWndEdit = GUICtrlGetHandle($hEdit)
    $hWndFrom = $ilParam
    $iIDFrom = _WinAPI_LoWord($iwParam)
    $iCode = _WinAPI_HiWord($iwParam)
    Switch $hWndFrom
    Case $hEdit, $hWndEdit
    Switch $iCode
    Case $EN_UPDATE ; Sent when an edit control is about to redraw itself
    Local $replace = "", $pos = 0
    For $i = 1 To StringLen(_GUICtrlEdit_GetText($hEdit))
    If StringInStr(StringStripWS($restricted, 8), StringMid(_GUICtrlEdit_GetText($hEdit), $i, 1)) = 0 Then
    $replace &= StringMid(_GUICtrlEdit_GetText($hEdit), $i, 1)
    Else
    $pos = $i
    EndIf
    Next

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

    If $pos > 0 Then
    _GUICtrlEdit_SetText($hEdit, $replace)
    _GUICtrlEdit_SetSel($hEdit, $pos-1,$pos-1)
    _GUICtrlEdit_ShowBalloonTip($hEdit, "", "verboten: " & @CR & $restricted, $TTI_NONE)
    EndIf
    ; no return value
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_COMMAND

    [/autoit]