Sign in to follow this  
Followers 0
Chimp

How to get some parameters about an Edit control?

4 posts in this topic

Given an "Edit control" with Horizontal and Vertical scroll bars, setted with a monospace font,
How can I know:
1) the exact client area dimension (excluding the surface of the Vert and Horiz. scroll bars )
2) the dimensions (in pixel or whatever) of the hidden portions of the "Edit control" that are on the right and/or on the left and/or over the top and/or below the bottom of the visible part?, that is:
if the content of the Edit control is completly visible, then there are not hidden portions, while if the content of the control is biggher of the visible area, then there are hidden parts. How to know the dimensions of the not visible portions of the Edit control  on any given moment while I'm  scrolling vertically and/or horizzontaly or if I change the dimensions of the font as well?
For example, when I set the Font dimension = 7, the whole text is visible, so the scrollbars are grayed out and parameters of the hidden parts should be all 0.
Since I'm sure that my explanation in english is not clear, I post this short script to show in practice the question:
Thanks for any suggestion.

#include <GUIConstants.au3>

HotKeySet("{ESC}", "End")
Local $aData
Local $Char = "         1         2         3         4         5         6         7         8" & @CRLF
$Char &= "12345678901234567890123456789012345678901234567890123456789012345678901234567890" & @CRLF
For $i = 3 To 22
    $Char &= StringFormat('%02i', $i) & _StringReplicate(" ", 7) & "|" & _StringReplicate("         |", 7) & @CRLF
Next

Global $MyGui = GUICreate("", 600, 430)
Global $hEdit1 = GUICtrlCreateEdit($Char, 0, 0, 500, 300, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_READONLY, $ES_MULTILINE))
GUICtrlSetFont(-1, 7, 0, 0, "Courier New")
GUICtrlSetBkColor($hEdit1, 0xffffff)

Local $idRadio1 = GUICtrlCreateRadio("Font size 7", 505, 10, 120, 20)
Local $idRadio2 = GUICtrlCreateRadio("Font size 10", 505, 40, 120, 20)
Local $idRadio3 = GUICtrlCreateRadio("Font size 12", 505, 70, 120, 20)
Local $idRadio4 = GUICtrlCreateRadio("Font size 16", 505, 100, 120, 20)
GUICtrlSetState($idRadio1, $GUI_CHECKED)

GUICtrlCreateLabel("pixel hidden on the left  : " & @CRLF & _
        "pixel hidden on the right : " & @CRLF & _
        "pixel hidden over the top : " & @CRLF & _
        "pixel hidden below bottom : " & @CRLF & _
        "Width of the edit         : " & @CRLF & _
        "Height of the edit        : ", 5, 305, 400, 150)
GUICtrlSetFont(-1, 12, 0, 0, "Courier New")
Local $hParameters = GUICtrlCreateLabel("", 300, 305, 200, 150)
GUICtrlSetFont(-1, 12, 0, 0, "Courier New")
GUISetState()

While 1
    $iMsg = GUIGetMsg()
    Select
        Case $iMsg = $GUI_EVENT_CLOSE
            End()

        Case $iMsg = $idRadio1
            GUICtrlSetFont($hEdit1, 7, 0, 0, "Courier New")

        Case $iMsg = $idRadio2
            GUICtrlSetFont($hEdit1, 10, 0, 0, "Courier New")

        Case $iMsg = $idRadio3
            GUICtrlSetFont($hEdit1, 12, 0, 0, "Courier New")

        Case $iMsg = $idRadio4
            GUICtrlSetFont($hEdit1, 16, 0, 0, "Courier New")
    EndSelect

    $aData = _CheckEdit($hEdit1) ; this should return actual parameters of the Edit <----- ??? how to get this data ???
    GUICtrlSetData($hParameters, $aData[0] & @CRLF & $aData[1] & @CRLF & $aData[2] & @CRLF & $aData[3] & @CRLF & $aData[4] & @CRLF & $aData[5])

