CYCho Posted February 20, 2021 Share Posted February 20, 2021 (edited) Suppose we want to put a text of variable lengths in the vertical center of two horizontal lines. The text is mostly one liner, but some are so long that they have to be wrapped to 2 lines. Unfortunately, GUICtrlCreateLabel() doesn't have a style option for vertical centering of the text. $SS_CENTERIMAGE works, but it does not allow wrapping. I thought I could adjust the vertical position of the label control if I know the text is wrapped. But I could not find a function to determine if the text is wrapped or not. StringSize UDF is too good and too cumbersome for this small purpose. Creating a temporary, hidden label control was the solution. It takes 4 to 6 milliseconds, which is about the same as, or a little faster than, StringSize UDF. expandcollapse popupHotKeySet("{ESC}", "Quit") local $hGUI = GUICreate("Vertical Center", 290, 200, -1, -1) GUISetFont(12, 400) Local $sLine = "-----------------------------------" Local $sShort = "A short string of characters" Local $sLong_1 = "A long string of characters in ORIGINAL label position" Local $sLong_2 = "A long string of characters in ADJUSTED label position" Local $iLabelWidth = 250 Local $idLabel_1 = GUICtrlCreateLabel($sLine, 20, 20, $iLabelWidth, 20) Local $idLabel_2 = GUICtrlCreateLabel($sShort, 20, 48, $iLabelWidth, 35) Local $idLabel_3 = GUICtrlCreateLabel($sLine, 20, 80, $iLabelWidth, 20) GUISetState() Local $sString = $sShort, $tmpGUI, $tmpLabel, $aPos While 1 ; The next 4 lines of code calculate the pixel width of a string. $tmpGUI = GUICreate("") GUISetFont(12, 400) ; keep font the same as main GUI $tmpLabel = GUICtrlCreateLabel($sString, 0, 0) $aPos = ControlGetPos($tmpGUI, "", $tmpLabel) GUIDelete($tmpGUI) If $aPos[2] <= $iLabelWidth Then ; text is one liner $sString = $sShort GUICtrlSetData($idLabel_2, $sString) GUICtrlSetPos($idLabel_2, 20, 48) $sString = $sLong_1 Else ; text is wrapped GUICtrlSetData($idLabel_2, $sString) Sleep(5000) $sString = $sLong_2 GUICtrlSetData($idLabel_2, $sString) GUICtrlSetPos($idLabel_2, 20, 40) $sString = $sShort EndIf Sleep(5000) WEnd Func Quit() GUIDelete($hGUI) Exit EndFunc Edited February 20, 2021 by CYCho Zmy, pixelsearch and Musashi 3 zPlayer - A Small Audio and Video Player Time Sync + SystemTimeAdjustment Link to comment Share on other sites More sharing options...
CYCho Posted February 20, 2021 Author Share Posted February 20, 2021 (edited) It is important to keep the font of temporary GUI the same as main GUI, because the string size is affected by the font size and style. Edited February 20, 2021 by CYCho zPlayer - A Small Audio and Video Player Time Sync + SystemTimeAdjustment Link to comment Share on other sites More sharing options...
pixelsearch Posted February 20, 2021 Share Posted February 20, 2021 (edited) Hi Cycho Nice idea to test the pixel width of a string in a temporary GUI. In a final script, it won't probably be tested over and over within a while Wend loop, but as we're in an example script... Just for fun, I tried another way, keeping the same position of the label. It works on my computer, not sure it will work accurately everywhere. expandcollapse popup#include <StaticConstants.au3> HotKeySet("{ESC}", "Quit") local $hGUI = GUICreate("Vertical Center", 290, 200, -1, -1) GUISetFont(12, 400) Local $sShort = "A short string of characters" Local $sLong = "A long string of characters in ORIGINAL label position" Local $iLabelWidth = 250 Local $idLabel = GUICtrlCreateLabel($sShort, 20, 48, $iLabelWidth, 40, $SS_SUNKEN) GUISetState() Local $sString = $sShort, $tmpGUI, $tmpLabel, $aPos While 1 ; The next 4 lines of code calculate the pixel width of a string. $tmpGUI = GUICreate("") GUISetFont(12, 400) ; keep font the same as main GUI $tmpLabel = GUICtrlCreateLabel($sString, 0, 0) $aPos = ControlGetPos($tmpGUI, "", $tmpLabel) GUIDelete($tmpGUI) If $aPos[2] <= $iLabelWidth Then ; text is one liner $sString = $sShort GUICtrlSetStyle($idLabel, $SS_CENTERIMAGE) GUICtrlSetData($idLabel, $sString) $sString = $sLong Else ; text is wrapped $sString = $sLong GUICtrlSetStyle($idLabel, 0) GUICtrlSetData($idLabel, $sString) $sString = $sShort EndIf Sleep(3000) WEnd Func Quit() GUIDelete($hGUI) Exit EndFunc Edited February 20, 2021 by pixelsearch typo Zmy, CYCho and Musashi 3 Link to comment Share on other sites More sharing options...
CYCho Posted February 20, 2021 Author Share Posted February 20, 2021 36 minutes ago, pixelsearch said: keeping the same position of the label Excellent! It works on my Windows 10, too. zPlayer - A Small Audio and Video Player Time Sync + SystemTimeAdjustment Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 20, 2021 Moderators Share Posted February 20, 2021 CYCho, Using a temporary GUI was exactly how I started with StringSize before switching to the current GetTextExtentPoint32W method - great minds think alike! M23 Musashi and CYCho 2 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Musashi Posted February 20, 2021 Share Posted February 20, 2021 3 hours ago, pixelsearch said: ... not sure it will work accurately everywhere. @pixelsearch and @CYCho : Both scripts even work on my stone age testing computer (Win7 SP1 64 Bit, AutoIt 3.3.14.0) pixelsearch and CYCho 2 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now