careca

(Solved) Clip text to fit label

8 posts in this topic

#1 ·  Posted (edited)

Hi, consider this code:

 

#Region ;Wrapper
#pragma compile(UPX, false)
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Res_SaveSource=y
;#AutoIt3Wrapper_Icon=
;#AutoIt3Wrapper_Res_Icon_Add=
;#AutoIt3Wrapper_Outfile=
;#AutoIt3Wrapper_Res_Comment=
;#AutoIt3Wrapper_Res_Description=
#AutoIt3Wrapper_Res_Fileversion=1.0
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#EndRegion ;Wrapper
;=============================================================================
#Region ;FileInstall
;FileInstall("file", @ScriptDir & "\file")
#EndRegion ;FileInstall
;=============================================================================
#Region ;Includes
#include <GUIConstants.au3>
#include <TrayConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#EndRegion ;Includes
;=============================================================================
#Region ;Opt
OnAutoItExitRegister("Quit")
Opt("TrayMenuMode", 1)
Opt("TrayIconHide", 0)
Opt("GUICoordMode", 1)
Opt("GUIResizeMode", 1)
Opt("TrayIconDebug", 1)
Opt("TrayAutoPause", 0)
Opt("MouseCoordMode", 2)
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 0)
Opt("GUIEventOptions", 1)
Opt("TrayOnEventMode", 1)
Opt("ExpandEnvStrings", 1)
Opt("WinTitleMatchMode", 2)
Opt("WinDetectHiddenText", 1)
#EndRegion ;Opt
;=============================================================================
#Region ;Vars ;Gui
Local $GUI, $totray, $restore, $exititem, $WinState

$GUI = GUICreate("Test Window", 220, 65, 0, 0)
GUISetState()
TraySetState(1)
TraySetClick(16)
$WinState = 1

GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Minimize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Restore")

Local $Text = GUICtrlCreateLabel('HKEY_CLASSES_ROOT\Directory\Background\shell\07 Sync\command', 10, 10, 200, 45, $SS_LEFT, $WS_EX_CLIENTEDGE)

#EndRegion ;Vars ;Gui
;=============================================================================
Func Minimize()
    WinSetState($GUI, '', @SW_MINIMIZE)
EndFunc   ;==>Minimize
;=============================================================================
Func Restore()
    WinSetState($GUI, '', @SW_RESTORE)
EndFunc   ;==>Restore
;=============================================================================
Func Quit()
    Exit
EndFunc   ;==>Quit
;=============================================================================
Do
    Sleep(100)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
;=============================================================================

A label that is "high" enough to fit 2 lines, but the text is hidden and only clips outside visible area.

What im searching for is a way to "cut" the text to fit the label, i've read a similar post, but in that post the font size varies, so a measurement had to be used, im wondering if the same has to be done here, or i can do it in another way.

Thanks.

PS: sorry for long code, it's just a template.

screenshot - 11012017-2046.png

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites



I know it's kind of different, but Melba's StringSize UDF is able to size controls dynamically depending on the size of your string. It might be able to cut the string and put it onto two lines as well, but I haven't looked heavily into it. Give me some time and I'll look around to see if I can think of anything else.


UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

Yeah, i looked into it, problem is when a string doesn't have spaces like:

"HKEY_CLASSES_ROOT\Directory\Background\shell\07 Sync\command"

UDF gives error 3 i think when it doesn't fit. It separates the string by the spaces.

I simply replaced the text, and it gives error 3 a lot.

This is example 2 with a different text.

; StringSize example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <GUIConstantsEx.au3>
#include "StringSize.au3"

Global $aFont[4] = ["Arial", "Tahoma", "Courier New", "Comic Sans MS"]
Global $aSize[4] = [9, 12, 10, 15]
Global $aWeight[4] = [200, 400, 600, 800]
Global $aAttrib[4] = [0, 2, 4, 0]
Global $aMaxWidth[4] = [250, 300, 400, 500]
Global $aColour[4] = [0xFFFF88, 0xBBFF88, 0xBB88FF, 0xFF88FF]
Global $aButMsg[4] = ["Press for next example", "Click here", "Please push", "And the next one please..."]

$sLabelMsg  = "HKEY_CLASSES_ROOT\Directory\Background\shell\07Sync\command\blahblahblahblahnlahdlfhf"

$hGUI = GUICreate("String Sizing Test", 500, 500)

$hLabel = GUICtrlCreateLabel("", 10, 10, -1, -1, 1)
$hNext = GUICtrlCreateButton("", 210, 460, 80, 30)