WEnd

Func _CheckEdit($hEdit1) ; <----- ??? how to get this data about the passed Edit ???
    Local $aData[6]
    $aData[0] = "???" ; Hidden pixels on the left
    $aData[1] = "???" ; Hidden pixels on the right
    $aData[2] = "???" ; Hidden pixels over the top
    $aData[3] = "???" ; Hidden pixels below bottom
    $aData[4] = "???" ; Width of the visible portion of the Edit (without scrollbar surface)
    $aData[5] = "???" ; Height of the visible portion of the Edit (without scrollbar surface)
    Return $aData
EndFunc   ;==>_CheckEdit


; returns one or more chars replicated n times
; Example: ConsoleWrite(_StringReplicate('*', 5) & @CRLF)
Func _StringReplicate($sChars = "", $iRepeats = 0)
    $sChars = String($sChars)
    $iRepeats = Int(Abs(Number($iRepeats)))
    Return StringReplace(StringFormat('%' & $iRepeats & 's', ""), " ", $sChars)
EndFunc   ;==>_StringReplicate

Func End()
    If WinActive("[ACTIVE]") = $MyGui Then
        Exit
    EndIf
EndFunc   ;==>End

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hopefully this will get you started. I got the width and height of the visible client area of your edit control and the width and height of the hidden text but I didn't finish calculating the hidden text on the left/right/top/bottom.

; Include these at the top
#include <GUIEdit.au3>
#include <Math.au3>

Func _CheckEdit($hEdit1) ; <----- ??? how to get this data about the passed Edit ???
    Local $aEditRect = _GUICtrlEdit_GetRECT(GUICtrlGetHandle($hEdit1))
    Local $iWidth = $aEditRect[2] - $aEditRect[0]
    Local $iHeight = $aEditRect[3] - $aEditRect[1]
    Local $aClientText = _GUICtrlEdit_GetTextSize($hEdit1)
    Local $aData[6]
    $aData[0] = "???" ; Hidden pixels on the left
    $aData[1] = $aClientText[0] - $iWidth ; Hidden pixels on the right
    $aData[2] = $aClientText[1] - $iHeight ; Hidden pixels over the top
    $aData[3] = "???" ; Hidden pixels below bottom
    $aData[4] = $iWidth ; Width of the visible portion of the Edit (without scrollbar surface)
    $aData[5] = $iHeight ; Height of the visible portion of the Edit (without scrollbar surface)
    Return $aData
EndFunc   ;==>_CheckEdit

Func _GUICtrlEdit_GetTextSize($iCtrlId)
    Local $hWnd = GUICtrlGetHandle($iCtrlId)
    Local $hFont = _SendMessage($hWnd, $WM_GETFONT)
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $iLineCount = _GUICtrlEdit_GetLineCount($hWnd)
    Local $aReturn[2] = [0, 0]

    _WinAPI_SelectObject($hDC, $hFont)

    For $iLine = 1 To $iLineCount
        Local $sText = _GUICtrlEdit_GetLine($hWnd, $iLine)
        Local $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sText)
        Local $iWidth = DllStructGetData($tSize, 1)
        Local $iHeight = DllStructGetData($tSize, 2)

        ; Get the higher of the two values, current $iWidth or the saved width stored in $aReturn[0]
        $aReturn[0] = _Max($iWidth, $aReturn[0])
        $aReturn[1] += $iHeight
    Next

    _WinAPI_SelectObject($hDC, $hFont)
    _WinAPI_ReleaseDC($hWnd, $hDC)

    ; Return the max width [0] and max height [1] of the edit control
    Return $aReturn
EndFunc   ;==>_GUICtrlEdit_GetTextSize

I was going to look into EM_GETSCROLLPOS and calculate the current scroll position of the bars but I need to eat some breakfast. If no one posts anything later I'll see if I can finish it.

Edited by InunoTaishou

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

