8

I'm looking for a way to center the active window on the desktop using Autohotkyes. Can someone give me a script that I could use please. Thanks

Carlos
  • 311

5 Answers5

10

These answers use title matching, which could apply to several windows. This will center only the active window, when you press win+c.

#c::
WinExist("A")
WinGetPos,,, sizeX, sizeY
WinMove, (A_ScreenWidth/2)-(sizeX/2), (A_ScreenHeight/2)-(sizeY/2)
return
Dirigible
  • 301
  • 3
  • 5
  • I like this as the active window being moved to center of screen doesn't change size. But, what does the "A" represent? Is that like a wildcard for any window that's active? – fohrums Dec 08 '18 at 11:09
6

For simplicity and adaptibilty I created an additional super short script which just takes the active window and centers it but also resizes it with the given width and height. This is maybe not what you exactly asked for, apart from being some years late. But, this is one thing about windows management I expect from OSes in times of resolutions above FHD. Hopefully someone else has need of it. hf

; HOTKEYS
#!Up::CenterActiveWindow() ; if win+alt+↑ is pressed

CenterActiveWindow()
{
    windowWidth := A_ScreenWidth * 0.7 ; desired width
    windowHeight := A_ScreenHeight ; desired height
    WinGetTitle, windowName, A
    WinMove, %windowName%, , A_ScreenWidth/2-(windowWidth/2), 0, windowWidth, windowHeight
}
bernout
  • 61
  • I've added a WinRestore right before the WinMove. Otherwise applying this to a maximized window resizes it in a still maximized state, which makes the window behave weirdly when interacting with it again. I've also noticed if you use WinExist("A") (see Dirigible's answer) you need skip the first two parameters of WinMove explicitly: WinMove,,, x, y, width, height. WinMove, x, y, width, height does not work, while WinMove, x, y does work. – Andreas Linnert Jul 12 '23 at 08:48
6

http://www.autohotkey.com/docs/commands/WinMove.htm was the first result on google with the phrase "autohotkey center window". It may help you out. See the example script.

Example Script

Run, calc.exe
WinWait, Calculator
WinMove, 0, 0 ; Move the window found by WinWait to the upper-left corner of the screen.

SplashTextOn, 400, 300, Clipboard, The clipboard contains:`n%clipboard%
WinMove, Clipboard, , 0, 0 ; Move the splash window to the top left corner. 
Msgbox, Press OK to dismiss the SplashText
SplashTextOff

; The following function centers the specified window on the screen:
CenterWindow(WinTitle)
{
    WinGetPos,,, Width, Height, %WinTitle%
    WinMove, %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
}

Customized

; The following function centers the specified window on the screen:
CenterWindow(WinTitle)
{
    WinGetPos,,, Width, Height, %WinTitle%
    WinMove, %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
    ;Msgbox, Made the Notepad Center?
}

Run, file.exe

CenterWindow("title of file.exe")
JohannesM
  • 1,020
3

Using JohannesM's CenterWindow() function, this script centers the active window on the hotkey RightShift & C. The hotkey RightShift & B moves the centered window back to its original location, height and width.

To user the script, copy the code and save it as a .ahk file. I used the filename center active window_RShiftC_B.ahk

With Autohokey installed, run the script by double clicking on the file center active window_RShiftC_B.ahk

RShift & c::

global windowName
global X
global Y
global begWidth
global begHeight

WinGetTitle, windowName, A

WinGetPos, X, Y, begWidth, begHeight, %windowName%

CenterWindow(windowTitleVariable)
{
    WinGetPos,,, Width, Height, %windowTitleVariable%
    WinMove, %windowTitleVariable%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2    )-(Height/2)
}

CenterWindow(windowName)

return


RShift & b::

WinMove, %windowName%,, X, Y, begWidth, begHeight

return  
  • Is there any difference between Rshift & C:: and >+^::? – Henrik Sep 27 '20 at 16:52
  • This is a great script. I use it. I found that, if your window is already the full height of the screen minus the height of the taskbar, centering the window pushes the bottom of it below the taskbar, cutting off visibility of the bottom edge of the window.

    A user can fix this by adding "-18" (or whatever number of pixels is needed) to the end of the equation that centers the window vertically. So, instead of "(A_ScreenHeight/2 )-(Height/2)," it is "(A_ScreenHeight/2 )-(Height/2)-18."

    You'll have to experiment with the number of pixels to subtract until it's right for your setup.

    – loopernow Nov 18 '23 at 16:15
1

For multiple monitors, if you want a script that centers the window on the current monitor, I found this person's lovely script here on the AutoHotkey.com forum. Thank you kon! Also fixed an issue with it moving desktop icons. Set it up so it will only center windows that have a minimize button. Got the code here from SKAN on the AutoHotkey.com forum. Thank you SKAN!

I added the #c:: at the beginning so that the hotkey to activate this script is set to Windows+C, which I felt was appropriate since Windows already allows you to manipulate windows using Windows+Arrow Keys.

Disclaimer:

Fixed this downside. Thank you SKAN! Sadly, there is a downside to this script. If you use it when there is no active windows, the shortcuts on your desktop will get re-arranged temporarily until you attempt to move a shortcut. Once you attempt to move the shortcut, all of them will revert back to their original locations. I doubt the shortcuts are actually moving, but I don't actually understand this script, so I'm not sure why this happens. It's not a huge deal in my opinion, but I figured I'd put in the disclaimer.

#c::

winHandle := WinExist("A") ; The window to operate on

; Don't worry about how this part works. Just trust that it gets the ; bounding coordinates of the monitor the window is on. ;-------------------------------------------------------------------------- VarSetCapacity(monitorInfo, 40), NumPut(40, monitorInfo) monitorHandle := DllCall("MonitorFromWindow", "Ptr", winHandle, "UInt", 0x2) DllCall("GetMonitorInfo", "Ptr", monitorHandle, "Ptr", &monitorInfo) ;--------------------------------------------------------------------------

workLeft := NumGet(monitorInfo, 20, "Int") ; Left workTop := NumGet(monitorInfo, 24, "Int") ; Top workRight := NumGet(monitorInfo, 28, "Int") ; Right workBottom := NumGet(monitorInfo, 32, "Int") ; Bottom WinGetPos,,, W, H, A WinGet, Style, Style, A If ( Style & 0x20000 ) ; WS_MINIMIZEBOX { WinMove, A,, workLeft + (workRight - workLeft) // 2 - W // 2, workTop + (workBottom - workTop) // 2 - H // 2 }