Inputbox in the centre of the GUI

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Inputbox in the centre of the GUI

31 Jul 2015, 06:05

I place my GUI somewhere on my two monitors; the used InputBox got no coordinates and is always in the centre of the first monitor.
Is there a simple way to place it always in the centre of the main-GUI?

Thanks in advance.
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Inputbox in the centre of the GUI

01 Aug 2015, 05:41

have no idea with 2 monitors , example inputbox in centre of GUI

Code: Select all

;- inputboxx middle of the GUI
gh:=300
gw:=700
gx:=200
gy:=100
Gui,show,x%gx% y%gy% h%gh% w%gw%,Test
gosub,a1
return
Guiclose:
exitapp

a1:
w:=300
h:=100
InputBox, OutputVar, Test, , , %w%, %h%, % (gx+(gw/2-(w/2))), % (gy+(gh/2-(h/2))),, 2147483,1234
if ErrorLevel
   return
MsgBox, You entered= %OutputVar%
return
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

01 Aug 2015, 10:34

Hi Garry

this code shows it in the middle of the the newly created, unmoved GUI. If I move the GUI and starts the InputBox again, then it is not in the middle of the current GUI - it is where it was before:

Code: Select all

;- inputboxx middle of the GUI
gh:=300
gw:=700
gx:=200
gy:=100
Gui, Add, Button, ga1, Show Inputbox...
Gui,show,x200 y100 h%gh% w%gw%,Test
gosub,a1
return
Guiclose:
exitapp
 
a1:
w:=300
h:=100
InputBox, OutputVar, Test, , , %w%, %h%, % (gx+(gw/2-(w/2))), % (gy+(gh/2-(h/2))),, 2147483,1234
if ErrorLevel
   return
MsgBox, You entered= %OutputVar%
return
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: Inputbox in the centre of the GUI

01 Aug 2015, 10:57

Get the ID of the GUI by using the +Hwnd option of the Gui command, then use WinGetPos to get the current position of the GUI window with that ID right before you create the InputBox.
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

01 Aug 2015, 11:01

Thanks boiler
I will try.
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Inputbox in the centre of the GUI

01 Aug 2015, 11:54

this opens Inputbox in the middle of GUI ( also when GUI moved )
( I use Edit instead of inputbox )

Code: Select all

;- inputboxx middle of the GUI
gh:=300
gw:=700
Gui,add,button , x10 y270 h25 w150 gA1,OPEN_INPUTBOX
Gui,show,x100 y20 h%gh% w%gw%,Test
gosub,a1
return
Guiclose:
exitapp

a1:
w:=300
h:=100
  Gui, +LastFound
  Gui_ID := WinExist()
  WinGetPos,x,y,w1,h1,ahk_id %GUI_ID%
InputBox, OutputVar, Test, , , %w%, %h%, % (x+(gw/2-(w/2))), % (y+(gh/2-(h/2))),, 2147483,1234
if ErrorLevel
   return
MsgBox, You entered= %OutputVar%
return
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

01 Aug 2015, 12:08

Thanks Garry

here is my code fragment which does the same. The main difference is that I use "A" for active window, you use LastFound and WinExist

Code: Select all

...
WinGetPos , Xpos, Ypos, Width, Height, A    ; Data from "a"ctive window
aktb := 330	; set width for Inputbox
akth := 150	; set height for Inputbox
InputBox, myvar, MyApp, Please write something,,%aktb%,%akth%,(Xpos + (width * 0.5) - (aktb * 0.5)),Ypos + (height * 0.5) - (akth * 0.5)
I tried to calculate it with a subroutine, but failed:

Code: Select all

aktb := 330	; set width for Inputbox
akth := 150	; set height for Inputbox
InputBox, myvar, MyApp, Please write something,,%aktb%,%akth%,myPos("x",%aktb%),myPos("y",%akth%)

....

mypos(modus,value)
{
    WinGetPos , Xpos, Ypos, Width, Height, A    ; A für aktives Window
    Ifequal, modus, X
    {
        value := (Xpos + (width * 0.5) - (value * 0.5))
    }
    Ifequal, modus, Y
    {
        value := Ypos + (height * 0.5) - (value * 0.5)
    }
}
return
I'm sure there are some stupid errors in it.
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: Inputbox in the centre of the GUI