This should do it. The hidden left/top might be off a few pixels because I think the control is accounting for the padding where the text is started (Since the letters aren't directly next to the left side of the edit control and they're not directly below the top of the edit control). Should accomplish what you're looking for.

Just a few notes:

Spoiler

I used _Max(Value, 0) for the $aData[n] values because it will produce a negative value for the right/bottom. It will produce a negative value because the text might not go all the way to the right of the line or the text might not go all the way to the bottom. Thus, producing a negative value.

I tried using _GUIScrollBars_GetScrollBarInfoEx first, and it worked pretty well, but it wouldn't calculate the right/bottom properly. Per 1px font size increase it was off by about 44px. Couldn't determine why. (It looked cleaner and calculated the left/top correctly but not the right/bottom)

_GUICtrlEdit_GetMaxTextSize will return an array with 3 values. The width of the longest line at position 0, the height of the top line to the bottom at position 1, and the height of a single line at position 3. I needed position 3 to calculate the $aData[2] and $aData[3] since nTrackPos is the position in pixels for the horizontal bar but total lines for the vertical bar.

 

#include <GUIEdit.au3>
#include <Math.au3>
#include <GuiScrollBars.au3>
#include <WinApi.au3>

Func _CheckEdit($hEdit1)
    Local $hWnd = GUICtrlGetHandle($hEdit1)
    Local $tRectEdit = _WinAPI_GetClientRect($hWnd)
    Local $iWidth = DllStructGetData($tRectEdit, "Right") - DllStructGetData($tRectEdit, "Left")
    Local $iHeight = DllStructGetData($tRectEdit, "Bottom") - DllStructGetData($tRectEdit, "Top")
    Local $aClientText = _GUICtrlEdit_GetMaxTextSize($hEdit1)
    Local $tScrollInfo = DllStructCreate($tagSCROLLINFO)
    Local $nTrackPosH
    Local $nMaxH
    Local $nTrackPosV
    Local $nMaxV
    Local $aData[6]

    DllStructSetData($tScrollInfo, "cbSize", DllStructGetSize($tScrollInfo))
    DllStructSetData($tScrollInfo, "fMask", $SIF_ALL)
    _GUIScrollBars_GetScrollInfo($hWnd, $SB_HORZ, $tScrollInfo)
    $nTrackPosH = DllStructGetData($tScrollInfo, "nTrackPos")
    $nMaxH = DllStructGetData($tScrollInfo, "nMax")

    DllStructSetData($tScrollInfo, "cbSize", DllStructGetSize($tScrollInfo))
    DllStructSetData($tScrollInfo, "fMask", $SIF_ALL)
    _GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tScrollInfo)
    $tScrollInfo = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT)
    $nTrackPosV = DllStructGetData($tScrollInfo, "nTrackPos")
    $nMaxV = DllStructGetData($tScrollInfo, "nMax")

    $aData[0] = _Max($nTrackPosH, 0)                            ; Hidden pixels on the left
    $aData[1] = _Max($aClientText[0] - $aData[0] - $iWidth, 0)  ; Hidden pixels on the right
    $aData[2] = _Max($nTrackPosV * $aClientText[2], 0)          ; Hidden pixels over the top
    $aData[3] = _Max($aClientText[1] - $aData[2] - $iHeight, 0) ; Hidden pixels below bottom
    $aData[4] = $iWidth                                         ; Width of the visible portion of the Edit (without scrollbar surface)
    $aData[5] = $iHeight                                        ; Height of the visible portion of the Edit (without scrollbar surface)

    Return $aData
EndFunc   ;==>_CheckEdit

