Jump to content

GUICtrlCreateLabel or GUICtrlSetData: Word wrap even with no white spaces?


Recommended Posts

Hi

I want to write a filesystem path as simple text in a GUI. When I use GUICtrlCreateLabel or GUICtrlSetData respectively, a very long path will only be wrapped around if it contains a white space. If the path contains no white space, the path will be truncated at the end of the Control (which ends near the boarder of the GUI).

I would like, that somehow the path would be splitted on an appropriate backslash within this path, and wrapped around to the next line.

But I have no idea if this is possible with AutoIt or how to do that? :huh:

Cheers, Roman :ILA:

Link to comment
Share on other sites

roman,

The GuiCtrlCreateLabel can have width and height parameters (kind of like invisible borders) . So if you set it with a width less than the main window's width, it will automatically wrap fit. Also you have to set appropriate height so that when the text wrap falls into new lines, it shows. If you want to manually make a new line from anywhere in the text, use the @LF line feed macro. See this example:

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

GUICreate("Window size: 300x300", 300, 300)
GUISetState()

GUICtrlCreateLabel("Label with width more than window: 500", 20, 20, 250)
GUICtrlSetFont(-1, 8.5, 600)
GUICtrlCreateLabel("", 20, 40, 500, 400)
GUICtrlSetData(-1, "C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012")

GUICtrlCreateLabel("Label with small width less than window: 260", 20, 100, 260)
GUICtrlSetFont(-1, 8.5, 600)
GUICtrlCreateLabel("", 20, 120, 260, 500)
GUICtrlSetData(-1, "C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012")

GUICtrlCreateLabel('Label with manual line feed "@LF" ', 20, 180, 250)
GUICtrlSetFont(-1, 8.5, 600)
GUICtrlCreateLabel("", 20, 200, 500, 500)
GUICtrlSetData(-1, "C:Documents and Settingsusername" & @LF & "Application DataNokiaPCSync" & @LF & "SynchDataBackups1232012")


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

You can also create different styles of actual frames for labels and also align and justify text with labels. See http://www.autoitscript.com/autoit3/docs/appendix/GUIStyles.htm#Label for more info

Edited by abaddon7734
Link to comment
Share on other sites

Hi abaddon7734

Thanks, but now try this:

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
GUICreate("Window size: 300x300", 300, 300)
GUISetState()
GUICtrlCreateLabel("Label with width more than window: 500", 20, 20, 250)
GUICtrlSetFont(-1, 8.5, 600)
GUICtrlCreateLabel("", 20, 40, 500, 400)
GUICtrlSetData(-1, "C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012")
GUICtrlCreateLabel("Label with small width less than window: 260", 20, 100, 260)
GUICtrlSetFont(-1, 8.5, 600)
GUICtrlCreateLabel("", 20, 120, 260, 500)
GUICtrlSetData(-1, "C:Documents_and_SettingsusernameApplication_DataNokiaPCSyncSynchDataBackups1232012")
GUICtrlCreateLabel('Label with manual line feed "@LF" ', 20, 180, 250)
GUICtrlSetFont(-1, 8.5, 600)
GUICtrlCreateLabel("", 20, 200, 500, 500)
GUICtrlSetData(-1, "C:Documents and Settingsusername" & @LF & "Application DataNokiaPCSync" & @LF & "SynchDataBackups1232012")

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

No wrap fit in the second example. Manually inserting @LF is no option.

Cheers.

Edited by roman
Link to comment
Share on other sites

Why not get the dimensions of the rectangle that would be able to bound the text

Try this code:

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPLus.au3>

Local $sLongest_Text="C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012"
Local $sPos=CalcRectangle($sLongest_Text)
GUICreate("Window size: 300x300", $sPos[0], 500)
GUISetState()
GUICtrlCreateLabel("Label with width more than window: 500", 20, 20, 250)
GUICtrlSetFont(-1, 8.5, 600)
$sPos=CalcRectangle("C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012")
GUICtrlCreateLabel("", 20, 40, $sPos[0], $sPos[1])
GUICtrlSetBkColor(-1,0xFF23FF)
GUICtrlSetData(-1, "C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012")
GUICtrlCreateLabel("Label with small width less than window: 260", 20, 100, 260)
GUICtrlSetFont(-1, 8.5, 600)
$sPos=CalcRectangle('C:Documents_and_SettingsusernameApplication_DataNokiaPCSyncSynchDataBackups1232012')
GUICtrlCreateLabel("", 20, 120, $sPos[0], $sPos[1])
GUICtrlSetBkColor(-1,0xFF23FF)
GUICtrlSetData(-1, "C:Documents_and_SettingsusernameApplication_DataNokiaPCSyncSynchDataBackups1232012")
GUICtrlCreateLabel('Label with manual line feed "@LF" ', 20, 180, 250)
GUICtrlSetFont(-1, 8.5, 600)
$sPos=CalcRectangle("C:Documents and Settingsusername" & @LF & "Application DataNokiaPCSync" & @LF & "SynchDataBackups1232012")
GUICtrlCreateLabel("", 20, 200, $sPos[0], $sPos[1])
GUICtrlSetData(-1, "C:Documents and Settingsusername" & @LF & "Application DataNokiaPCSync" & @LF & "SynchDataBackups1232012")
GUICtrlSetBkColor(-1,0xFF23FF)

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

