Jump to content

Fitting string length to label width


Go to solution Solved by Nine,

Recommended Posts

As the title says. I am trying to fit text of random lengths into a label control. I know there are snippets out there but most size the label to the text. I've been playing around for hours and have a working script but I want it better.

Currently the text is trimmed but there is still space in the label for more text. I think the issue is where the max number of characters is calculated. "x" is probably the character that takes up the most space and the calc uses that size based on font properties. Obviously the label could hold more characters if they were lower case or symbols. I would like to try and improve the max size calculation, and make it as bulletproof as possible. 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>
#include <FontConstants.au3>
#include <WinAPIShPath.au3>

Opt("GUIOnEventMode", 1)

Local $sLongString = _
        'One after one, by the star-dogged Moon, ' & _
        'Too quick for groan or sigh, ' & _
        'Each turned his face with a ghastly pang, ' & _
        'And cursed me with his eye. ' & _
        'Four times fifty living men, ' & _
        '(And I heard nor sigh nor groan) ' & _
        'With heavy thump, a lifeless lump, ' & _
        'They dropped down one by one.'

Local $sLongString1 = _
        'https://sso.microsoft.com/adfs/ls/idpinitiatedsignon.aspx?' & _
        'LoginToRP=https://www.microsoft.com/en-gb/microsoft-365/' & _
        'small-business-resource-center&client-request-id=5648b446' & _
        '-4e0b-4116-7e37-0080000000e5&pullStatus=0||4'

Local $sLongString2 = _ ; fits perfect
        'STARTOFSTRINGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXENDOFSTRING'

Local $h_GUI = GUICreate("Compact String Example", 350, 50)
Local $idString_lbl = GUICtrlCreateLabel('', 10, 10, 327, 21, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1, 8, 400, 0, "MS Sans Serif")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState()

GUICtrlSetData($idString_lbl, _StringFitToLabel($idString_lbl, $sLongString))

While 1
    Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _GetControlFontSettings($idLabel)
    Local $aiFont[3] = [0, 0, 0]

    Local $hFont = GUICtrlSendMsg($idLabel, $WM_GETFONT, 0, 0) ; handle for control retrieved
    If Not $hFont Then Return SetError(1, @error, $aiFont)

    Local $tFont = DllStructCreate("int;int;int;int;int;byte;byte;byte;byte;byte;byte;byte;byte;char[32]")
    If @error Then Return SetError(2, @error, $aiFont)

    If Not _WinAPI_GetObject($hFont, DllStructGetSize($tFont), DllStructGetPtr($tFont)) Then Return SetError(3, @error, $aiFont)

    $aiFont[0] = DllStructGetData($tFont, 1) ; lfHeight
    $aiFont[1] = DllStructGetData($tFont, 5) ; lfWeight
    $aiFont[2] = DllStructGetData($tFont, 14) ; lfFaceName

    Return $aiFont
EndFunc   ;==>_GetControlFontSettings