Func _GUICtrlEdit_GetMaxTextSize($iCtrlId)
    Local $hWnd = GUICtrlGetHandle($iCtrlId)
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $hFont = _SendMessage($hWnd, $WM_GETFONT)
    Local $hSelectObject = _WinAPI_SelectObject($hDC, $hFont)
    Local $iLineCount = _GUICtrlEdit_GetLineCount($hWnd)
    Local $aReturn[3] = [0, 0, 0]

    For $iLine = 0 To $iLineCount
        Local $sText = _GUICtrlEdit_GetLine($hWnd, $iLine)
        Local $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sText)
        Local $iWidth = DllStructGetData($tSize, 1)
        Local $iHeight = DllStructGetData($tSize, 2)

        $aReturn[0] = _Max($iWidth, $aReturn[0])
        $aReturn[1] += $iHeight
        $aReturn[2] = _Max($iHeight, $aReturn[2])
    Next

    _WinAPI_SelectObject($hDC, $hSelectObject)
    _WinAPI_ReleaseDC($hWnd, $hDC)

    ; $aReturn[0] = Longest width of a line
    ; $aReturn[1] = Total height of the lines top to bottom
    ; $aReturn[3] = Tallest height of a single line
    Return $aReturn
EndFunc   ;==>_GUICtrlEdit_GetMaxTextSize


 

Edited by InunoTaishou
1 person likes this

Share this post


Link to post
Share on other sites

Hi InunoTaishou
Thought it was a little simpler to get those parameters, but i see that it's quite complex (or better, those data is not of immediate availibility)
I like the way you got those infos, and your function is very usefull for me.

