How to check if a window is centered before centering?

Get help with using AutoHotkey 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] [Expand] [Download] GeSHi © Codebox Plus



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: 1297
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] [Download] GeSHi © Codebox Plus

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: 26
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] [Expand] [Download] GeSHi © Codebox Plus


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] [Expand] [Download] GeSHi © Codebox Plus


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] [Expand] [Download] GeSHi © Codebox Plus



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] [Expand] [Download] GeSHi © Codebox Plus


Return to “Ask For Help”

Who is online

Users browsing this forum: ethangolson and 24 guests