; Function to calculate the maximum number of characters that can fit within a label's width
Func _GetLabelMaxChars($idLabel)
    Local $iMaxChars = 10
    Local $aiFont = _GetControlFontSettings($idLabel)
    If $aiFont[1] = 0 Then Return SetError(1, 0, $iMaxChars) ; set a default of 10 characters if error occurs

    Local $hDC = _WinAPI_GetDC(GUICtrlGetHandle($idLabel))
    If Not $hDC Then Return SetError(2, 0, $iMaxChars) ; set a default of 10 characters if error occurs

    Local $hFont = _WinAPI_CreateFont($aiFont[0], 0, 0, 0, $aiFont[1], False, False, False, $DEFAULT_CHARSET, _
            $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $aiFont[2])
    If Not $hFont Then Return SetError(3, 0, $iMaxChars) ; set a default of 10 characters if error occurs

    _WinAPI_SelectObject($hDC, $hFont)

    Local $tSize = _WinAPI_GetTextExtentPoint32($hDC, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
    If @error Then Return SetError(4, 0, $iMaxChars)

    Local $aPos = ControlGetPos(WinGetHandle("[ACTIVE]"), '', $idLabel)
    If Not IsArray($aPos) Then Return SetError(5, 0, $iMaxChars)

    $iMaxChars = Floor($aPos[2] / (DllStructGetData($tSize, "X") / 52)) ; this is what may need to be improved

    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteObject($hFont)

    Return $iMaxChars
EndFunc   ;==>_GetLabelMaxChars

Func _StringFitToLabel($idLabel, $sString)
    Local $iMaxChars = _GetLabelMaxChars($idLabel)

    If StringLen($sString) > $iMaxChars Then
        ; Calculate the number of characters to keep from each end
        Local $iCharsToKeep = Floor(($iMaxChars - 3) / 2) ; Keep room for ellipsis (...)

        ; trim the string to the character length
        $sString = StringLeft($sString, $iCharsToKeep) & '...' & StringRight($sString, $iCharsToKeep)
    EndIf

    ConsoleWrite('$iMaxChars: ' & $iMaxChars & @CRLF)
    ConsoleWrite('$sString  : ' & StringLen($sString) & @CRLF)

    Return $sString
EndFunc   ;==>_StringFitToLabel

 

Link to comment
Share on other sites

I never had the need to use it myself but when this subject arises a lot of users suggest to use this great UDF written by Melba23.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Yeah, thanks. I looked at that and _StringCompact() plus a few other posts. 99% want to expand the label to fit the text, or change the  font size to fit the label. I want to do the opposite so thought I would have a go writing one. 8 hrs later..... :mad2:

Link to comment
Share on other sites

an easy solution to calculate the length would be if you use monospapse font, such as "DejaVu Sans Mono"

#include <GUIConstantsEx.au3>

Local $aCtrl[6]
$aCtrl[0] = 5
Local $hGUI = GUICreate(@AutoItVersion, 400, 200)

GUISetFont(9,  400, 0, "DejaVu Sans Mono") ; will display underlined characters

$aCtrl[1] = GUICtrlCreateLabel("01234567890123456789012345", 10, 10)
$aCtrl[2] = GUICtrlCreateLabel("abcdefghijklmnopqrstuvwxyz", 10, 30)
$aCtrl[3] = GUICtrlCreateLabel(StringUpper("abcdefghijklmnopqrstuvwxyz"), 10, 50)
$aCtrl[4] = GUICtrlCreateLabel(StringUpper("xxxxxxxxxxxxxxxxxxxxxxxxxx"), 10, 70)
$aCtrl[5] = GUICtrlCreateLabel("||||||||||||||||||||||||||", 10, 90)
GUISetState(@SW_SHOW)

For $i = 1 To $aCtrl[0]
    $iLen = StringLen(GUICtrlRead($aCtrl[$i]))
    ConsoleWrite("$iLen=" & $iLen )
    $iWidth = ControlGetPos($hGUI, '', $aCtrl[$i])[2]
    ConsoleWrite(" , $iWidth=" & $iWidth)
    ConsoleWrite(" , $iWidth/$iLen=" & $iWidth / $iLen & @CRLF)
Next

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

I know that I know nothing

Link to comment
Share on other sites

  • Moderators

benners,

This post shows how to determine the largest font size that will fit into a specified label width. Hope it helps.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks, I am unsure how that helps, so I must be missing something :D? In your example, the labels auto size and use a fixed font. In normal use, the label size will be fixed and the font may be different every time depending on the label. If I set a fixed width and get the resulting $iWidth / $iLen, how do you envision that being used in the calculation to trim the string?

I have played around with the _GetLabelMaxChars function. Instead of using a default string and that length, it now uses the actual string and its length. This seems to fill more of the label but still on occasion, misses a few characters off the end.

; Function to calculate the maximum number of characters that can fit within a label's width
Func _GetLabelMaxChars($idLabel, $sString)
    Local $iMaxChars = 10
    Local $aiFont = _GetControlFontSettings($idLabel)
    If $aiFont[1] = 0 Then Return SetError(1, 0, $iMaxChars) ; set a default of 10 characters if error occurs

    Local $hDC = _WinAPI_GetDC(GUICtrlGetHandle($idLabel))
    If Not $hDC Then Return SetError(2, 0, $iMaxChars) ; set a default of 10 characters if error occurs

    Local $hFont = _WinAPI_CreateFont($aiFont[0], 0, 0, 0, $aiFont[1], False, False, False, $DEFAULT_CHARSET, _
            $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $aiFont[2])
    If Not $hFont Then Return SetError(3, 0, $iMaxChars) ; set a default of 10 characters if error occurs

    _WinAPI_SelectObject($hDC, $hFont)

    Local $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sString)
    If @error Then Return SetError(4, 0, $iMaxChars)

    Local $aPos = ControlGetPos(WinGetHandle("[ACTIVE]"), '', $idLabel)
    If Not IsArray($aPos) Then Return SetError(5, 0, $iMaxChars)

    $iMaxChars = Floor($aPos[2] / (DllStructGetData($tSize, "X") / StringLen($sString))) ; this is what may need to be improved

    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteObject($hFont)

    ConsoleWrite('DllStructGetData: ' & DllStructGetData($tSize, "X") & @CRLF)
    Return $iMaxChars
EndFunc   ;==>_GetLabelMaxChars

 

Link to comment
Share on other sites

  • Moderators

benners,

Quote

so I must be missing something

Indeed so.  The _FindMaxSize function uses the label size passed to it and determines the maximum font size that will fit into it - both horizontally and vertically. That is why the font size decreases as the text to fit gets longer while the label stays the same size.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorry Melba, That reply was in response to ioa747 :D. With your function I don't need to know the maximum font size the label will hold, I need to trim the text, like _WinAPI_PathCompactPath does for filepaths. If the string is of an absurdly long length and I use this long string with your function it errors out. The strings I pass to the label will never be this long, I use it as a test string for my functions.

Local $sLongString3 = _
        '0x000001000500404000000100200028420000560000003030000001002000A82500007E4200002020000001002000A810000026680000181800000100200088090000CE780000101000000100200068040000568200002800000040000000800000000100200000000000004200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569D8890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF890569FF880469D00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572AB91FF6FC497FF57AB7FFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF52A67AFF4CA074FF35895DFF466F5FFF880468DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7B'
$sLongString3 &= 'FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51'
$sLongString3 &= 'AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF53A57AFF6C5A72FF6E5271FF6E5271FF6E5271FF6E5271FF6E5271FF637575FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF579778FF686773FF6E5571FF696373FF599278FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF53A57AFF627775FF6C5972FF6D5771FF657074FF54A079FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF617A75FF890569FF890569FF890569FF890569FF890569FF890569FF82176BFF51AC7BFF51AC7BFF52A77AFF714B70FF880769FF890569FF890569FF890569FF880569FF73436FFF53A57AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF666C74FF84126AFF890569FF890569FF890569FF890569FF860B69FF6C5A72FF51AA7AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF773A6EFF627875'
$sLongString3 &= 'FF627875FF627875FF627875FF599278FF51AC7BFF51AA7AFF78376EFF890569FF85106AFF6F5271FF666D74FF6D5671FF83146AFF890569FF7A316DFF51A97AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF6C5972FF890569FF880669FF763C6EFF686773FF666A73FF73456FFF860B69FF890569FF5E8576FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF676A73FF890569FF801D6BFF579979FF51AC7BFF51AC7BFF51AC7BFF559F79FF7E246CFF890569FF6A6172FF51AC7BFF51AC7BFF51AC7BFF5E8576FF880569FF860C69FF5F8076FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF74436FFF890569FF5F7F76FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF7F226CFF890569FF5D8576FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF5A9078FF880669FF82186BFF51AB7AFF51AC7BFF51AC7BFF77386EFF890569FF676973FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF74436FFF890569FF5F7F76FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51'
$sLongString3 &= 'AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF54A179FF880569FF811A6BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF7D276CFF890569FF589578FF51AC7BFF51AB7BFF850E6AFF870A69FF52A77AFF51AC7BFF51AC7BFF6D5872FF7A316DFF7A316DFF83156AFF890569FF5F7F76FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF589678FF890569FF7B2B6DFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF77386EFF890569FF5C8977FF51AC7BFF51A97AFF890569FF82196BFF51AC7BFF51AC7BFF51AC7BFF801F6BFF890569FF890569FF890569FF880769FF5A8F77FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF53A47AFF880669FF83166AFF51AB7AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF7F226CFF890569FF579978FF51AC7BFF51AC7BFF84126AFF870869FF53A47AFF51AC7BFF51AC7BFF53A47AFF589478FF589478FF589478FF569C79FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99'
$sLongString3 &= 'FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF7C296CFF890569FF617C75FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF5D8777FF880569FF801E6BFF51AB7BFF51AC7BFF51AC7BFF74406FFF890569FF6B5D72FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF637575FF890569FF83156AFF5A8F77FF51AC7BFF51AC7BFF51AC7BFF589678FF811C6BFF890569FF666C74FF51AC7BFF51AC7BFF51AC7BFF5A8F77FF870869FF870869FF656F74FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF5D8676FF6D5771FF52A67AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF627875FF890569FF6F5271FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AB7BFF724770FF890569FF870969FF75406FFF6C5A72FF73446FFF860C69FF890569FF753F6FFF51AB7AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF666B74FF880669FF890569FF7C296CFF6E5471FF6D5771FF79336DFF880569FF890569FF5C8A77FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
$sLongString3 &= '00000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF5B8B77FF880769FF676773FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AA7AFF6B5D72FF860D69FF890569FF890569FF890569FF870A69FF6E5571FF51A97AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF607E76FF801F6BFF890569FF890569FF890569FF890569FF83146AFF666B74FF51AB7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF53A47AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF53A37AFF617975FF676873FF637675FF559F79FF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AB7AFF5C8A77FF666C74FF676973FF5F8276FF52A87AFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD00000000000000000000000000000000000000000000000000000000000000'
$sLongString3 &= '00000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E572B093FF6FCA99FF55B07FFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF51AC7BFF4BA575FF338E5DFF43735FFF890569DD0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E5788187FF76938BFF638179FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF607E76FF5C7971FF4A6860FF565461FF880469DD00000000000000000000000000'
$sLongString3 &= '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000880569E3A33D8AFFA7458FFFA3428DFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA2418CFFA13F8BFF9B3985FF983080FF880469DA0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFE7E9EEFFCFD1D6FFC3AEC3FF880469D80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFE7E9EEFFCFD1D6FFC3AEC3'
$sLongString3 &= 'FF890569D80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFE7E9EEFFCFD1D6FFC3AEC3FF890569D80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFE7E9EEFFCFD1D6FFC3AEC3FF890569D80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFED'
$sLongString3 &= 'EFF4FFEDEFF4FFE7E9EEFFCFD1D6FFC3AEC3FF890569D80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFE9E5EEFFAC589AFFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFAA5397FFBB7AAEFFEDEFF4FFEDEFF4FFE7E9EEFFCFD1D6FFC3AEC3FF890569D80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFECEDF3FFCCA3C7FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFCA9DC3FFD5B8D3FFEDEFF4FFEDEFF4FFE7E9EEFFCFD1D6FFC3AEC3FF890569D80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000890569E2F0DFECFFFFFFFFFFEFF1F5FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4FFEDEFF4'
$sLongString3 &= 'FF'

 

