Edit immer so groß wie GUI

  • Hi,
    ich will eine GUI mit einem einzigen Editfeld.
    Dieses soll immer so groß sein wie die GUI.
    Wird beim schreiben eine neue Zeile erreicht, soll sich die GUI samt Editfeld vergrößern.
    Wird eine Zeile gelöscht so soll sich alles verkleinern.
    Es funktioniert eigentlich ganz gut, jedoch wie bekomme ich das Edit genau passend in die GUI?

    Spoiler anzeigen
    [autoit]

    #region ;************ Includes ************
    #include <Constants.au3>
    #include <GuiEdit.au3>
    #include <WindowsConstants.au3>
    #include <GUIConstantsEx.au3>
    #endregion ;************ Includes ************

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

    ;allgemeine Deklarierung (Notizen)
    Global $hProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    Global $hMod = _WinAPI_GetModuleHandle(0)
    Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hProc), $hMod)

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

    ;AutoItSetOption
    Opt("GUICloseOnESC", 0) ;1=ESC beendet, 0=ESC schließt nicht
    Opt('GUIOnEventMode', 1)

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

    Global $iHoehe_beginn = 200

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

    Global $hGui = GUICreate("Notiz", 200, $iHoehe_beginn, -1, -1, $WS_SIZEBOX, $WS_EX_TOOLWINDOW)
    Global $idEdit = GUICtrlCreateEdit("", 0, 0, 200, $iHoehe_beginn, BitOR($ES_MULTILINE, $ES_WANTRETURN, $WS_VSCROLL, $ES_AUTOVSCROLL))

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

    Global $hEdit = GUICtrlGetHandle($idEdit)
    Global $iFontHeight = _GetFontHeight($hEdit)
    Global $iEditH = $iFontHeight * 2
    _CalcEditSize()

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

    GUISetState()

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_beenden")

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

    Func _GetFontHeight($hWnd)
    Local $hDC = _WinAPI_GetDC($hEdit)
    Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    DllCall("gdi32.dll", "bool", "GetTextMetricsW", "handle", $hDC, "ptr", DllStructGetPtr($tTEXTMETRIC))
    _WinAPI_ReleaseDC($hEdit, $hDC)
    Return DllStructGetData($tTEXTMETRIC, "tmAscent")
    EndFunc ;==>_GetFontHeight

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

    Func _CalcEditSize()
    Local $aPos_fenster
    $aPos_fenster = WinGetPos($hGui)

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

    Local $iCnt = _GUICtrlEdit_GetLineCount($hEdit)
    $iEditH = $iCnt * $iFontHeight
    If $iEditH + 50 > $iHoehe_beginn Then
    WinMove($hGui, "", $aPos_fenster[0], $aPos_fenster[1], $aPos_fenster[2], $iEditH + $iFontHeight + 50)
    EndIf
    EndFunc ;==>_CalcEditSize

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

    Func _KeyProc($nCode, $wParam, $lParam)
    Local $aPos_fenster

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

    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    ;~ Local $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)

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

    If $wParam = $WM_KEYDOWN Then
    If ($iEditH + $iFontHeight * 2 + 50) > $iHoehe_beginn Then
    $aPos_fenster = WinGetPos($hGui)
    _WinAPI_SetWindowPos(_WinAPI_GetFocus(), 0, 0, 0, $aPos_fenster[2], $iEditH + $iFontHeight * 2, $SWP_NOZORDER)
    EndIf
    EndIf
    _CalcEditSize()
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndFunc ;==>_KeyProc

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

    While 1
    Sleep(10)
    WEnd

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

    Func _beenden()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hProc)
    Exit
    EndFunc ;==>_beenden

    [/autoit]

    EDIT: Habs ein bisschen optimiert, aber immer noch nicht gut genug :S

  • Irgendwelche Includes vergessen, oder gibts diese Konstante nur in aktuellen Autoit Versionen?

    Code
    `C:\Users\*******\Desktop\AutoIt v3 Script (neu) (2).au3(35,56) : WARNING: $tagTEXTMETRIC: possibly used before declaration.
        Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    C:\Users\********\Desktop\AutoIt v3 Script (neu) (2).au3(35,56) : ERROR: $tagTEXTMETRIC: undeclared global variable.
        Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

    Wie dem auch sei, wäre nett, wenn du diese noch nachliefern könntest.

  • In 3.3.8.1 schon ;).
    Du kannst aber einfach diese Zeile in das Script einfügen wenn du nicht updaten willst:

    Spoiler anzeigen
    [autoit]

    ; ===============================================================================================================================
    ; *******************************************************************************************************************************
    ; GetTextMetrics Structures
    ; *******************************************************************************************************************************
    ; ===============================================================================================================================
    ; #STRUCTURE# ===================================================================================================================
    ; Name...........: $tagTEXTMETRIC
    ; Description ...: Contains basic information about a physical font. All sizes are specified in logical units, that is, they depend on the current mapping mode of the display context.
    ; Fields ........: tmHeight - Specifies the height (ascent + descent) of characters.
    ; tmAscent - Specifies the ascent (units above the base line) of characters.
    ; tmDescent - Specifies the descent (units below the base line) of characters.
    ; tmInternalLeading - Specifies the amount of leading (space) inside the bounds set by the tmHeight member.
    ; | Accent marks and other diacritical characters may occur in this area. The designer may set this member to zero.
    ; tmExternalLeading - Specifies the amount of extra leading (space) that the application adds between rows.
    ; | Since this area is outside the font, it contains no marks and is not altered by text output calls in either OPAQUE or TRANSPARENT mode.
    ; | The designer may set this member to zero.
    ; tmAveCharWidth - Specifies the average width of characters in the font (generally defined as the width of the letter x).
    ; | This value does not include the overhang required for bold or italic characters.
    ; tmMaxCharWidth - Specifies the width of the widest character in the font.
    ; tmWeight - Specifies the weight of the font.
    ; tmOverhang - Specifies the extra width per string that may be added to some synthesized fonts.
    ; | When synthesizing some attributes, such as bold or italic, graphics device interface (GDI) or a device may have to add width to a string on both a per-character and per-string basis.
    ; | For example, GDI makes a string bold by expanding the spacing of each character and overstriking by an offset value
    ; | it italicizes a font by shearing the string. In either case, there is an overhang past the basic string.
    ; | For bold strings, the overhang is the distance by which the overstrike is offset. For italic strings, the overhang is the amount the top of the font is sheared past the bottom of the font.
    ; | The tmOverhang member enables the application to determine how much of the character width returned by a GetTextExtentPoint32 function call on a single character is the actual character width and how much is the per-string extra width.
    ; | The actual width is the extent minus the overhang.
    ; tmDigitizedAspectX - Specifies the horizontal aspect of the device for which the font was designed.
    ; tmDigitizedAspectY - Specifies the vertical aspect of the device for which the font was designed.
    ; | The ratio of the tmDigitizedAspectX and tmDigitizedAspectY members is the aspect ratio of the device for which the font was designed.
    ; tmFirstChar - Specifies the value of the first character defined in the font.
    ; tmLastChar - Specifies the value of the last character defined in the font.
    ; tmDefaultChar - Specifies the value of the character to be substituted for characters not in the font.
    ; tmBreakChar - Specifies the value of the character that will be used to define word breaks for text justification.
    ; tmItalic - Specifies an italic font if it is nonzero.
    ; tmUnderlined - Specifies an underlined font if it is nonzero.
    ; tmStruckOut - Specifies a strikeout font if it is nonzero.
    ; tmPitchAndFamily - Specifies information about the pitch, the technology, and the family of a physical font.
    ; The four low-order bits of this member specify information about the pitch and the technology of the font. A constant is defined for each of the four bits.
    ; $TMPF_FIXED_PITCH If this bit is set the font is a variable pitch font. If this bit is clear the font is a fixed pitch font. Note very carefully that those meanings are the opposite of what the constant name implies.
    ; $TMPF_VECTOR If this bit is set the font is a vector font.
    ; $TMPF_TRUETYPE If this bit is set the font is a TrueType font.
    ; $TMPF_DEVICE If this bit is set the font is a device font.
    ; tmCharSet - Specifies the character set of the font. The character set can be one of the following values.
    ; |ANSI_CHARSET
    ; |BALTIC_CHARSET
    ; |CHINESEBIG5_CHARSET
    ; |DEFAULT_CHARSET
    ; |EASTEUROPE_CHARSET
    ; |GB2312_CHARSET
    ; |GREEK_CHARSET
    ; |HANGUL_CHARSET
    ; |MAC_CHARSET
    ; |OEM_CHARSET
    ; |RUSSIAN_CHARSET
    ; |SHIFTJIS_CHARSET
    ; |SYMBOL_CHARSET
    ; |TURKISH_CHARSET
    ; |VIETNAMESE_CHARSET
    ; Author ........: Gary Frost
    ; Remarks .......:
    ; ===============================================================================================================================
    Global Const $tagTEXTMETRIC = "long tmHeight;long tmAscent;long tmDescent;long tmInternalLeading;long tmExternalLeading;" & _
    "long tmAveCharWidth;long tmMaxCharWidth;long tmWeight;long tmOverhang;long tmDigitizedAspectX;long tmDigitizedAspectY;" & _
    "wchar tmFirstChar;wchar tmLastChar;wchar tmDefaultChar;wchar tmBreakChar;byte tmItalic;byte tmUnderlined;byte tmStruckOut;" & _
    "byte tmPitchAndFamily;byte tmCharSet"

    [/autoit]
  • Habs nochmal verbessert.
    Sieht schon besser aus.
    Kann man noch was optimieren :?:
    Beim größer- bzw. kleinerziehen der GUI flackert das ganze ca. 1x pro Sekunde. Bekommt man dies weg :?:

    Spoiler anzeigen
    [autoit]

    #region ;************ Includes ************
    #include <Constants.au3>
    #include <GuiEdit.au3>
    #include <WindowsConstants.au3>
    #include <GUIConstantsEx.au3>
    #endregion ;************ Includes ************

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

    ;allgemeine Deklarierung (Notizen)
    Global $hProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    Global $hMod = _WinAPI_GetModuleHandle(0)
    Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hProc), $hMod)

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

    ;AutoItSetOption
    Opt("GUICloseOnESC", 0) ;1=ESC beendet, 0=ESC schließt nicht
    Opt('GUIOnEventMode', 1)

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

    Global $iBreite_beginn = 200
    Global $iHoehe_beginn = 200
    Global $iHoehe_min = 100

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

    Global $hGui = GUICreate("Notiz", $iBreite_beginn, $iHoehe_beginn, -1, -1, $WS_SIZEBOX, $WS_EX_TOOLWINDOW)
    Global $idEdit = GUICtrlCreateEdit("", 0, 0, $iBreite_beginn - 2, $iHoehe_beginn - 20, BitOR($ES_MULTILINE, $ES_WANTRETURN, $WS_VSCROLL, $ES_AUTOVSCROLL))

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

    Global $hEdit = GUICtrlGetHandle($idEdit)
    Global $iFontHeight = _GetFontHeight($hEdit)
    Global $iEditH = $iFontHeight * 2
    _CalcEditSize()

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

    GUISetState()

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_beenden")

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

    Func _GetFontHeight($hWnd)
    Local $hDC = _WinAPI_GetDC($hEdit)
    Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    DllCall("gdi32.dll", "bool", "GetTextMetricsW", "handle", $hDC, "ptr", DllStructGetPtr($tTEXTMETRIC))
    _WinAPI_ReleaseDC($hEdit, $hDC)
    Return DllStructGetData($tTEXTMETRIC, "tmAscent")
    EndFunc ;==>_GetFontHeight

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

    Func _CalcEditSize()
    Local $aPos_fenster
    $aPos_fenster = WinGetPos($hGui)

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

    Local $iCnt = _GUICtrlEdit_GetLineCount($hEdit)
    $iEditH = $iCnt * $iFontHeight
    If $iEditH > $iHoehe_min Then
    WinMove($hGui, "", $aPos_fenster[0], $aPos_fenster[1], $aPos_fenster[2], $iEditH + $iFontHeight + 45)
    EndIf
    EndFunc ;==>_CalcEditSize

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

    Func _KeyProc($nCode, $wParam, $lParam)
    Local $aPos_fenster

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

    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    ;~ Local $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)

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

    ;~ If $wParam = $WM_KEYDOWN Then
    ;~ If ($iEditH + $iFontHeight * 2 + 50) > $iHoehe_beginn Then
    ;~ $aPos_fenster = WinGetPos($hGui)
    ;~ _WinAPI_SetWindowPos(_WinAPI_GetFocus(), 0, 0, 0, $aPos_fenster[2], $iEditH + $iFontHeight * 2, $SWP_NOZORDER)
    ;~ EndIf
    ;~ EndIf
    _CalcEditSize()
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndFunc ;==>_KeyProc

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

    While 1
    Sleep(10)
    WEnd

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

    Func _beenden()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hProc)
    Exit
    EndFunc ;==>_beenden

    [/autoit]
    • Offizieller Beitrag

    Probier es mal so, dann flackert eigentlich nix mehr:

    Spoiler anzeigen
    [autoit]

    #region ;************ Includes ************
    #include <Constants.au3>
    #include <GuiEdit.au3>
    #include <WindowsConstants.au3>
    #include <GUIConstantsEx.au3>
    #endregion ;************ Includes ************

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

    ;allgemeine Deklarierung (Notizen)
    Global $hProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    Global $hMod = _WinAPI_GetModuleHandle(0)
    Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hProc), $hMod)

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

    ;AutoItSetOption
    Opt("GUICloseOnESC", 0) ;1=ESC beendet, 0=ESC schließt nicht
    Opt('GUIOnEventMode', 1)

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

    Global $iBreite_beginn = 200
    Global $iHoehe_beginn = 200
    Global $iHoehe_min = 100

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

    Global $hGui = GUICreate("Notiz", $iBreite_beginn, $iHoehe_beginn, -1, -1, Bitor($WS_SIZEBOX,$WS_CLIPCHILDREN,$WS_CLIPSIBLINGS), $WS_EX_TOOLWINDOW)
    Global $idEdit = GUICtrlCreateEdit("", 0, 0, $iBreite_beginn - 2, $iHoehe_beginn - 20, BitOR($ES_MULTILINE, $ES_WANTRETURN, $WS_VSCROLL, $ES_AUTOVSCROLL))

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

    Global $hEdit = GUICtrlGetHandle($idEdit)
    Global $iFontHeight = _GetFontHeight($hEdit)
    Global $iEditH = $iFontHeight * 2
    _CalcEditSize()

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

    GUISetState()

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_beenden")

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

    Func _GetFontHeight($hWnd)
    Local $hDC = _WinAPI_GetDC($hEdit)
    Local $tTEXTMETRIC = DllStructCreate($tagTEXTMETRIC)
    DllCall("gdi32.dll", "bool", "GetTextMetricsW", "handle", $hDC, "ptr", DllStructGetPtr($tTEXTMETRIC))
    _WinAPI_ReleaseDC($hEdit, $hDC)
    Return DllStructGetData($tTEXTMETRIC, "tmAscent")
    EndFunc ;==>_GetFontHeight

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

    Func _CalcEditSize()
    Local $aPos_fenster
    $aPos_fenster = WinGetPos($hGui)

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

    Local $iCnt = _GUICtrlEdit_GetLineCount($hEdit)
    $iEditH = $iCnt * $iFontHeight
    If $iEditH > $iHoehe_min Then
    WinMove($hGui, "", $aPos_fenster[0], $aPos_fenster[1], $aPos_fenster[2], $iEditH + $iFontHeight + 45)
    EndIf
    EndFunc ;==>_CalcEditSize

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

    Func _KeyProc($nCode, $wParam, $lParam)
    Local $aPos_fenster

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

    If $nCode < 0 Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    ;~ Local $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)

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

    ;~ If $wParam = $WM_KEYDOWN Then
    ;~ If ($iEditH + $iFontHeight * 2 + 50) > $iHoehe_beginn Then
    ;~ $aPos_fenster = WinGetPos($hGui)
    ;~ _WinAPI_SetWindowPos(_WinAPI_GetFocus(), 0, 0, 0, $aPos_fenster[2], $iEditH + $iFontHeight * 2, $SWP_NOZORDER)
    ;~ EndIf
    ;~ EndIf
    _CalcEditSize()
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndFunc ;==>_KeyProc

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

    While 1
    Sleep(10)
    WEnd

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

    Func _beenden()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hProc)
    Exit
    EndFunc ;==>_beenden

    [/autoit]
    • Offizieller Beitrag

    C++ API Programmierung ist auch gut für AutoIt. Hab nach fast 2 Jahren endlich den Wälzer durchforstet. :D
    Und siehe da, ich bin gerade bei den Window Styles :P

  • also ein bisschen flackerts noch.
    Wenn im Edit Text drinsteht und man dauern die Enter-Taste gedrückt hält.

    Dies ist zwar minimal, wenn man es jedoch behoben könnte wärs genial :P

  • Naja so wie ich das verstehe änderst du bei jedem Tastendruck die GUI / edit Größe. Beim Dauerdrücken wird die Funktion also entsprechend oft aufgerufen, was man dann als Flackern wahrnimmt. Verhindern könnte man das z.B. durch einen simplen timer, welche eine Aktualisierung vorübergehnd unterbindet (z.B. mind 20ms seit der letzten Aktualisierung). Unterdrückte Aktualisierungen müssen durch eine Flag Variable vermerkt werden. Adlibregister (oder eine if Bedingung in der Hauptschleife) könnte man dann nutzen um bei gesetztem Flag eine nachträgliche Aktualisierung durchzuführen, ansonsten würde die letzte notwendige Aktualisierung evtl. verloren gehen.

  • Vergiss meinen Ansatz, das macht es höchstens schlimmer, weil dann der Scrollbar Balken dauernd sichtbar wird und flackert. Mir ist aber noch etwas anderes aufgefallen. Deine resize Funktion wird öffter ausgeführt als notwendig, auch wenn neue höhe gleich alte höhe wird das Fenster resized. Hab das mal entsprechend geändert:


    [autoit]


    Func _CalcEditSize()
    Local $aPos_fenster
    $aPos_fenster = WinGetPos($hGui)

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

    Local $iCnt = _GUICtrlEdit_GetLineCount($hEdit)
    $iEditH = $iCnt * $iFontHeight
    If $iEditH > $iHoehe_min Then
    $newH = $iEditH + $iFontHeight + 45
    If $newH = $aPos_fenster[3] Then Return ; ohne diese Zeile wird deine Funktion auch ausgeführt wenn neue höhe = alte höhe, siehe consolen output
    ConsoleWrite($newH & " --> " & $aPos_fenster[3] & @CRLF)
    $test = _GUICtrlEdit_BeginUpdate($hEdit)
    ConsoleWrite("return of begin update: " & $test & " / errorcode: " & @error & @CRLF) ; ??????????? scheint nicht zu klappen ???????????
    WinMove($hGui, "", $aPos_fenster[0], $aPos_fenster[1], $aPos_fenster[2],$newH)
    _GUICtrlEdit_EndUpdate($hEdit)
    EndIf
    EndFunc ;==>_CalcEditSize

    [/autoit]

    Dann hatte ich den Gedanken, dass BeginUpdate für das Editcontrol evtl. ein Flackern verhindern könnte, jedoch scheint die Funktion immer False zu liefern und somit wirkungslos zu sein, ich habe keine Ahnung warum.