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