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

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
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 comment
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 comment
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!

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
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
 Share

×
×
  • Create New...