Jump to content

How to vertically center text in label control


 Share

Recommended Posts

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,

Thoms

Link to comment
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>

Example()

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)

    GUISetState(@SW_SHOW)

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

 

Link to comment
Share on other sites

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.

Thx,

Thoms

Edited by thoms
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

×
×
  • Create New...