Center GUI button in the x axis. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
KilliK
Posts: 224
Joined: 10 Mar 2016, 21:19

Center GUI button in the x axis.

21 Oct 2019, 13:23

Hello.

I have created this small GUI. My problem is that I cant center the EXIT button in its x axis.
The solutions I have found are for centering the buttons in the GUI box, not to the axis where the button is positioned.
So how can I do that?

here is the code with the buttons. they are positioned in two columns and three rows. The Exit button is the only one in its own row.
The GUI box is static, not dynamic.

Code: Select all

Gui, Add, button, section w150 h35, 1
Gui, Add, button, ys w150 h35, 2
Gui, Add, button, xs section w150 h35, 3
Gui, Add, button, ys w150 h35, 4
Gui, Add, button, center w200 h35, EXIT
Gui, Show, Center, Work Selector
Return
User avatar
Hellbent
Posts: 933
Joined: 23 Sep 2017, 13:34

Re: Center GUI button in the x axis.  Topic is solved

21 Oct 2019, 13:48

Try:

Code: Select all

#SingleInstance,Force
Gui,+HwndGuiHwnd
Gui, Add, button, section w150 h35, 1
Gui, Add, button, ys w150 h35, 2
Gui, Add, button, xs section w150 h35, 3
Gui, Add, button, ys w150 h35, 4
Gui, Show, Center, Work Selector
WinGetPos,,,W,,ahk_Id %GuiHwnd%
Gui, Add, button,% "x" floor(W/2)-100 " w200 h35", EXIT
Gui, Show,AutoSize
return
GuiClose:
GuiContextMenu:
*Esc::
   ExitApp
KilliK
Posts: 224
Joined: 10 Mar 2016, 21:19

Re: Center GUI button in the x axis.

21 Oct 2019, 20:00

thank you, I will test it right now. I have one question though. why do you subtract 100 in teh function?
User avatar
Hellbent
Posts: 933
Joined: 23 Sep 2017, 13:34

Re: Center GUI button in the x axis.

21 Oct 2019, 20:12

KilliK wrote:
21 Oct 2019, 20:00
thank you, I will test it right now. I have one question though. why do you subtract 100 in teh function?

Code: Select all

Gui, Add, button,% "x" floor(W/2)-100 " w200 h35", EXIT
The -100 is half the width of the button w200

I could have wrote it like this too.

Code: Select all

Gui, Add, button,% "x" floor(W/2-100) " w200 h35", EXIT
What it is doing is taking half of the guis width and subtracting half of the buttons width to get the starting point that the button needs to sit to be centered.
KilliK
Posts: 224
Joined: 10 Mar 2016, 21:19

Re: Center GUI button in the x axis.

26 Oct 2019, 08:13

thanks, your code works fine. :bravo:

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Google [Bot] and 87 guests