Jump to content

Vertical Centering of Label Text


CYCho
 Share

Recommended Posts

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.
 

HotKeySet("{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 by CYCho
Link to comment
Share on other sites

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.

#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 by pixelsearch
typo
Link to comment
Share on other sites

  • Moderators

CYCho,

Using a temporary GUI was exactly how I started with StringSize before switching to the current GetTextExtentPoint32W method - great minds think alike!

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

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) :lol:

Musashi-C64.png

"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

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