Skip to content

Instantly share code, notes, and snippets.

@straef
Last active September 22, 2024 22:03
Show Gist options
  • Save straef/e8235d1f2d1ac8c625326b61e76128f3 to your computer and use it in GitHub Desktop.
Save straef/e8235d1f2d1ac8c625326b61e76128f3 to your computer and use it in GitHub Desktop.
AutoHotKey Simple Center

Simple Window Center

now updated for v2!

Simple Window Center is a script for AutoHotKey that simply centers the active window on the current monitor, no snapping, no resizing.

Hotkey used in this script is Win + Alt + Up.

Changelog:

  • v1.0 | 2020.10.29
    • initial version and subsequent figuring out of how gists work (like adding a read-me)
  • v1.1 | 2020.12.01
    • added specific information for making sublime text the default program to edit, without changing default program to open, using the windows registry
  • v1.2 | 2021-05-17
    • trimmed description
  • v1.3 | 2021.09.04
    • updated method of replacing notepad as default text editor to use sublime launcher
  • v2.0 | 2024.09.22
    • drastically trimmed description (see links for past sources)
    • removed problematic notepad replacement methods
    • updated script to use autohotkey v2
    • slight optimizations

links

AWMooreCO - https://gist.github.com/AWMooreCO/1ef708055a11862ca9dc
Cerothen - https://gist.github.com/Cerothen/b52724498da640873fa27052770ac10d
park-brian - https://gist.github.com/park-brian/f3f790e559e5145b99bf0f19c7928dd8

#Requires AutoHotkey v2
; SendMode Input
; centers current window without changing dimensions by pressing win + alt + up-arrow
#!Up::CenterActiveWindow()
CenterActiveWindow()
{
; get the current monitor from the active window handle
NumPut("UInt", 40, buf := Buffer(40,0))
monitorHandle := DllCall("MonitorFromWindow", "Int", WinExist("A"), "UInt", 2)
DllCall("GetMonitorInfoW", "Ptr", monitorHandle, "Ptr", buf)
; get active monitor dimensions and offsets
mXo := NumGet(buf, 20, "Int")
mYo := NumGet(buf, 24, "Int")
mXd := NumGet(buf, 28, "Int") - mXo
mYd := NumGet(buf, 32, "Int") - mYo
; get active window dimensions and calculate target offset for centering
WinGetPos(,, &wXd, &wYd, "A")
tXo := ( mXd / 2 + mXo ) - ( wXd / 2 )
tYo := ( mYd / 2 + mYo ) - ( wYd / 2 )
; move window to center of monitor
WinMove(tXo, tYo,,, "A")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment