Jump to content

Revisiting GUICtrlCreateLabel and text centering

Recommended Posts

(since necroing old post is not allowed/recommended)
With regards to Melba23's script

#include <StaticConstants.au3>

Const $iGUIWidth = 300
Const $iGUIHeight = 160

$sTempGUIMessage = " Correct!" & @CRLF & "520" & " ms"

$hGui = GUICreate("Testing", $iGUIWidth, $iGUIHeight)

; Create the label asking Windows to size it
$cLabel = GUICtrlCreateLabel($sTempGUIMessage, -1, -1, Default, Default, $SS_CENTER)
; And get its size
$aPos = ControlGetPos($hGUI, "", $cLabel)
; Calculate the position
$iX = ($iGUIWidth - $aPos[2]) / 2
$iY = ($iGUIHeight - $aPos[3]) / 2
; And move the label
ControlMove($hGUI, "", $cLabel, $iX, $iY)
GUICtrlSetBkColor(-1, 0xCCFFCC)

Thought it manages to center a label vertically and horizontally, my concern is that, this is only valid for a static label.

What if I altered the text on the label using GUICtrlSetData and the text will no longer be centered.
Doing a ControlGetPos and ControlMove the 2nd time wont work.

I need to center a Text (horizontally AND vertically) on a GUI (either it be Label, or otherwise).
Text can be 1 liner, or 2 lines or more. And I need to be able to set font as well.

Creating a label using: GUICtrlCreateLabel($text, -1, -1, Default, Default, 0x01)
somehow negates font settings: GUICtrlSetFont ($gui, $lab, 400, 0, "Verdana", 5)

Currently, I am thinking of constantly creating and deleting these Labels as needed. But creating and deleting as often as 100ms worries me.

Edited by Burgaud
Link to post
Share on other sites

According to your issue described above.

You could hide control maybe ? So you won't move them and break their status. 


I'm not sure but, maybe you could also try to redraw your GUI with 



My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote


 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

ADO - Wiki


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