Quote

Subscript used on non-accessible variable.:
If $aRet[3] < $iHeight Then
If $aRet^ ERROR

 

Link to comment
Share on other sites

  • Moderators

benners,

In that case you will need to set the smallest font size you want to use and then if you still cannot fit the text into the label, start trimming the text untill it does fit. I will have a think about how my UDF can do this.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Cheers. The code I posted works close enough with most strings.  For some reason, when strings are mostly upper and lower case, that's when the text overruns the label by a few characters.

It's only for a splash screen for now, but if I think I will use the code again, , I normally add it to my udf so want it bulletproof.

Link to comment
Share on other sites

  • Solution

Here one solution, maybe ?

#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>
#include <FontConstants.au3>
#include <WinAPIShPath.au3>

Opt("GUIOnEventMode", 1)

Local $sLongString = _
        'One after one, by the star-dogged Moon, ' & _
        'Too quick for groan or sigh, ' & _
        'Each turned his face with a ghastly pang, ' & _
        'And cursed me with his eye. ' & _
        'Four times fifty living men, ' & _
        '(And I heard nor sigh nor groan) ' & _
        'With heavy thump, a lifeless lump, ' & _
        'They dropped down one by one.'

Local $sLongString1 = _
        'https://sso.microsoft.com/adfs/ls/idpinitiatedsignon.aspx?' & _
        'LoginToRP=https://www.microsoft.com/en-gb/microsoft-365/' & _
        'small-business-resource-center&client-request-id=5648b446' & _
        '-4e0b-4116-7e37-0080000000e5&pullStatus=0||4'

