Simply center a button !?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ozzii
Posts: 486
Joined: 30 Oct 2013, 06:04

Simply center a button !?

12 Jan 2014, 05:05

Hi,
I'm new to AHK and I have a question:
How can I do to simply center a button on a GUI?
I've search the internet but found nothing simple (or it's me ;) )

There is some conditions:
-I don't know the size of the gui
-The GUI is not resizable
-The button have a fix width, height

Thanks in advance.
just me
Posts: 9573
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Simply center a button !?

12 Jan 2014, 06:14

You may use the :arrow: GuiSize label:

Code: Select all

#NoEnv
Centered := False ; initial value = False / not centered
BtnW := 200 ; fixed button width
BtnH := 30  ; fixed button height
Random, GuiW, 300, 800  ; rendom GUI width
Random, GuiH, 100, 400  ; random GUI height
Gui, Add, Button, w%BtnW% h%BtnH% vCenteredBtn, Centered Button
Gui, Show, w%GuiW% h%GuiH%, Centered Button
Return

GuiClose:
ExitApp

GuiSize:
   If (Centered = False) { ; the button hasn't been centered as yet
      ; Calculate the horizontally centered X-position
      BtnX := (A_GuiWidth - BtnW) // 2
      ; Calculate the vertically centered Y-position
      BtnY := (A_GuiHeight - BtnH) // 2
      ; Center the button
      GuiControl, Move, CenteredBtn, x%BtnX% y%BtnY%
      ; Mark as centered
      Centered := True
   }
Return
ozzii
Posts: 486
Joined: 30 Oct 2013, 06:04

Re: Simply center a button !?

12 Jan 2014, 06:41

Thanks...
I thought that the guisize was just for a resizable gui!!!

I still think that a build in command will be great!!!
User avatar
ankitkraken
Posts: 84
Joined: 01 Jun 2019, 08:47
Location: India

Re: Simply center a button !?

09 Nov 2019, 01:50

Is there a simpler method available now?

Code: Select all

x+10
x-10
moves the button after the previous element ends or before it begins. Is there no way to center the buttons relative to the previous elements?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Descolada, fiaztv1, JKJadan, Pianist and 212 guests