Center an AHK window

Get help with using AutoHotkey and its commands and hotkeys
DutchPete
Posts: 87
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] [Download] GeSHi © Codebox Plus

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: 142
Joined: 23 Sep 2017, 13:34

Re: Center an AHK window

20 Jun 2018, 07:06

This is mostly pseudo code.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

DutchPete
Posts: 87
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] [Download] GeSHi © Codebox Plus

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: 142
Joined: 23 Sep 2017, 13:34

Re: Center an AHK window

20 Jun 2018, 08:06

DutchPete wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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: 842
Joined: 11 Jan 2017, 17:59

Re: Center an AHK window

20 Jun 2018, 08:07

Code: [Select all] [Download] GeSHi © Codebox Plus

WinGetPos, , , w, h, ahk_pid %PID%
x := (A_ScreenWidth - w) // 2
y := (A_ScreenHeight - h) // 2
WinMove, ahk_pid %PID%, , % x, % y
DutchPete
Posts: 87
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: 87
Joined: 03 Feb 2015, 03:59

Re: Center an AHK window

20 Jun 2018, 08:23

swagfag wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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: 142
Joined: 23 Sep 2017, 13:34

Re: Center an AHK window

20 Jun 2018, 08:26

DutchPete wrote: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] [Expand] [Download] GeSHi © Codebox Plus



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: 87
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] [Expand] [Download] GeSHi © Codebox Plus



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: 842
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] [Download] GeSHi © Codebox Plus

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: 87
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: almatrah, mshall and 55 guests