Need Autohotkey to center active window

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

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")

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
}

Tags:

Autohotkey