Looking for a script to center active window in Windows 11

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
yassin
Posts: 2
Joined: 27 Nov 2021, 14:45

Looking for a script to center active window in Windows 11

27 Nov 2021, 15:26

Hello!

This is my first time using AutoHotKey and I'm looking for a script to make a keyboard shortcut to center active window. And how to make it autostart with Windows. I'm on Windows 11.

Thanks!
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Looking for a script to center active window in Windows 11

27 Nov 2021, 16:46

Use WinGetPos to determine the size of the window (specifying A for the WinTitle parameter to select the active window), then use A_ScreenWidth, A_ScreenHeight, and the appropriate math to determine where you need to move its upper-left corner using WinMove.

See Hotkeys for how to make a keyboard shortcut to trigger the code. To have the script launch with windows, right click on the script file and create a file shortcut, then move that shortcut to the Windows Startup folder.
yassin
Posts: 2
Joined: 27 Nov 2021, 14:45

Re: Looking for a script to center active window in Windows 11

28 Nov 2021, 11:58

Thank you!

Here's the code that I ended up using.

Code: Select all

#c::
SysGet Mon,MonitorWorkArea
WinGetPos ,,,wW,wH,A
WinMove A,,(MonRight-wW)/2,(MonBottom-wH)/2
Return
LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Looking for a script to center active window in Windows 11

29 Dec 2021, 08:56

Do you know why this doesn't work in Windows 10?
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Looking for a script to center active window in Windows 11

29 Dec 2021, 09:09

It does work in Windows 10. If it doesn’t work for you, there are other reasons, such as the particular window you are trying to use or if you have multiple monitors or you didn’t press the correct hotkey (Win+C).
LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Looking for a script to center active window in Windows 11

29 Dec 2021, 10:20

I had to add #SingleInstance,force.

Thank you & Happy New Year!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jameswrightesq, scriptor2016 and 260 guests