How to check if a window is centered before centering?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

How to check if a window is centered before centering?

17 Aug 2018, 00:42

So using the example code on the WinMove page, this is a function I've come up with:

Code: Select all

CenterWindow(WinTitle)
{
    WinGetPos, X, Y, Width, Height, %WinTitle%
	if (X = (A_ScreenWidth/2)-(Width/2) and Y = (A_ScreenHeight/2)-(Height/2) and Width = 1616 and Height = 939)
	{
		return
	} else {
		WinMove, ahk_exe throttle.exe, , 0, 0, 1616, 939
		Sleep, 100
		WinMove, %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
	}
}
What I want is for it to check and see if the window is centered (and resized correctly), before actually doing anything. If it is already in the center of the screen, and if it's resized to 1616x939, it should do nothing. If it's not, it should center it. However, this doesn't seem to work, as the window hops around the screen (when it moves it to 0, 0 and back to the center of the screen again). What have I done wrong?
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to check if a window is centered before centering?

17 Aug 2018, 01:32

Hallo,
try:

Code: Select all

CenterWindow(WinTitle)
{
	WinGetPos, X, Y, Width, Height, %WinTitle%
	if (X = (A_ScreenWidth//2)-(Width//2) and Y = (A_ScreenHeight//2)-(Height//2) and Width = 1616 and Height = 939)
		return
	else
		WinMove, %WinTitle%,, (A_ScreenWidth//2)-(1616//2), (A_ScreenHeight//2)-(939//2), 1616, 939
}
pro100andrik94
Posts: 27
Joined: 08 Aug 2018, 07:27

Re: How to check if a window is centered before centering?

17 Aug 2018, 03:12

It's because you'r code is little wrong.

Code: Select all

CenterWindow(WinTitle)
{
    WinGetPos,xx,yy, Width, Height, %WinTitle%
    x1 := xx + Width/2
    y1 := yy + Height/2
    loop 2
    { 
    y1 := yy + Height/2
        loop 2
        { 
            if ((A_ScreenWidth/2 == x1) && (A_ScreenHeight/2 == y1)) 
            {
            msgbox all
            goto end
            }
            else
            y1 := y1 + 0.5
        }
    
    x1 := x1 + 0.5
    } 
    WinMove, %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
    end:
return
And if windows size is not even number you will have 0.5 and it's not match with center of the screen at all. But I solved this problem. Look loops.
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: How to check if a window is centered before centering?

17 Aug 2018, 17:15

pro100andrik94 wrote:It's because you'r code is little wrong.

Code: Select all

CenterWindow(WinTitle)
{
    WinGetPos,xx,yy, Width, Height, %WinTitle%
    x1 := xx + Width/2
    y1 := yy + Height/2
    loop 2
    { 
    y1 := yy + Height/2
        loop 2
        { 
            if ((A_ScreenWidth/2 == x1) && (A_ScreenHeight/2 == y1)) 
            {
            msgbox all
            goto end
            }
            else
            y1 := y1 + 0.5
        }
    
    x1 := x1 + 0.5
    } 
    WinMove, %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
    end:
return
And if windows size is not even number you will have 0.5 and it's not match with center of the screen at all. But I solved this problem. Look loops.
This seems to work almost exactly as I want. With a couple tweaks (primarily as it relates to the window's height and size), it seems to work perfectly. Thanks! (ex, if you don't resize the window before centering it, sometimes it will be centered and then get resized, causing it to then be off-centered)

That said, I had some trouble following along - could you explain what your code is doing at each step and how it works? Aside from the multiple variables that you're using and the loops, I also don't understand why you use goto end instead of just a return at that point?

Here's my version of the code:

Code: Select all

CenterWindow(WinTitle)
{
    WinGetPos,xx,yy, Width, Height, %WinTitle%
    x1 := xx + Width/2
    y1 := yy + Height/2
    loop 2
    { 
    y1 := yy + Height/2
        loop 2
        { 
            if ((A_ScreenWidth/2 == x1) && (A_ScreenHeight/2 == y1) && Width = 1616 && Height = 939) 
            {
            msgbox all
            return
            }
            else
            y1 := y1 + 0.5
        }
    x1 := x1 + 0.5
    } 
	WinMove, ahk_exe throttle.exe, , 0, 0, 1616, 939
	WinGetPos,,, Width, Height, %WinTitle%
	Sleep, 100
    WinMove, %WinTitle%,, (A_ScreenWidth/2)-(Width/2), (A_ScreenHeight/2)-(Height/2)
}
Edit: Here's the full version of my script. It seems to be working well, except where I've used %SetWidth% and %SetHeight% -- its seems like AHK isn't reading the variable's value correctly for some reason (which I set at the top of the script), because if I change these all to numbers (1616 and 939), it works fine...

Code: Select all

#NoTrayIcon
#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.

SetWidth = 1616
SetHeight = 939

Run, ".\full throttle remastered.bat"
Process, Wait, throttle.exe, 120
Process, Exist, throttle.exe
Throttle = %ErrorLevel%
if Throttle != 0
{
	Sleep, 2000
	CenterWindow("ahk_exe throttle.exe")
}
else
{
	MsgBox "Full Throttle Remastered could not be started"
}
return

; The following function centers the specified window on the screen if not already centered:
CenterWindow(WinTitle)
{
    WinGetPos,xx,yy, winx, winy, %WinTitle%
    x1 := xx + winx/2
    y1 := yy + winy/2
    loop 2
    { 
    y1 := yy + winy/2
        loop 2
        { 
            if ((A_ScreenWidth/2 = x1) && (A_ScreenHeight/2 = y1) && (winx = %SetWidth%) && (winy = %SetHeight%)) 
            {
                msgbox got em
            return
            }
            else
            y1 := y1 + 0.5
        }
    x1 := x1 + 0.5
    } 
	WinMove, ahk_exe throttle.exe,, 0, 0, %SetWidth%, %SetHeight%
	WinGetPos,,, winx, winy, %WinTitle%
	Sleep, 100
    WinMove, %WinTitle%,, (A_ScreenWidth/2)-(winx/2), (A_ScreenHeight/2)-(winy/2)
}
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Lamron750 and 245 guests