GUISetState(@SW_HIDE)

While 1

    $sFont = $aFont[Random(0, 3, 1)]
    $iSize = $aSize[Random(0, 3, 1)]
    $iWeight = $aWeight[Random(0, 3, 1)]
    $iAttrib = $aAttrib[Random(0, 3, 1)]
    $iMaxWidth = Random(200, 850, 1)
    $iColour = $aColour[Random(0, 3, 1)]
    $sButMsg = $aButMsg[Random(0, 3, 1)]

    $aButReturn = _StringSize($sButMsg)
    $aMsgReturn = _StringSize($sLabelMsg, $iSize, $iWeight, $iAttrib, $sFont, $iMaxWidth)
    $iError = @error

    If IsArray($aMsgReturn) = 1 Then

        WinMove("String Sizing Test", "", (@DesktopWidth - ($aMsgReturn[2] + 25)) / 2, (@DesktopHeight - ($aMsgReturn[3] + 85)) / 2, $aMsgReturn[2] + 25, $aMsgReturn[3] + 85 )
        GUISetState(@SW_SHOW, $hGUI)

        ControlMove($hGUI, "", $hLabel, 10, 10, $aMsgReturn[2], $aMsgReturn[3])
            GUICtrlSetData($hLabel, $aMsgReturn[0])
            GUICtrlSetFont($hLabel, $iSize, $iWeight, $iAttrib, $sFont)
            GUICtrlSetBkColor($hLabel, $iColour)
        ControlMove($hGUI, "", $hNext, ($aMsgReturn[2] - $aButReturn[2]) / 2, $aMsgReturn[3] + 20, $aButReturn[2] + 20, 30)
            GUICtrlSetData($hNext, $aButReturn[0])

        While 1
            Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $hNext
                    ExitLoop
            EndSwitch
        WEnd
    Else
        MsgBox(0, "Error", "Code = " & $iError)
    EndIf
WEnd

Exit

; End of example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites

Try this..

; - Replace this

Local $Text = GUICtrlCreateLabel('HKEY_CLASSES_ROOT\Directory\Background\shell\07 Sync\command', 10, 10, 200, 45, $SS_LEFT, $WS_EX_CLIENTEDGE)

; - With

Local $trimamount = 31
Local $Text = 'HKEY_CLASSES_ROOT\Directory\Background\shell\07 Sync\command'
Local $Text_len = StringLen($Text)
Local $Text_P1 = StringTrimRight($Text, $Text_len - $trimamount)
Local $Text_P2 = StringTrimLeft($Text, $trimamount)
Local $Output = $Text_P1 & @CRLF & $Text_P2

GUICtrlCreateLabel($Output, 10, 10, 200, 45, $SS_LEFT, $WS_EX_CLIENTEDGE)

 

1 person likes this

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Share this post


Link to post
Share on other sites

Yeah, that works, thanks.

I just have to change it a bit for 3 splits/strings. :)


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

How about this..

Local $trimamount = 31
Local $Text = 'HKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\B'
Local $Output, $Text_

Local $Text_len = StringLen($Text)
Local $Parts = Round($Text_len / $trimamount, 0)
For $x = 1 To $Parts Step 1
    $Text_ = StringLeft($Text, $trimamount)
    $Text = StringTrimLeft($Text, $trimamount)
    $Output = $Output & $Text_ & @CRLF
Next
Local $LableSize = $Parts * 15

GUICtrlCreateLabel($Output, 10, 10, 200, $LableSize, $SS_LEFT, $WS_EX_CLIENTEDGE)

 

Edited by zone97

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I had edited my post, must've forgot to save or smt, meanwhile i came up with this.

Local $Retrieve = ClipGet()
    Local $Text_len = StringLen($Retrieve)
    Local $Text_P1 = StringLeft($Retrieve, 30)
    Local $Text_P2 = StringMid($Retrieve, 31, 30)
    Local $Text_P3 = StringRight($Retrieve, $Text_len - 60)
    Local $Output = $Text_P1 & @CRLF & $Text_P2 & @CRLF & $Text_P3

Left mid right, simple right?

Thanks for everything.

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites

Here's another one, I did this a while back for someone who was wanting the same thing you're trying to do (wrap text in a label)

#include <GUIConstants.au3>
#include <WinAPI.au3>
#include <Array.au3>