02 Aug 2015, 12:07

garys script works fine on my end. Does it not work for you?

Instead of using WinExist(), LastFound or "A" explicitly define an hwnd to your gui.

Code: Select all

ibW := 400, ibH := 200
Gui New, +HwndhGui
Gui %hGui%:Add, Button, ga1, Show Inputbox...
Gui %hGui%:show,x200 y100 h300 w700,Test
gosub,a1
return
Guiclose:
exitapp
 
a1:
WinGetPos, gx, gy, gw, gh, ahk_id %hGui%
ibX := Round( gx + ( gw / 2 ) - ( ibW / 2 ) )
, ibY := Round( gy + ( gh / 2 ) - ( ibH / 2 ) )
InputBox, OutputVar, Title, Prompt,, %ibW%, %ibH%, %ibX%, %ibY%,,, Default
if ErrorLevel
   return
MsgBox, You entered= %OutputVar%
return
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

02 Aug 2015, 13:31

Hi dd900

yes, it works for me now.

I tried the +Hwnd before, but made some mistake. I will try again.

If I use 10 Inputboxes with different sizes, I need ten times the code. It would be great to store the WinGetPos and the current calculation in a subroutine / function ....
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: Inputbox in the centre of the GUI

02 Aug 2015, 13:43

You could always use an Array to store the InputBox sizes.

Why do you need so many different sizes?
Last edited by dd900 on 02 Aug 2015, 13:55, edited 1 time in total.
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

02 Aug 2015, 13:49

dd900 wrote:You could always use an Array to ...
Yes, but how this will help to avoid a "WinPetPos + Calculation" section before every InputBox?
dd900 wrote:...Why do you need so many different sizes?
At the moment I need 2 or three different ones, because there is input of short and long strings, and I want to display the value without scrolling (if possible...)
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: Inputbox in the centre of the GUI

02 Aug 2015, 13:59

You will either have to do the calculation everytime you move/resize the gui or whenever you call an InputBox. Otherwise you will not have updated coords and size when the gui has changed positions.

Code: Select all

ibSize := [ { w: 300, h: 200 }, { w: 350, h: 250 } ]
Gui New, +HwndhGui
Gui %hGui%:Add, Button, ga1, Inputbox1
Gui %hGui%:Add, Button, ga2, Inputbox2
Gui %hGui%:show,x200 y100 h300 w700,Test
gosub,a1
return
Guiclose:
exitapp

getWinPos:
WinGetPos, gx, gy, gw, gh, ahk_id %hGui%
ibX := Round( gx + ( gw / 2 ) - ( ibSize[ ib ].w / 2 ) )
, ibY := Round( gy + ( gh / 2 ) - ( ibSize[ ib ].h / 2 ) )
return
 
a1:
ib := 1
gosub getWinPos
InputBox, OutputVar, Title, Prompt,, % ibSize[ ib ].w, % ibSize[ ib ].h, %ibX%, %ibY%,,, Default
gosub ibReturn
return

a2:
ib := 2
gosub getWinPos
InputBox, OutputVar, Title, Prompt,, % ibSize[ ib ].w, % ibSize[ ib ].h, %ibX%, %ibY%,,, Default
gosub ibReturn
return

ibReturn:
if ErrorLevel
   MsgBox, Error
else
	MsgBox, You entered= %OutputVar%
return
This can be shortened even further depending on how you code everything.
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

02 Aug 2015, 14:20

dd900 wrote:You will either have to do the calculation everytime you move/resize the gui or ...
Yes, that's clear.
dd900 wrote:...This can be shortened even further depending on how you code everything.
I have a rather long, slow, low level style to code. "Shortening" is not my speciality :shh:
dd900 wrote:

Code: Select all

....
a1:
ib := 1
gosub getWinPos
....
Yepp, I think that's the trick. Flexible and short.

Thanks :bravo:
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: Inputbox in the centre of the GUI

02 Aug 2015, 15:27

I hope you get everything worked out. Good luck with your script.

Code: Select all

