A simple script to center a active window either right, left or center

Post your working scripts, libraries and tools for AHK v1.1 and older
BruceO
Posts: 12
Joined: 06 Sep 2019, 11:15

A simple script to center a active window either right, left or center

Post by BruceO » 21 Sep 2019, 05:50

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.

SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance force ;Skips the dialog box and replaces the old instance automatically, which is similar in effect to the Reload command.

SysGet, VirtualScreenWidth, 78
SysGet, VirtualScreenHeight, 79
rwidth := VirtualScreenWidth-5
rhight := VirtualScreenHeight-5
lWidth := 5
WinTitle = A

F10::
; The following function centers the specified window on the right side of the screen:
WinGetPos,,, Width, Height, %WinTitle%
WinMove , %WinTitle%,, lWidth,(rhight/2)-(Height/2)
Return
F11::
; The following function centers the specified window on the center of the screen:
WinGetPos,,, Width, Height, %WinTitle%
WinMove , %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
Return
F12::
; The following function centers the specified window on the left side of the screen:
WinGetPos,,, Width, Height, %WinTitle%
WinMove , %WinTitle%,, rwidth-width,(rhight/2)-(Height/2)
Return

gregster
Posts: 9014
Joined: 30 Sep 2013, 06:48

Re: A simple script to center a active window either right, left or center

Post by gregster » 21 Sep 2019, 05:56

Hi Bruce, the idea of the [code][/code] tags is actually to put the code between them, so that the codebox does the syntax highlighting for you ;) (among other things). Like this :

Code: Select all

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance force ;Skips the dialog box and replaces the old instance automatically, which is similar in effect to the Reload command.

SysGet, VirtualScreenWidth, 78
SysGet, VirtualScreenHeight, 79
rwidth := VirtualScreenWidth-5
rhight := VirtualScreenHeight-5
lWidth := 5
WinTitle = A

F10::
; The following function centers the specified window on the right side of the screen:
WinGetPos,,, Width, Height, %WinTitle%
WinMove , %WinTitle%,, lWidth,(rhight/2)-(Height/2)
Return
F11::
; The following function centers the specified window on the center of the screen:	
WinGetPos,,, Width, Height, %WinTitle%
WinMove , %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
Return
F12::
; The following function centers the specified window on the left side of the screen:
WinGetPos,,, Width, Height, %WinTitle%
WinMove , %WinTitle%,, rwidth-width,(rhight/2)-(Height/2)
Return

User avatar
DataLife
Posts: 447
Joined: 29 Sep 2013, 19:52

Re: A simple script to center a active window either right, left or center

Post by DataLife » 21 Sep 2019, 09:40

@Bruce
thanks for posting this script. With a little adjustments I have it working nicely with my screen.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.

User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Re: A simple script to center a active window either right, left or center

Post by jackdunning » 03 Oct 2019, 13:35

I wrote a similar script I call WindowMove.ahk. It offers a dropdown list of options for different screen locations. You can find a short description on my Free Scripts page.

Image

I also offer an AutoHotkey Version 2.0 example script for this app.

BruceO
Posts: 12
Joined: 06 Sep 2019, 11:15

Updates Center Windows script for Autohotkey V2

Post by BruceO » 05 Jun 2023, 11:11

Code: Select all

; REMOVED: #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode("Input") ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir(A_ScriptDir) ; Ensures a consistent starting directory.
#SingleInstance force ;Skips the dialog box and replaces the old instance automatically, which is similar in effect to the Reload command.

VirtualScreenWidth := SysGet(78)
VirtualScreenHeight := SysGet(79)
rwidth := VirtualScreenWidth-5
rhight := VirtualScreenHeight-5
lWidth := 5
WinTitle := "A"

F10::
; The following function centers the specified window on the right side of the screen:
{ ; V1toV2: Added bracket
WinGetPos(, , &Width, &Height, WinTitle)
WinMove(lWidth, (rhight/2)-(Height/2), , , WinTitle)
Return
} ; V1toV2: Added Bracket before hotkey or Hotstring

F11::
; The following function centers the specified window on the center of the screen:
{ ; V1toV2: Added bracket
WinGetPos(, , &Width, &Height, WinTitle)
WinMove((A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2), , , WinTitle)
Return
} ; V1toV2: Added Bracket before hotkey or Hotstring

F12::
; The following function centers the specified window on the left side of the screen:
{ ; V1toV2: Added bracket
WinGetPos(, , &Width, &Height, WinTitle)
WinMove(rwidth-width, (rhight/2)-(Height/2), , , WinTitle)
Return
} ; V1toV2: Added bracket in the end

Post Reply

Return to “Scripts and Functions (v1)”