Thank You very much!


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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

    • nacerbaaziz
      By nacerbaaziz
      Hi dears
      How are you?
      I have a question, to you please.
      How do I create an edit box for numbers only and does not accept letters? using autoit
      greetings to all
      I hope you help ME
      Thanks to all in advance
    • Simpel
      By Simpel
      Hi,
      I'm trying to create a console reader. All is working but I have a problem with the look. I want the line numbers with another backgroundcolor in silver. I made this with a small silver label. The edit control over the label has a transparent background.
      But if I start the prog then i can see how the line numbers wipe the silver background off. And if I click into the Edit then the silver label disappears completely.
      #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <ColorConstants.au3> #include <StaticConstants.au3> #include <GuiEdit.au3> Opt("GUIOnEventMode", 1) Global $g_iZaehler = 0 Global $g_hGUI = GUICreate("Console: StdoutRead" , 800, 800, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_CLIPCHILDREN, $WS_EX_ACCEPTFILES) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") Global $text = GUICtrlCreateEdit("",10,30,780,760, $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_READONLY + $ES_NOHIDESEL) GUICtrlSetState(-1, $GUI_DROPACCEPTED) GUICtrlSetFont(-1, 9, -1, -1, "Lucida Console") GUICtrlSetResizing(-1, $GUI_DOCKBORDERS) GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT) Global $g_LaZeilen = GUICtrlCreateLabel("Zeilen: " & StringFormat("% 5d", $g_iZaehler), 680, 10, 100, 9, $SS_LEFTNOWORDWRAP) GUICtrlSetFont(-1, 9, -1, -1, "Lucida Console") GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKSIZE) Global $g_hCbFreeze = GUICtrlCreateCheckbox("&Freeze", 13, 5, 90) GUICtrlSetFont(-1, 9, -1, -1, "Lucida Console") GUICtrlSetResizing(-1, $GUI_DOCKALL) GUICtrlCreateLabel("", 11, 31, 42, 758) ; soll die Ziffern andersfarbig hinterlegen GUICtrlSetBkColor(-1, $COLOR_SILVER) GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM + $GUI_DOCKWIDTH) GUISetState(@SW_SHOW) Global $sText = StringFormat("% 5d", $g_iZaehler) & @CRLF _GUICtrlEdit_AppendText($text, $sText) While 1 Sleep(200) $g_iZaehler += 1 $sText = StringFormat("% 5d", $g_iZaehler) & @CRLF _GUICtrlEdit_AppendText($text, $sText) WEnd Func _Exit() Exit EndFunc Do you have any suggestions.
      Regards, Conrad
    • TheDcoder
      By TheDcoder
      Hello! I think the thread title is obvious enough , Here is my code:
      #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 290, 139, 192, 124) $Edit1 = GUICtrlCreateEdit("Test", 8, 8, 273, 121, $ES_READONLY) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd  
      Thanks in advance, TD
    • Rhazz
      By Rhazz
      Hello. First of all, my full code:
      #include <WinAPI.au3> #include <File.au3> #include <String.au3> Local $aFiles, $IntOrFloat Local $aFileToRead = _WinAPI_GetOpenFileName("Open file to read", "Text Files (*.txt)") Local $aFinalFile = _WinAPI_GetSaveFileName("How do you want to save the file?", "Text Files (*.txt)") Local $sFilenameWithoutExtension = _StringBetween($aFinalFile[2],"",".") Local $sLinesPerLine = InputBox("Lines Per Line", "How many lines per line do you want?","","") Local $sCountLines = _FileCountLines($aFileToRead[2]) If ($sCountLines/$sLinesPerLine) >= 1 Then ; When the division result is a number with decimal, there will be less words in the last file and the script won't create it , therefore I add 1 to the result to prevent a file lack. Am I wrong? For example: if user introduces 3 the operation will be "4/3=1.33" and I need two files, the first "line1,line2,line3" and the second "line4". $IntOrFloat = $sCountLines/$sLinesPerLine Else $IntOrFloat = ($sCountLines/$sLinesPerLine) + 1 EndIf For $i = 1 To $IntOrFloat MsgBox(0,"", "Creating file " & $i) $aFiles = $aFinalFile[1] & "\" & $sFilenameWithoutExtension[0] & "-" & $i & ".txt" _FileCreate($aFiles) FileOpen($aFiles,2) For $a = ( ( ( $i - 1 ) * $sLinesPerLine ) + 1 ) To ( $i * $sLinesPerLine ) FileWrite($aFinalFile[2],FileReadLine($aFileToRead[2], $a) & ",") Next FileClose($aFiles) Next MsgBox(0,"Done","Done") With it I want to read a text file with a lot of lines and I wanna split it in multiple files with multiple lines (of the first file) in each line, separated by commas.
      For example: the user executes the script and selects a text file to open that contains:
      Then, the user selects the path and name of the result files. Suppose "C:\result.txt"
      Finally, the user introduces the number of lines per line (lines of the initial text file to each line of the result text files). Suppose the user enters 2.
      So far my code works fine. But I tried to code a loop for do (continuing the example):
      Start first loop repetition. Create "C:\result.-1.txt", and write on it "Line 1 asdf,Line 2 asdf" End first loop repetition. Start second loop repetition. Create "C:\result.-2.txt", and write on it "Line 3 asdf,Line 4 asdf" End second loop repetition. But my code results (continuing the example) in three files created:
      C:\result.txt that contains "Line 1 asdf,Line 2 asdf,Line 3 asdf,Line 4 asdf," C:\result.-1.txt that contains nothing. C:\result.-2.txt that contains nothing. Where is the error? I can't understand where is it...
    • 31290
      By 31290
      Hi Everyone, 
      I'm facing a small issue while trying to remove spaces at the end of lines contained in a temp file.
      Indeed, I'm creating the list of uninstallable apps on my computer and display them in an edit box.
      Here's the result:

      As you can see, all lines have bunch of spaces at their end.
      I did try the StringStripWS function, the StringTrimLeft one... no one works... 
      Here's the code I use:
      Func f_ListApps() GuiCtrlSetState ($g_AppList, $GUI_DISABLE) RunWait(@ComSpec & ' /c ' & 'wmic /node:' & @Computername &' product get name > %temp%\model.txt' ,"", @SW_HIDE) $file =(@TempDir & "/model.txt") $fileread = FileRead($file) GUICtrlCreateEdit($fileread, 5, 250, 395, 400) FileDelete(@TempDir & "/model.txt") EndFunc Surely a dumbass question but few hairs remains on my head
      Thanks