Func CalcRectangle($sString, $sFont='Arial',$fFontsize=8.5)
_GDIPlus_Startup()
Local $aResult, $hFont, $i=0 ,$iLen = StringLen($sString),$tLayout
Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle()))
Local $tRectF = DllStructCreate($tagGDIPRECTF)
Local $hFormat = _GDIPlus_StringFormatCreate()
Local $hFont = _GDIPlus_FontCreate( _GDIPlus_FontFamilyCreate($sFont), $fFontsize, 0)
Do
$tLayout = _GDIPlus_RectFCreate(0, 0, $i, 0)
$aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", _
"handle", $hGfx, _
"wstr", $sString, _
"int", $iLen, _
"handle", $hFont, _
"struct*", $tLayout, _
"handle", $hFormat, _
"struct*", $tRectF, _
"int*", 0, _
"int*", 0)
If $aResult[8] >= $iLen Then ExitLoop
$i += 1
Until False
_GDIPlus_FontDispose($hFont)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_GraphicsDispose($hGfx)
$tLayout = 0
Local $sReturn[2]=[DllStructGetData($aResult[7],'Width'),DllStructGetData($aResult[7],'Height')]
_GDIPlus_Shutdown()
Return $sReturn
EndFunc

The rectangle having the exact text in it(nor more nor less space) has been colored pink

Hope this helps :)

Regards

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

As U wish friend

then u have only two choices left

First: Type the default text of the label upon creation then the label will align the text in accordance

Second(recommended) : Use the following function and get the font size which would be able to fit in the specified width and height

The following script adjusts the size of the font so that the text fits in the specified width and height

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPLus.au3>

Local $sLongest_Text="C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012"
Local $sPos
GUICreate("Window size: 300x300", 500, 500)
GUISetState()
GUICtrlCreateLabel("Label with width more than window: 500", 20, 20, 250)
GUICtrlSetFont(-1, 8.5, 600)
$sPos=CalcFontSize("C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012",500,100)
GUICtrlCreateLabel("", 20, 40, 500, 100)
GUICtrlSetFont(-1,$sPos)
;GUICtrlSetBkColor(-1,0xFF23FF)
GUICtrlSetData(-1, "C:Documents and SettingsusernameApplication DataNokiaPCSyncSynchDataBackups1232012")
GUICtrlCreateLabel("Label with small width less than window: 260", 20, 100, 260)
GUICtrlSetFont(-1, 8.5, 600)
$sPos=CalcFontSize("C:Documents_and_SettingsusernameApplication_DataNokiaPCSyncSynchDataBackups1232012",260,100)
GUICtrlCreateLabel("", 20, 120, 260,100)
GUICtrlSetFont(-1,$sPos)
;GUICtrlSetBkColor(-1,0xFF23FF)
GUICtrlSetData(-1, "C:Documents_and_SettingsusernameApplication_DataNokiaPCSyncSynchDataBackups1232012")
GUICtrlCreateLabel('Label with manual line feed "@LF" ', 20, 180, 250)
GUICtrlSetFont(-1, 8.5, 600)
$sPos=CalcFontSize("C:Documents and Settingsusername" & @LF & "Application DataNokiaPCSync" & @LF & "SynchDataBackups1232012",100,100)
GUICtrlCreateLabel("", 20, 200, 100,100)
GUICtrlSetFont(-1,$sPos)
GUICtrlSetData(-1, "C:Documents and Settingsusername" & @LF & "Application DataNokiaPCSync" & @LF & "SynchDataBackups1232012")
;GUICtrlSetBkColor(-1,0xFF23FF)

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

Func CalcFontSize($sString, $iW, $iH, $sFont='Arial')

_GDIPlus_Startup()
Local $aResult, $hFont, $i, $iLen = StringLen($sString)
Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle()))
Local $tRectF = DllStructCreate($tagGDIPRECTF)
Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
Local $hFormat = _GDIPlus_StringFormatCreate()
Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
$i = 1
Do
$hFont = _GDIPlus_FontCreate($hFamily, $i, 0)
$aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", "handle", $hGfx, "wstr", $sString, "int", -1, "handle", $hFont, "struct*", $tLayout, "handle", $hFormat, "struct*", $tRectF, "int*", 0, "int*", 0)
_GDIPlus_FontDispose($hFont)
If $aResult[8] < $iLen Or $aResult[9] = 2 Or $i > $iH / 1.45 Then ExitLoop
$i += 1
Until False
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_GraphicsDispose($hGfx)
$tLayout = 0
Return $i-0.0000000001
_GDIPlus_Shutdown()
EndFunc
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

If you have a long path name, inside a label you can use either of the $DT_PATH_ELLIPSIS or $DT_END_ELLIPSIS style settings in the label. Just be aware, that the 2 values in the WindowsConstants.au3 file are set according to Microsoft documentation, but they work backwards, for example the Path_Ellipsis creates a path like this "C:Documents and Settings..." and the End_Ellipsis creates a path like this "C:Documents...Username", this is a problem with Windows and not AutoIt.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 11 months later...

I don't believe this is documented for GUICtrlCreateLabel, so I wouldn't totally rely on it if it isn't documented in the help file. Look at _StringCompact in my signature instead.

Oh and please refrain from necoring old posts.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Yeah, because the help file totally documents everything that you can do with AutoIt.

Correct.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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