Center an AHK window

Get help with using AutoHotkey and its commands and hotkeys
DutchPete
Posts: 98
Joined: 03 Feb 2015, 03:59

Center an AHK window

20 Jun 2018, 06:48

I have the following code to run an AHK script and set it to a specific size. I also want to center the resulting Quick Note window: can someone advise me how to do that?

Code: Select all

Run, "C:\Users\Peter\Scripts\AHK\quicknotes.ahk",,, PID
WinWait, ahk_pid %PID%
WinActivate, ahk_pid %PID%
WinMove, ahk_pid %PID%, , , , 1200, 700
Hellbent
Posts: 143
Joined: 23 Sep 2017, 13:34

Re: Center an AHK window

20 Jun 2018, 07:06

This is mostly pseudo code.

Code: Select all

;Take the width of your screen and divide it by 2
;Then subtract half of the width of the window

x := (A_ScreenWidth//2) - (Window_Width//2)


;Then do the same for the height

y := (A_ScreenHeight//2) - (Window_Height//2)

;Lastly, show you window at x and y

WinMove,Some Window, x%x% y%y%

DutchPete
Posts: 98
Joined: 03 Feb 2015, 03:59

Re: Center an AHK window

20 Jun 2018, 08:00

Thanks Hellbent. Not being too familiar with AHK code, I have tried to integrate your suggestion into my code.

Code: Select all

Run, "C:\Users\Peter\Scripts\AHK\quicknotes.ahk",,, PID
WinWait, ahk_pid %PID%
WinActivate, ahk_pid %PID%
WinMove, ahk_pid %PID%, , , , 1200, 700
x := (A_ScreenWidth//2) - (Window_Width//2)
y := (A_ScreenHeight//2) - (Window_Height//2)
WinMove,Some Window, x%x% y%y%
I don't see where to add my laptop's screen dimensions which are 1366x768
Hellbent
Posts: 143
Joined: 23 Sep 2017, 13:34

Re: Center an AHK window

20 Jun 2018, 08:06

Code: Select all

Run, "C:\Users\Peter\Scripts\AHK\quicknotes.ahk",,, PID
WinWait, ahk_pid %PID%
WinActivate, ahk_pid %PID%
WinMove, ahk_pid %PID%, , , , 1200, 700
x := (A_ScreenWidth//2) - (Window_Width//2)
y := (A_ScreenHeight//2) - (Window_Height//2)
WinMove,Some Window, x%x% y%y%
I don't see where to add my laptop's screen dimensions which are 1366x768

I am not going to test my code below, but it should work if what you had worked for what it was doing before.

Code: Select all

Run, "C:\Users\Peter\Scripts\AHK\quicknotes.ahk",,, PID
WinWait, ahk_pid %PID%
WinActivate, ahk_pid %PID%
Width:=1200
Height:=700
x_var := (A_ScreenWidth//2) - (Width//2) ;Note "A_ScreenWidth" is a variable that is built into ahk that gets the screen resolution for you, same with "A_ScreenHeight"
y_var := (A_ScreenHeight//2) - (Height//2)
WinMove, ahk_pid %PID%, ,x_var ,y_var ,Width,Height
Hope that is clear and that it helps.
swagfag
Posts: 1371
Joined: 11 Jan 2017, 17:59

Re: Center an AHK window

20 Jun 2018, 08:07

Code: Select all

WinGetPos, , , w, h, ahk_pid %PID%
x := (A_ScreenWidth - w) // 2
y := (A_ScreenHeight - h) // 2
WinMove, ahk_pid %PID%, , % x, % y
DutchPete
Posts: 98
Joined: 03 Feb 2015, 03:59

Re: Center an AHK window

20 Jun 2018, 08:18

Hellbent, yes that works fine.
Is there a way to move the window up a few pixels to the height dimension?
DutchPete
Posts: 98
Joined: 03 Feb 2015, 03:59

Re: Center an AHK window

20 Jun 2018, 08:23

Code: Select all

WinGetPos, , , w, h, ahk_pid %PID%
x := (A_ScreenWidth - w) // 2
y := (A_ScreenHeight - h) // 2
WinMove, ahk_pid %PID%, , % x, % y
So Swagfag, your code is slightly different from Hellbent's, but when I use it (total code script below), it does not work - the window remains unchanged. Hellbent's solution works.

Code: Select all

Run, "C:\Users\Peter\Scripts\AHK\quicknotes.ahk",,, PID
WinWait, ahk_pid %PID%
WinActivate, ahk_pid %PID%
Width:=1200
Height:=700
WinGetPos, , , w, h, ahk_pid %PID%
x := (A_ScreenWidth - w) // 2
y := (A_ScreenHeight - h) // 2
WinMove, ahk_pid %PID%, , % x, % y
Hellbent
Posts: 143
Joined: 23 Sep 2017, 13:34

Re: Center an AHK window

20 Jun 2018, 08:26

Hellbent, yes that works fine.
Is there a way to move the window up a few pixels to the height dimension?
if you want to tweak your y position you can just take the result of the y_var and add or subtract from it

Code: Select all

;before y_var was set like this
y_var := (A_ScreenHeight//2) - (Height//2)
;or
y_var := (A_ScreenHeight-Height)//2

;You can adjust it in a few ways

;( 1 )
; adding or subtracting
y_var := (A_ScreenHeight-Height)//2
y_Var += 100  ;adding (setting the window lower on the screen)
y_Var -= 100  ;subtracting (setting the window higher on the screen)

; ( 2)
; Adjusting in the same line as you set y_Var

; ADDING (Moving window lower)
y_var := (A_ScreenHeight-Height)//2 + 100

; SUBTRACTING  (Moving window higher)
y_var := (A_ScreenHeight-Height)//2 - 100
if you want the y to be the top of the screen and only center the x then you can omit the y:=(blah blah blah),
and just say that y:=0
DutchPete
Posts: 98
Joined: 03 Feb 2015, 03:59

Re: Center an AHK window

20 Jun 2018, 08:55

if you want to tweak your y position you can just take the result of the y_var and add or subtract from it

Code: Select all

;before y_var was set like this
y_var := (A_ScreenHeight//2) - (Height//2)
;or
y_var := (A_ScreenHeight-Height)//2

;You can adjust it in a few ways

;( 1 )
; adding or subtracting
y_var := (A_ScreenHeight-Height)//2
y_Var += 100  ;adding (setting the window lower on the screen)
y_Var -= 100  ;subtracting (setting the window higher on the screen)

; ( 2)
; Adjusting in the same line as you set y_Var

; ADDING (Moving window lower)
y_var := (A_ScreenHeight-Height)//2 + 100

; SUBTRACTING  (Moving window higher)
y_var := (A_ScreenHeight-Height)//2 - 100
if you want the y to be the top of the screen and only center the x then you can omit the y:=(blah blah blah),
and just say that y:=0
Thanks Hellbent, you're a star. I now got it working exactly the way I want. Thanks a lot :D
swagfag
Posts: 1371
Joined: 11 Jan 2017, 17:59

Re: Center an AHK window

20 Jun 2018, 15:57

it should work man, its pretty much the same script:

Code: Select all

Run, notepad.exe, , , PID
WinWait, ahk_pid %PID%
WinActivate, ahk_pid %PID%
WinGetPos, , , w, h, ahk_pid %PID%
x := (A_ScreenWidth - w) // 2
y := (A_ScreenHeight - h) // 2
WinMove, ahk_pid %PID%, , % x, % y
DutchPete
Posts: 98
Joined: 03 Feb 2015, 03:59

Re: Center an AHK window

21 Jun 2018, 10:40

@Swagfag: thanks, but I now have a working version so I'll stick with that; I hope you don't mind.
Thanks for your help.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot] and 58 guests