Center Active Window Using AutoHotkey

No more dragging or guessing—center any window perfectly, even across multiple monitors.

As someone who’s constantly alt-tabbing between code editors, browser tabs, and terminal windows, I’ve lost hours dragging windows to the approximate center of my screen. Windows’ built-in snapping? Clunky. Third-party tools? Bloatware. Let’s fix this with 8 lines of AutoHotkey magic that works flawlessly on multi-monitor setups.

Before you ask

Yes, this handles ultrawide monitors. Yes, it respects taskbar positioning. No, it won’t break when you unplug your laptop from a dock.

The Script

Features:

; Center active window
#c::  {
    WinGetPos(&X, &Y, &W, &H, "A")
    CoordMode("Mouse", "Screen")
    MouseGetPos(&mouseX, &mouseY)
    monitorNumber := MonitorGetPrimary()

    ; Find which monitor the window is actually on
    Loop MonitorGetCount() {
        MonitorGetWorkArea(A_Index, &mLeft, &mTop, &mRight, &mBottom)
        if (X >= mLeft && X < mRight && Y >= mTop && Y < mBottom) {
            monitorNumber := A_Index
            break
        }
    }

    ; Calculate perfect center for THAT monitor
    MonitorGetWorkArea(monitorNumber, &monitorLeft, &monitorTop, &monitorRight, &monitorBottom)
    newX := monitorLeft + (monitorRight - monitorLeft - W) / 2
    newY := monitorTop + (monitorBottom - monitorTop - H) / 2
    WinMove newX, newY,,, "A"
}

How to Set It Up

  1. Install AutoHotkey v2+ (Download here).
  2. Save the script as WindowCenteringWizard.ahk.
  3. Right-click → Run Script. (Want it always available? Add to startup).

Customize The Script

Gotchas

Explanation: How It Works

Or: “Why you shouldn’t just copy-paste code blindly”

This script isn’t malware, but blindly running code you don’t understand is how you end up debugging a 3 AM disaster. Let’s break it down so you’re not left guessing

1. The Mouse Trick (That Isn’t Pointless)

CoordMode("Mouse", "Screen")
MouseGetPos(&mouseX, &mouseY)

Why? AutoHotkey sometimes struggles with windows spanning monitors. Grabbing the mouse position acts like a “hint” to avoid centering your Notepad window on the wrong screen.

2. The Monitor Detective Work

Loop MonitorGetCount() {
    MonitorGetWorkArea(A_Index, &mLeft, &mTop, &mRight, &mBottom)
    if (X >= mLeft && X < mRight && Y >= mTop && Y < mBottom) {
        monitorNumber := A_Index
        break
    }
}

Translation: It hunts down which monitor your window is actually on by checking coordinates against all connected displays. No more “why’s my window halfway offscreen?!” moments.

3. Math That (Actually) Makes Sense

newX := monitorLeft + (monitorRight - monitorLeft - W) / 2
newY := monitorTop + (monitorBottom - monitorTop - H) / 2

Some algebra that I actually can understand—it’s calculating the exact center of your monitor’s usable area (subtracting taskbars/docks).

Conclusion

This script has lived in my system tray for almost a year—no crashes, no conflicts. Next time you’re arranging windows, hit Win + C and reclaim your sanity.