• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Tsukasa no Hibi

Cloudy Sky. Occasional Rain.

You are here: Home / Blog / AutoHotkey: Center a Window on the Program’s display

AutoHotkey: Center a Window on the Program’s display

2024-12-29 by tsukasa Leave a Comment

This holiday was all about window management and AutoHotkey for me. Despite the name sounding a lot like AutoIt and being based off of it, AHK is a pretty different beast.

Today’s small hack session is all about centering the active window on the screen the window is on. For that to work, we first have to determine the window’s position and width, then calculate the window X and Y center point, then loop through the displays to get their work area properties and determine which of the displays the window is most likely on.

After that we can calculate the new X and Y positions based on the window dimensions and the determined display work area.

    activeWindow := WinExist("A")
    if !activeWindow
        return

    ; Retrieve the active window's current position and size
    WinGetPos(&winX, &winY, &winWidth, &winHeight, activeWindow)

    ; Calculate the center of the window
    windowCenterX := Round(winX + winWidth / 2, 0)
    windowCenterY := Round(winY + winHeight / 2, 0)

    ; Get number of monitors - but only ones used for the desktop!
    monitorCount := SysGet(80)

    ; Set initial value to check if we found the monitor the window is on...
    monitorIndex := -1

    ; Loop through each monitor to find which one the window is on...
    ; Do this by checking on which monitor the center of the window is, otherwise we get false positives.
    Loop monitorCount {
        MonitorGetWorkArea(A_Index, &workAreaX, &workAreaY, &workAreaWidth, &workAreaHeight)

        if (windowCenterX >= workAreaX and windowCenterX <= workAreaWidth
            and
            windowCenterY >= workAreaY and windowCenterY <= workAreaHeight)
        {
            monitorIndex := A_Index
            break
        }
    }

    ; If still initial value, exit...
    if monitorIndex = -1
        return

    ; Get the dimensions of the determined monitor's work area
    MonitorGetWorkArea(monitorIndex, &workAreaX, &workAreaY, &workAreaWidth, &workAreaHeight)

    ; Calculate new position to center the window
    newX := Round(workAreaX + ((workAreaWidth - workAreaX) - winWidth) / 2, 0)
    newY := Round(workAreaY + ((workAreaHeight - workAreaY) - winHeight) / 2, 0)

    ; Move the window to the new position
    WinMove(newX, newY, , , activeWindow)

This works nicely on multi-monitor setups.

Filed Under: Blog Tagged With: AutoHotkey, Programming, Scripting, Windows

Reader Interactions

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Tags

.net AutoHotkey Bitcasa Blog C# Docker Fonts Fun Google Chrome Hardware How To Life Linux Mono Mplayer Music My NAS nVidia OBS OpenEdge OpenSSH Palaver Pangya! Portable App PowerShell Programming Quote RaiDrive Reminder Scripting Software Streaming Technology Tips twitter Video Virtualization VMware Web Windows Wine WordPress Wuala YouTube ZNC

Cool stuff

  • AdiIRC
  • Affinity Designer
  • Affinity Photo
  • AquaSnap
  • Arch Linux
  • Archive Team
  • ConEmu
  • Debian
  • Directory Opus
  • Far Manager
  • FileLocator Pro
  • Fluent Search
  • foobar2000
  • Forte Agent
  • IRCCloud
  • ISBoxer
  • Jetbrains Rider
  • Newsblur
  • OBS Studio
  • Obsidian.md
  • RaiDrive
  • Sublime
  • SyncBackPro
  • The Semware Editor
  • True Launch Bar
  • Vivaldi
  • Wavebox
  • WinHex
  • WinRAR
  • WinSCP
  • XYplorer
  • ZNC
  • Zoom Player Max

Semantic Web

  • Mastodon
  • Tsukasa no Hibi
  • Tsukasa no Hibi Sitemap

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Footer

About

Going since 2004, Tsukasa no Hibi is my personal blog about technology, media and sometimes society.

More about Tsukasa no Hibi

WordPress · Log in