Global $hMain = GUICreate("Multiline", 420, 175)
Global $lblDisplay = GUICtrlCreateLabel("", 10, 10, 380, 130)
Global $btnUpateLabel = GUICtrlCreateButton("Update Label", 10, 145, 380, 20)
Global $btnUp = GUICtrlCreateButton("↑", 390, 10, 25, 25)
Global $btnDown = GUICtrlCreateButton("↓", 390, 115, 25, 25)
Global $lblTopLine = GUICtrlCreateLabel(0 & @CRLF & 0, 395, 60, 30, 30)
Global $aLines = Null
Global $iTopLine = 0

GUICtrlSetFont($btnDown, 14)
GUICtrlSetFont($btnUp, 14)

GUISetState(@SW_SHOW)

While (True)
    Local $sString = ""

    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            GUIDelete($hMain)
            Exit 0
        Case $btnUpateLabel
            #cs
            Local $iRandomStop = Random(9999, 99999, 1)
            For $i = 0 To $iRandomStop
                $sString &= Random(0, 9, 1)
            Next
            #ce
            $sString = 'HKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\BHKEY_CLASSES_ROOT\Directory\B'
            $aLines = LongStringToArray($lblDisplay, $sString, $hMain)
            $iTopLine = 0
            ConsoleWrite("Total string length: " & StringLen($sString) & @CRLF & "Rows created from string: " & UBound($aLines) & @CRLF & "Current string: " & $sString & @CRLF)
            GUICtrlSetDataFromArray($lblDisplay, $aLines, $iTopLine, -1)
            GUICtrlSetData($lblTopLine, $iTopLine + 1 & @CRLF & UBound($aLines))

        Case $btnDown
            If ($iTopLine + 1 < UBound($aLines)) Then
                $iTopLine += 1
                GUICtrlSetDataFromArray($lblDisplay, $aLines, $iTopLine, -1)
                GUICtrlSetData($lblTopLine, $iTopLine + 1 & @CRLF & UBound($aLines))
            EndIf
        Case $btnUp
            If ($iTopLine - 1 >= 0) Then
                $iTopLine -= 1
                GUICtrlSetDataFromArray($lblDisplay, $aLines, $iTopLine, -1)
                GUICtrlSetData($lblTopLine, $iTopLine + 1 & @CRLF & UBound($aLines))
            EndIf
    EndSwitch
WEnd

Func GUICtrlSetDataFromArray(Const $iCtrlid, Const $aArray, $iRowStart = -1, $iRowEnd = -1)
    If (Not IsArray($aArray)) Then Return SetError(1, 0, ConsoleWrite("Array not created yet. Press the Update Label button." & @CRLF))
    If ($iRowStart < 0 Or StringLen($iRowStart) = 0 Or $iRowStart >= UBound($aArray)) Then $iRowStart = 0
    If ($iRowEnd < 0 Or StringLen($iRowEnd) = 0 Or $iRowEnd >= UBound($aArray)) Then $iRowEnd = UBound($aArray) - 1
    Local $sDataString = ""

    For $i = $iRowStart To $iRowEnd
        $sDataString &= $aArray[$i] & @CRLF
    Next
    Return GUICtrlSetData($iCtrlid, StringTrimRight($sDataString, 2))
EndFunc   ;==>GUICtrlSetDataFromArray

Func LongStringToArray(Const $iCtrlid, Const $vValue, Const $hWindow)
    If ($vValue = "") Then Return SetError(1, 0, $vValue)
    Local $hWnd = (IsHWnd($iCtrlid) ? $iCtrlid : GUICtrlGetHandle($iCtrlid))
    Local $sFinalString = ""
    Local $aSize = ControlGetPos($hWindow, "", $iCtrlid)
    Local $iStringLen = StringLen($vValue)
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $hFont = _SendMessage($hWnd, $WM_GETFONT)
    Local $hSelectObject = _WinAPI_SelectObject($hDC, $hFont)

    For $i = 1 To $iStringLen
        Local $sLine = ""
        Local $bMeasured = False
        Do
            $sLine &= StringMid($vValue, $i, 1)

            Local $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sLine)

            If (DllStructGetData($tSize, 1) > $aSize[2]) Then
                $sLine = StringTrimRight($sLine, 1)
                $bMeasured = True
                $i -= 1
            Else
                $i += 1
            EndIf
            $tSize = Null

        Until ($bMeasured Or $i > $iStringLen)

        $sFinalString &= $sLine & @LF
    Next

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

    Return StringSplit(StringTrimRight($sFinalString, 2), @LF, $STR_NOCOUNT)
EndFunc   ;==>LongStringToArray

 

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