Jump to content

Simple way to center text vertically, horizontally, or both?


Recommended Posts

  • Moderators

SharkyEXE,

It works perfectly for me:

#include <StaticConstants.au3>
#include "StringSize.au3"

Const $iGUIWidth = 800
Const $iGUIHeight = 200

$hGui = GUICreate("", 800, 200)
GUISetFont(10)
GUISetState()

For $iFontSize = 10 To 20

    WinSetTitle($hGui, "", "FontSize = " & $iFontSize)
    ; Get the size of label needed to hold the text
    $aRet = _StringSize("Both vertically" & @CRLF & "and horizontally centered", $iFontSize)
    ; Add a bit to cater for Windows sizing eccentricity
    $aRet[2] += 5
    ; Work out where it needs to be on the GUI
    $iX = ($iGUIWidth - $aRet[2]) / 2
    $iY = ($iGUIHeight - $aRet[3]) / 2
    ; And create it
    $cLabel = GUICtrlCreateLabel("Both vertically" & @CRLF & "and horizontally centered", $iX, $iY, $aRet[2], $aRet[3], $SS_CENTER)
    GUICtrlSetFont($cLabel, $iFontSize)
    Sleep(1000)
    GUICtrlDelete($cLabel)

Next

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 post
Share on other sites
  • Moderators

SharkyEXE,

Quote

very-very need fix

As I have tried to explain several times, the dimensions returned by the StringSize function are those given by the Windows API call to measure the passed characters in a specified font, size and type. If the values returned are not what you think they should be then take it up with MS, not me.

And the average difference in the "top" and "bottom" measurements of the text is just over 1mm - the maximum difference is 1.8mm. This is "very-very" small - why do you need such precision in the vertical position?

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 post
Share on other sites
  • Moderators

SharkeyEXE,

I am not offended in the slightest - just trying to explain the limitations that Windows imposes on us!

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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...