ibSize := { ib1: { w: 400, h: 200 }, ib2: { w: 450, h: 250 } }
Gui New, +HwndhGui
Gui %hGui%:Add, Button, vib1 gibShow, Inputbox1
Gui %hGui%:Add, Button, vib2 gibShow, Inputbox2
Gui %hGui%:show,x200 y100 h300 w700,Test
OnMessage( 0x0047, "WM_WINDOWPOSCHANGED" )
WM_WINDOWPOSCHANGED()
return

GuiClose( hwnd ) {
	exitapp
}

WM_WINDOWPOSCHANGED() {
	global
	WinGetPos, gx, gy, gw, gh, ahk_id %hGui%
	return
}

ibShow() {
	global
	InputBox, OutputVar, Title, Prompt, 0
		, % ibSize[ A_GuiControl ].w
		, % ibSize[ A_GuiControl ].h
		, % Round( gx + ( gw / 2 ) - ( ibSize[ A_GuiControl ].w / 2 ) )
		, % Round( gy + ( gh / 2 ) - ( ibSize[ A_GuiControl ].h / 2 ) ),,, Default
	if ErrorLevel
		MsgBox, Error
	else
		MsgBox, You entered= %OutputVar%
	return
}
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

03 Aug 2015, 05:01

EDIT: sorry - mixed up the forum language. See English below.
------------------------------
Nur zur Vollständigkeit (ich bin mit der aktuellen Lösung zufrieden):

Das Programm ist mathematisch und codetechnisch in Ordnung, aber die Inputbox sitzt zwar ziemlich, aber nicht genau in der Mitte. (Da müssten sich die Mittelpunkte überlagern und die Seitenabstände gleich sein).

Ich habe auf meinem Display "Win 7 x64, 120 dpi, Zoom 125%" - wirkt sich das aus?
-------------------------
Just for completeness (I am satisfied with the current solution):

The program is mathematically and per code ok, but the Inputbox is placed ratherbut not exactly in the middle. (Since the midpoints would overlap and side clearances should be the same).

I have on my screen "Win 7 x64, 120 dpi, 125% Zoom" - does this affect me?
AHK_Centered-InputBox.jpg
Last edited by Peter2 on 03 Aug 2015, 14:18, edited 1 time in total.
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: Inputbox in the centre of the GUI

03 Aug 2015, 13:20

Gui New, +HwndhGui -DPIScale
just me
Posts: 9459
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Inputbox in the centre of the GUI

04 Aug 2015, 03:15

... or:

Code: Select all

#NoEnv
IBSize := [{W: 300, H: 200}, {W: 350, H: 250}]
ScaleDown := 96 / A_ScreenDPI
Gui, New, +HwndhGui
Gui, %hGui%:Add, Button, gIB1, Inputbox1
Gui, %hGui%:Add, Button, gIB2, Inputbox2
Gui, %hGui%:show, x200 y100 h300 w700, Test
Gosub, IB1
Return

Guiclose:
ExitApp

GetWinPos:
WinGetPos, GX, GY, GW, GH, ahk_id %hGui%
IBX := Round((GX + (GW / 2) - (IBSize[IB].W / 2)) * ScaleDown)
IBY := Round((GY + (GH / 2) - (IBSize[IB].H / 2)) * ScaleDown)
Return

IB1:
IB2:
IB := SubStr(A_ThisLabel, 3)
Gosub, GetWinPos
InputBox, OutputVar, Title, Prompt, , % IBSize[IB].w, % IBSize[IB].h, %IBX%, %IBY%, , , Default
If ErrorLevel
   MsgBox, Error
Else
	MsgBox, You entered: %OutputVar%
Return
BTW: Using this code, the whole area of the InputBox will be roughly centered whithin the whole area of the Gui window. If you want to center the client area within the client area of the Gui, you'll need other code.
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Inputbox in the centre of the GUI

04 Aug 2015, 03:22

BTW: Wouldn't that be an interessting feature request for InputBox in a future version of AHK?

The current behaviour - if GUI is on second monitor (or at the border of wide-screen) and the IB appears always in the middle of first monitor - could be improved via an additional option.
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Insanibaccha, PsysimSV and 205 guests