Local $sLongString2 = _ ; fits perfect
        'STARTOFSTRINGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXENDOFSTRING'

Local $h_GUI = GUICreate("Compact String Example", 350, 150)
Local $idString_lbl = GUICtrlCreateLabel('', 10, 10, 327, 21, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1, 8, 400, 0, "MS Sans Serif")

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState()

GUICtrlSetData($idString_lbl, _GetLabelMaxString($idString_lbl, $sLongString))

While 1
    Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _GetControlFontSettings($idLabel)
    Local $aiFont[3] = [0, 0, 0]

    Local $hFont = GUICtrlSendMsg($idLabel, $WM_GETFONT, 0, 0) ; handle for control retrieved
    If Not $hFont Then Return SetError(1, @error, $aiFont)

    Local $tFont = DllStructCreate("int;int;int;int;int;byte;byte;byte;byte;byte;byte;byte;byte;char[32]")
    If @error Then Return SetError(2, @error, $aiFont)

    If Not _WinAPI_GetObject($hFont, DllStructGetSize($tFont), DllStructGetPtr($tFont)) Then Return SetError(3, @error, $aiFont)

    $aiFont[0] = DllStructGetData($tFont, 1) ; lfHeight
    $aiFont[1] = DllStructGetData($tFont, 5) ; lfWeight
    $aiFont[2] = DllStructGetData($tFont, 14) ; lfFaceName

    Return $aiFont
EndFunc   ;==>_GetControlFontSettings

Func _GetLabelMaxString($idLabel, $sString)
    Local $aiFont = _GetControlFontSettings($idLabel)
    Local $hDC = _WinAPI_GetDC(GUICtrlGetHandle($idLabel))
    Local $hFont = _WinAPI_CreateFont($aiFont[0], 0, 0, 0, $aiFont[1], False, False, False, $DEFAULT_CHARSET, _
            $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $aiFont[2])
    _WinAPI_SelectObject($hDC, $hFont)

    Local $aPos = ControlGetPos(WinGetHandle("[ACTIVE]"), '', $idLabel), $tSize
    For $i = 1 To StringLen($sString)
      $tSize = _WinAPI_GetTextExtentPoint32($hDC, StringLeft($sString, $i) & "..." & StringRight($sString, $i))
      If $tSize.X > $aPos[2] Then ExitLoop
    Next

    If $i < stringlen($sString) then $sString = StringLeft($sString, $i-2) & "..." & StringRight($sString, $i-2)

    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteObject($hFont)

    Return $sString
