Jump to content

How to get some parameters about an Edit control?


Recommended Posts

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....

Link to post
Share on other sites

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
Link to post
Share on other sites

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
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....

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mo0C0w
      Hey guys!
      I just started working with the GUI yesterday and it has only brought more fun to the Autoit adventure  
      Basically I have a GUI windows that opens and based on the selection of the combo box the button will do something different. This specific scenario I am talking about the Client Update Verification  combo selection. When selected and the button is pressed my second gui window opens. As of now if I only paste comp1 or comp2 by itself it works correctly, but if I paste both, it has a fit.
      What would the correct way to do this be? Any help or examples, would be greatly appreciated! Thank you in advance
       
      #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Client_List.au3> #Region ### START Koda GUI section ### Form=c:\users\dnoble\pictures\plexiibox v4.kxf Global $Form1_1 = GUICreate("Plexii", 336, 419, 1548, 586) GUISetBkColor(0xFFFFFF) Global $Pic1 = GUICtrlCreatePic("C:\Users\dnoble\Pictures\plexii.jpg", 41, 0, 252, 268, BitOR($GUI_SS_DEFAULT_PIC,$SS_CENTERIMAGE)) Global $Label2 = GUICtrlCreateLabel("Select Test", 125, 280, 95, 24) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") Global $Combo2 = GUICtrlCreateCombo("Select Test", 39, 312, 257, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) GUICtrlSetData(-1, "Client Update Verification|Bids") GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") Global $Button1 = GUICtrlCreateButton("Proceed", 111, 352, 129, 33) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 If GUICtrlRead($Combo2) = "Client Update Verification" Then _Form2() ;clientupdateverification() EndSwitch WEnd Func _Form2() #Region ### START Koda GUI section ### Form=c:\users\dnoble\pictures\plexiiboxclientupdate verification.kxf $Form1_1 = GUICreate("Plexii", 336, 521, 866, 454) GUISetBkColor(0xFFFFFF) $Edit1 = GUICtrlCreateEdit("", 57, 72, 241, 345) GUICtrlSetData(-1, "Edit1") $Button1 = GUICtrlCreateButton("Execute", 95, 440, 145, 41) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 If GUICtrlRead($Edit1) = "comp1" Then comp1() If GUICtrlRead($Edit1) = "comp2" Then comp2() EndSwitch WEnd EndFunc  

    • By mike1950r
      Hi,
      I have a problem with the scrollbar in edit control.
      In my case I use richedit, but it appears also in the "normal" editcontrol.
      The problem ONLY appears, when working with my program (including editcontrol) and another program like notpad on the other screen.
      It appears when using the scrollbar of notepad, then my scroll bar and so on ... (vertical scrollbars only)
      and then suddenly the area behind my scrollbar gets highlighted (black) like selected, above and below,
      if i move the mouse to the left during pressing, the whole area behind the scrollbar gets highlighted in black.
      When my program is loaded alone the problem NEVER happens.
      I have made two screenshots, 1st one when the problem appears, and the second one showing when moving to the left.
      I really have no idea and there is nothing special in my editcontrol.
      I would be really thankful, if someone could help me further to get this issue solved.
      thanks in advance
      cheers mike


       
    • By rudi
      Hello,
      very propably this has been asked before, well, I miss the thread(s) ...
      Is there a command line switch to tell the SciTE setup EXE to install silently ("/S") *AND* to end up with "edit" (instead of "run") as system wide default action for *.au3 files?
       

       
      As it can be done with the config tool:
      https://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/SciTEConfigb1.PNG
       
      Regards, Rudi.
    • 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
    • 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
×
×
  • Create New...