How to vertically center text in label control

3 posts in this topic

Hello Forum,

I'm trying to vertically center text in label controls, but no way. The search on the forum returns no result, or I don't search the right way :>

When I insert a button and a label of same size close to each other, the text is centered on the button, but top aligned on the label, or edit. Which doesn't look really aesthetic :no:

Any idea is welcome

Thanks in advance,


Share this post

Link to post
Share on other sites

Here is one idea to center text vertically and horizontally in a label and edit control.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <String.au3>


Func Example()
    GUICreate("My GUI", 200, 400)

    Local $iCtrlDepth = 200, $iFontSize = 9
    $iCenter = Int($iCtrlDepth / ($iFontSize * 4))
    GUICtrlCreateLabel(_StringRepeat(@CRLF, $iCenter) & "Label", 10, 30, 100, $iCtrlDepth, $SS_CENTER) 
    GUICtrlSetBkColor(-1, 0xA0FFFF)
    GUICtrlSetFont(-1, $iFontSize)

    $iCtrlDepth1 = 150
    $iFontSize = 12
    $iCenter = Int($iCtrlDepth1 / ($iFontSize * 4))
    GUICtrlCreateEdit(_StringRepeat(@CRLF, $iCenter) & "Edit", 10, 40 + $iCtrlDepth, 100, $iCtrlDepth1, $ES_CENTER)
    GUICtrlSetFont(-1, $iFontSize)
    GUICtrlSetBkColor(-1, 0xFFFFA0)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
EndFunc   ;==>Example


Share this post

Link to post
Share on other sites

#3 ·  Posted (edited)

Hi Malkey,

Thanks for your quick answer. This works pretty well for large label height/font size ratio, but my label height is 52 and font size is 12.

This results in approx. bottom aligned text.

I've found another way (for lazy): I put an empty 52 height label, then a 26 height label with text 12 on top of it. Both with same color.

I expected an easier way to do it.

BTW, I don't understand $BS_VCENTER for buttons as they are vertically centered by default. This would be more helpful for labels/edits in my case.



Edited by thoms

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

  • Similar Content

    • natedog102
      By natedog102
      This small UDF helps you save any text to speech to a .wav file. 
      #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 #include-once ; #INDEX# ======================================================================================================================= ; Title .........: _TTStoWav() ; AutoIt Version : ; Language ......: English ; Author(s) .....: natedog102 ; Modifiers .....: ; Forum link ....: ; Description ...: TTS to .wav file using SAPI ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _TTStoWav ; Description ...: TTS to .wav file using SAPI ; Syntax.........: _TTStoWav($sText, $sLocation[, $iRate = 1[, $iVolume = 100]]) ; Parameters ....: $sText - String you want converted to a .wav ; $sLocation - Save location + file name, example: "C:\folder\sample.wav" ; $iRate - TTS speak rate between -10 and 10 ; $iVolume - Volume of the TTS between 0 and 100 ; Return values .: Success - Returns one ; Failure - Returns zero ; Author ........: natedog102 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _TTStoWav($sText, $sLocation, $iRate = 1, $iVolume = 100) $oFileStream = ObjCreate("SAPI.SpFileStream") If @error Then Return 0 EndIf $oFileStream.Format.Type = 39 $oFileStream.Open($sLocation, 3) $oSpeech = ObjCreate('SAPI.SpVoice') If @error Then Return 0 EndIf $oSpeech.AudioOutputStream = $oFileStream $oSpeech.Rate = $iRate $oSpeech.Volume = $iVolume $oSpeech.Speak($sText, 3) $oSpeech.WaitUntilDone(10000) $oFileStream.Close() Return 1 EndFunc ;==>_TTStoWav And here's example usage:
      _TTStoWav("This is a test", "C:\folder-must-exist\sample2.wav") I have never made a UDF before so I don't know if I coded it correctly or if I followed all the recommended guidelines. Apologies if I haven't.
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      in need the path/text present int above shown input box to be copied or retrieved into a variable .. how can i do that?
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Friends i am new to autoit and i am stuck in middle of automation of flashing tool..
      my requirement is i have a window and i have to get the value present in that window (below is the picture)

      in the above window i need to get the value of the highlighted field.the value of this filed keeps on changing...ii need to get the current value present in the field..below are the details of the window and field..

      CAN it be done?

    • houser747
      By houser747
      I have previously used _IEFormElementGetObjByName and _IEFormElementSetValue to enter text into a search box on a form and then submit the form.
      I am now trying to enter text into a search box which is not part of a form. 
      Here is the HTML from the website that i'm trying to enter the data on and then submit the search.
      <div class="row">
          <div class="form-group col-xs-12">
              <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_lblSearchText" for="input-search">Registreringsbeteckning</span>
              <div class="input-group col-xs-12">
                  <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_preSearchText" class="input-group-addon">SE -</span>
                  <input name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$txtSearchText" type="text" value="DTH" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_txtSearchText" class="form-control" />
      <div class="row">
          <div class="form-group col-xs-12">
              <label class="sr-only" for="">Sök</label>
              <input type="submit" name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$btnSearch" value="Sök" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_btnSearch" class="btn btn-primary ladda-button" data-style="expand-right" />
      Many thanks in advance