EndFunc   ;==>_GetLabelMaxChars

 

Link to comment
Share on other sites

Posted (edited)

That works a lot more consistently than mine. Nice :D. There's still a minor issue with certain strings and font properties but I'll take it (see code below). Thanks.

I combined the two remaining functions into 1 as well.

#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>
#include <FontConstants.au3>

Opt("GUIOnEventMode", 1)

Local $sLongString = _
        'One after one, by the star-dogged Moon, ' & _
        'Too quick for groan or sigh, ' & _
        'Each turned his face with a ghastly pang, ' & _
        'And cursed me with his eye. ' & _
        'Four times fifty living men, ' & _
        '(And I heard nor sigh nor groan) ' & _
        'With heavy thump, a lifeless lump, ' & _
        'They dropped down one by one.'

Local $sLongString1 = _
        'https://sso.microsoft.com/adfs/ls/idpinitiatedsignon.aspx?' & _
        'LoginToRP=https://www.microsoft.com/en-gb/microsoft-365/' & _
        'small-business-resource-center&client-request-id=5648b446' & _
        '-4e0b-4116-7e37-0080000000e5&pullStatus=0||4'

Local $sLongString2 = _ ; fits perfect
        'STARTOFSTRINGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' & _
        'XXXXXXXXXXXXXXXXXXXXXXENDOFSTRING'

Local $h_GUI = GUICreate("Compact String Example", 350, 150)
Local $idString_lbl = GUICtrlCreateLabel('', 10, 10, 327, 21, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState()

GUICtrlSetData($idString_lbl, _StringFitToLabel($idString_lbl, $sLongString2))

While 1
    Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _StringFitToLabel($idLabel, $sString, $hGui)
    Local Enum _
            $FONTHIEGHT, _
            $FONTWEIGHT, _
            $FONTFACENAME

    Local $aiFont[3] = _
            [1, _ ;_    lfHeight
            5, _ ;_     lfWeight
            14] ;_      lfFaceName

    Local $hFont = GUICtrlSendMsg($idLabel, $WM_GETFONT, 0, 0) ; handle for control retrieved
    If Not $hFont Then Return SetError(1, @error, $sString)

    Local $tFont = DllStructCreate("int;int;int;int;int;byte;byte;byte;byte;byte;byte;byte;byte;char[32]")
    If @error Then Return SetError(2, @error, $sString)

    If Not _WinAPI_GetObject($hFont, DllStructGetSize($tFont), DllStructGetPtr($tFont)) Then Return SetError(3, 0, $sString)

    ; load the array
    For $i = 0 To UBound($aiFont) - 1
        $aiFont[$i] = DllStructGetData($tFont, $aiFont[$i])
    Next

    Local $hDC = _WinAPI_GetDC(GUICtrlGetHandle($idLabel))
    If Not $hDC Then Return SetError(4, 0, $sString)

    Local $hFont = _WinAPI_CreateFont($aiFont[$FONTHIEGHT], 0, 0, 0, $aiFont[$FONTWEIGHT], False, False, False, $DEFAULT_CHARSET, _
            $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $aiFont[$FONTFACENAME])
    If Not $hFont Then Return SetError(5, 0, $sString)

    If _WinAPI_SelectObject($hDC, $hFont) <= 0 Then Return SetError(6, 0, $sString)

    Local $aPos = ControlGetPos(hGui, '', $idLabel), $tSize

    For $i = 1 To StringLen($sString)
        $tSize = _WinAPI_GetTextExtentPoint32($hDC, StringLeft($sString, $i) & "..." & StringRight($sString, $i))
        If $tSize.X > $aPos[2] Then ExitLoop
    Next

    If $i < StringLen($sString) Then $sString = StringLeft($sString, $i - 2) & "..." & StringRight($sString, $i - 2)

    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteObject($hFont)

    Return $sString
EndFunc   ;==>_StringFitToLabel

 

Edited by benners
WinGetHandle("[ACTIVE]") came back to bite me
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...