Gui window center regardless of sizing?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
RDC
Posts: 112
Joined: 29 Jan 2023, 10:22

Gui window center regardless of sizing?

Post by RDC » 22 Mar 2023, 09:28

Can anyone tell me what I am doing wrong? I am simply trying to center text in a resizable Gui window regardless of it's size being changed on the fly. So far I can get the text to center the X direction, but not the Y position and when I do get the text to center, it doesn't stay that way when I drag the edges to resize the window. Really trying to wrap my head around these controls, but I'm obviously not quite grasping it.

Code: Select all

^T::

Gui 
   +AlwaysOnTop 
   -DPIScale
   +Resize
Gui, Color, BLACK
Gui, Margin, 10, 10
Gui, Font, s16 w500 cBLUE

Gui, Add, Text, vMyText, Text for example.
Gui, Show, , Resizable GUI Window
Return

CenterText() {
    GuiControlGet, ControlWidth, % MyText, W
    GuiControlGet, ControlHeight, % MyText, H
    GuiControlGet, GuiWidth, , W
    GuiControlGet, GuiHeight, , H
    X := (GuiWidth - ControlWidth) / 2
    Y := (GuiHeight - ControlHeight) / 2
    GuiControl, Move, % MyText, %X%, %Y%
}
Return

^Esc::
     Gui, Destroy
     ExitApp

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Gui window center regardless of sizing?

Post by Hellbent » 22 Mar 2023, 09:53

You had a number of issues with your code and syntax.

This needs to be tweaked to deal with the windows border.

Code: Select all


^T::

Gui 
   +AlwaysOnTop 
   -DPIScale
   +Resize
   +HwndGuiHwnd ;<<<<-----    
   
Gui, Color, BLACK
Gui, Margin, 10, 10
Gui, Font, s16 w500 cBLUE

Gui, Add, Text, vMyText, Text for example.
Gui, Show, , Resizable GUI Window
Return

GuiSize:
	GuiControlGet, pos , pos , MyText
    ControlWidth := posW , ControlHeight := posH + 26
    WinGetPos,,, GuiWidth , GuiHeight , % "ahk_id " GuiHwnd
    X := (GuiWidth - ControlWidth) / 2
    Y := (GuiHeight - ControlHeight) / 2
    GuiControl, Move,  MyText , x%X% y%Y%
	return


^Esc::
     Gui, Destroy
     ExitApp

User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: Gui window center regardless of sizing?

Post by Hellbent » 22 Mar 2023, 10:12

This should deal with the windows border ( caption / title )

Code: Select all

^T::
Gui 
   +AlwaysOnTop 
   -DPIScale
   +Resize
   +HwndGuiHwnd ;<<<<-----    
   
Gui, Color, BLACK
Gui, Margin, 10, 10
Gui, Font, s16 w500 cBLUE

Gui, Add, Text, vMyText, Text for example.
Gui, Show, , Resizable GUI Window
Return

GuiSize:
	GuiControlGet, pos , pos , MyText
    ControlWidth := posW , ControlHeight := posH ;+ 26
	VarSetCapacity( RECT , 16 , 0 ) ;https://www.autohotkey.com/boards/viewtopic.php?t=60924#p257561
	DllCall( "user32\GetClientRect" , Ptr , GuiHwnd , Ptr , &RECT )
	DllCall( "user32\ClientToScreen" , Ptr , GuiHwnd , Ptr , &RECT )
	GuiWidth := NumGet( &RECT , 8 , "Int" )
	GuiHeight := NumGet( &RECT , 12 , "Int" )
    X := (GuiWidth - ControlWidth) / 2
    Y := (GuiHeight - ControlHeight) / 2
    GuiControl, Move,  MyText , x%X% y%Y%
	return

^Esc::
     Gui, Destroy
     ExitApp

Post Reply

Return to “Ask for Help (v1)”