Skip to content

Instantly share code, notes, and snippets.

@straef
Last active September 4, 2021 21:51
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

Simple Window Center is a script for AutoHotKey that simply centers the active window on the current monitor. Plenty of times I have windows open, which I do not want to make fullscreen, that I would like to center in my monitor. I did not want any fancy functionality for snapping or anything else, but the only scripts I had previously found also resized the window.

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

The three commented lines at the bottom are for investigating the values stored in the monitorInfo variable, and left in for reference. Removing them will in no way alter the function of the script and is recommended. If you know what all the other values in the array represent please leave a comment informing me, because I have not been able to find it out.

I was only able to do this by inspecting and appropriating pieces from these gists by AWMooreCO, Cerothen, and park-brian; which offer neat functionality for window control. they gave me the insight and inspiration to finally fix the window-centering script I was previously using.

In addition they provide very good information on how to use Sublime Text to edit and run AutoHotKey scripts, which I recommend implementing with a few tweaks. I also wanted to set Sublime Text as my default text editor, and previously manually edited each filetype registry entry to replace notepad. A better way of doing it is to redirect calls to notepad in the registry with a single Key. (sublime as default editor source) The simplest method I have found for this that does not cause errors when dealing with spaces in the filepath is to use sublime launcher.

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
    • removed:

    They also give great information on making Sublime Text 3 the default editor of ahk scripts. I did have to additionally do some registry modifications to make the filetype associations. I highly suggest that you use Sublime Text to edit your scripts by following the information provided in those gists. And then—if you are comfortable editing your registry—additionally setting the (Default) value for {filetype}\shell\edit\command to "[your\correct\filepath]\sublime_text.exe" "%1"

    • updated the method for associating file types.
      • old method: set (Default) value for each {filetype}\shell\edit\command to "{your\filepath\sublime_text.exe" "1%"
  • v1.3 | 2021.09.04
    • updated method of replacing notepad as default text editor to use sublime launcher
; centers current window without changing dimensions by pressing win + alt + up-arrow
#!Up::CenterActiveWindow()
CenterActiveWindow()
{
; Get the window handle from active window
winHandle := WinExist("A")
; Get the current monitor from the active window handle
VarSetCapacity(monitorInfo, 40)
NumPut(40, monitorInfo)
monitorHandle := DllCall("MonitorFromWindow", "uint", winHandle, "uint", 0x2)
DllCall("GetMonitorInfo", "uint", monitorHandle, "uint", &monitorInfo)
; calculating the active monitor dimensions and offsets (accounting for taskbar)
mXo := NumGet(monitorInfo, 20, "Int")
mYo := NumGet(monitorInfo, 24, "Int")
mXd := NumGet(monitorInfo, 28, "Int") - mXo
mYd := NumGet(monitorInfo, 32, "Int") - mYo
mXc := mXd / 2 + mXo
mYc := mYd / 2 + mYo
; get window dimensions and calculate left-top corner position to center the window on current monitor
WinGetPos,,, wXd, wYd, A
wXo := mXc - ( wXd / 2 )
wYo := mYc - ( wYd / 2 )
; move window to center of monitor
WinMove, A,, wXo, wYo
}
; figuring out what all the values in the monitorInfo array are
; MsgBox, % "four: mLeft - " . NumGet(monitorInfo, 4, "Int") . "`neight: mTop - " . NumGet(monitorInfo, 8, "Int") . "`ntwelve: mRight - " . NumGet(monitorInfo, 12, "Int") . "`nsixteen: mBottom - " . NumGet(monitorInfo, 16, "Int") . "`n`ntwenty: workLeft - " . NumGet(monitorInfo, 20, "Int") . "`ntwenty-four: workTop - " . NumGet(monitorInfo, 24, "Int") . "`ntwenty-eight: workRight - " . NumGet(monitorInfo, 28, "Int") . "`nthirty-two: workBottom - " . NumGet(monitorInfo, 32, "Int")
; MsgBox, % "one - " . NumGet(monitorInfo, 1, "Int") . "`ntwo - " . NumGet(monitorInfo, 2, "Int") . "`nthree - " . NumGet(monitorInfo, 3, "Int") . "`nfour: mLeft - " . NumGet(monitorInfo, 4, "Int") . "`nfive - " . NumGet(monitorInfo, 5, "Int") . "`nsix - " . NumGet(monitorInfo, 6, "Int") . "`nseven - " . NumGet(monitorInfo, 7, "Int") . "`neight: mTop - " . NumGet(monitorInfo, 8, "Int") . "`nnine - " . NumGet(monitorInfo, 9, "Int") . "`nten - " . NumGet(monitorInfo, 10, "Int") . "`neleven - " . NumGet(monitorInfo, 11, "Int") . "`ntwelve: mRight - " . NumGet(monitorInfo, 12, "Int") . " `nthirteen - " . NumGet(monitorInfo, 13, "Int") . "`nfourteen - " . NumGet(monitorInfo, 14, "Int") . "`nfifteen - " . NumGet(monitorInfo, 15, "Int") . "`nsixteen: mBottom - " . NumGet(monitorInfo, 16, "Int") . "`nseventeen - " . NumGet(monitorInfo, 17, "Int") . "`neighteen - " . NumGet(monitorInfo, 18, "Int") . "`nnineteen - " . NumGet(monitorInfo, 19, "Int") . "`ntwenty: workLeft - " . NumGet(monitorInfo, 20, "Int") . "`ntwenty-one - " . NumGet(monitorInfo, 21, "Int") . "`ntwenty-two - " . NumGet(monitorInfo, 22, "Int") . "`ntwenty-three - " . NumGet(monitorInfo, 23, "Int") . "`ntwenty-four: workTop - " . NumGet(monitorInfo, 24, "Int") . "`ntwenty-five - " . NumGet(monitorInfo, 25, "Int") . "`ntwenty-six - " . NumGet(monitorInfo, 26, "Int") . "`ntwenty-seven - " . NumGet(monitorInfo, 27, "Int") . "`ntwenty-eight: workRight - " . NumGet(monitorInfo, 28, "Int") . "`ntwenty-nine - " . NumGet(monitorInfo, 29, "Int") . "`nthirty - " . NumGet(monitorInfo, 30, "Int") . "`nthirty-one - " . NumGet(monitorInfo, 31, "Int") . "`nthirty-two: workBottom - " . NumGet(monitorInfo, 32, "Int") . "`nthirty-three - " . NumGet(monitorInfo, 33, "Int") . "`nthirty-four - " . NumGet(monitorInfo, 34, "Int") . "`nthirty-five - " . NumGet(monitorInfo, 35, "Int") . "`nthirty-six: isPrime - " . NumGet(monitorInfo, 36, "Int") . "`nthirty-seven - " . NumGet(monitorInfo, 37, "Int") . "`nthirty-eight - " . NumGet(monitorInfo, 38, "Int") . "`nthirty-nine - " . NumGet(monitorInfo, 39, "Int") . "`nforty - " . NumGet(monitorInfo, 40, "Int")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment