Limiting a window's startup size Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AndreB
Posts: 7
Joined: 27 Sep 2016, 06:58

Limiting a window's startup size

22 Sep 2021, 03:41

Hi all

I recently bought a bigger screen to replace my 3 screen setup. The new one is 49", so there's enough space for everything :D I am using the awesome GridMove on Windows 10 and 11 which still works as a charm. As well, I have AHK running in the background, mostly for shortcuts.

Most applications behave perfectly fine. They seem to remember their last location on the screen and start up right there where I want them. One application, Java SWING based, somehow wants to be as big as possible. It takes the screen dimension and substracts some pixels on each side to get the initial size. This is no problem with other applications, but Swing has an issue with their form elements. They seem to still think that they're on a 49" screen and behave accordingly.

Is there a way to limit the size of a window when it shows up for the first time?

Any hint welcome!

Cheers

André
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size

22 Sep 2021, 07:03

Code: Select all

#SingleInstance Force
wTitle = ahk_exe notepad.exe
WinWait, %wTitle%
winResize(wTitle, 300, 200)

winResize(wTitle, maxW, maxH) {
 WinGetPos,,, w, h, %wTitle%
 WinMove, %wTitle%,,,, Min(w, maxW), Min(h, maxH)
 SoundBeep, 1700
}
RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Limiting a window's startup size

22 Sep 2021, 07:17

I could be wrong (and please correct me if I am), but shouldn't that be:

Code: Select all

wTitle := ahk_exe notepad.exe
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size

22 Sep 2021, 07:29

You have a choice of using = with a literal string, or := with an expression. You can try yours in the script to confirm that it is neither of these and will not work.
RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Limiting a window's startup size

22 Sep 2021, 08:45

Thank you for the clarification - and the reminder of why I and many others struggle so with the inconsistent syntax of ahk. Don't get me wrong, It's a great language from a functional POV and I have many scripts in production, but they were exasperating to make work.

In many other languages := is ALWAYS assignment and = or == is ALWAYS comparison. Anything enclosed in quotes is ALWAYS a literal string and unquoted references are ALWAYS considered a variable or expression.

I find myself constantly trying to figure out which form any specific command is expecting and when to use % around a var and when not. :crazy:

I would love to convert to V2, but need to wait until it is deemed stable before I start converting my production scripts.

Russ
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size

22 Sep 2021, 10:11

All good points. I will actually miss the legacy syntax with strings; I don't want to type extra quotation marks! I believe that you are not entirely correct, in that I believe that := would typically be used with expressions rather than literal strings, in other languages-- consistent with AHK in this syntax. If you were following syntax of other languages, you might have written the following.

Code: Select all

wTitle := "ahk_exe notepad.exe"
Nonetheless, I appreciate the challenge of keeping all of the syntax straight. On the bright side, it seems that v2's approach to this may improve clarity, decrease frequency of bugs, and decrease a certain amount of confusion.

I did find AHK's v1 approach initially confusing, but after a few weeks, the air cleared markedly. I tend to think as follows.

1. AHK commands use literal strings; functions use expressions.
2. If I have a literal string assignment, I can use =; otherwise, :=, with an expression, variable name, other function, quoted string, etc.
3. To use an expression as an AHK command parameter, precede with % and a space, to force the command to use the parameter as an expression instead of a literal string.
4. A variable name not flanked by % acts as an expression; a variable name flanked by % tends to act as a literal string, in practice.

I think that's all that I needed to figure out, to keep everything right. It's then a simple matter: as I write a new statement, I ask myself, "Is this a command?" If so, then I know that I can use a literal string, or a forced expression. If it's a function, then I use an expression. In assigning variables to strings, I have a choice. Functions always use (), so distinguishing commands from functions is easy.
RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Limiting a window's startup size

22 Sep 2021, 11:20

All true, and I have had to keep those rules in mind.

And then you come across something like the Run command where the first parameter is a path that should be enclosed in quotes if it has spaces and the second parameter must NOT have quotes - even if there are spaces in the path! These are the inconsistencies that add to my receding hairline and make me almost always look up every command whenever I go to use it. :wtf:

Russ
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size

22 Sep 2021, 11:53

I would argue here that there is more consistency than inconsistency: AHK parameters, as a general rule, are not quoted. AHK requires the quotes for the parameter's parameters (as I think of it) with spaces, not for its own parameters. It makes sense-- in handling the extra parameters, this would be similar to what you would do if running the parameter and its parameters as a command from the command line. Otherwise, the parsing of those parameters would be ambiguous.
AndreB
Posts: 7
Joined: 27 Sep 2016, 06:58

Re: Limiting a window's startup size

23 Sep 2021, 02:02

Wow, that was quick. Thanks gents, I'll give this a try. Sorry for my late reply, but I didn't get any e-mails from the forum; need to check my spam folder.
AndreB
Posts: 7
Joined: 27 Sep 2016, 06:58

Re: Limiting a window's startup size

23 Sep 2021, 02:24

Hi Mikey

Thanks for this. I added some exclude rule for the title. It comes in a bit too late, however. I Actually expected that, the Java elements can't handle the width of my screen :|

Would it be safe to but this inside a "while 1=1" loop, or is there a better way to keep it running and waiting for windows without eating up all the resources?

(maybe put WinWait and the winResize call into a function, which will be called by winResize ... recursion nightmare?)

Cheers

André


mikeyww wrote:
22 Sep 2021, 07:03

Code: Select all

#SingleInstance Force
wTitle = ahk_exe notepad.exe
WinWait, %wTitle%
winResize(wTitle, 300, 200)

winResize(wTitle, maxW, maxH) {
 WinGetPos,,, w, h, %wTitle%
 WinMove, %wTitle%,,,, Min(w, maxW), Min(h, maxH)
 SoundBeep, 1700
}
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size

23 Sep 2021, 06:09

Another method, for multiple windows:

Code: Select all

#SingleInstance Force
Global resizeProc := "notepad", maxW := 500, maxH := 300
Gui, +LastFound
DllCall("RegisterShellHookWindow", "UInt", WinExist())
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "winResize")

winResize(wParam, lParam) {
 If (wParam != WINDOWCREATED := 1)
  Return
 WinGet, pname, ProcessName, % wTitle := "ahk_id " lParam
 SplitPath, pname,,,, fnBare
 If fnBare not in %resizeProc%
  Return
 WinGetPos,,, w, h, %wTitle%
 WinMove, %wTitle%,,,, Min(w, maxW), Min(h, maxH)
 SoundBeep, 1700
}
For just a single window:

Code: Select all

#SingleInstance Force
Loop {
 WinWait, ahk_exe notepad.exe
 WinGetPos,,, w, h
 WinMove,,,,, Min(w, MAXWIDTH := 500), Min(h, MAXHEIGHT := 250)
 SoundBeep, 1700
 WinWaitClose
 SoundBeep, 1000
}
AndreB
Posts: 7
Joined: 27 Sep 2016, 06:58

Re: Limiting a window's startup size

23 Sep 2021, 06:48

:shock:

The alternative method looks, ehm, different. AHK, the language of many syntaxes? I have to admit that I don't really get what it's doing.

Unfortunately I can't go for the process name, it will always be javaw.exe. My WindowSpy shows this:

Code: Select all

XCarrier DEV@XC_003D04-PRI@abonhote[441627547]
ahk_class SunAwtFrame
ahk_exe javaw.exe
ahk_pid 2072
So if the title contains XCarrier the script should jump in.
AndreB
Posts: 7
Joined: 27 Sep 2016, 06:58

Re: Limiting a window's startup size

23 Sep 2021, 06:53

Ah yes, forgot to mention: I have many of these windows open, not just one.

Thanks for your help, I really appreciate that :)
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size  Topic is solved

23 Sep 2021, 06:58

Code: Select all

#SingleInstance Force
Gui, +LastFound
DllCall("RegisterShellHookWindow", "UInt", WinExist())
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "winResize")

winResize(wParam, lParam) {
 If (wParam != WINDOWCREATED := 1)
  Return
 WinGet, pname, ProcessName, % wTitle := "ahk_id " lParam
 WinGetTitle, winTitle, %wTitle%
 If !Instr(winTitle, "XCarrier") || pname != "javaw.exe"
  Return
 WinGetPos,,, w, h, %wTitle%
 WinMove, %wTitle%,,,, Min(w, MAXWIDTH := 500), Min(h, MAXHEIGHT := 300)
 SoundBeep, 1700
}
More on shell hook if needed: https://autohotkey.com/board/topic/32628-tool-shellhook-messages/

My crude understanding of what happens: when a window event occurs, a message is sent to a hidden GUI, and the winResize function is run. The system will pass the window event number and the window HWND to the function. That function checks to see if the message indicates that a window was created. If a window was created, the function gets the process name and window title. If those things match the needed criteria, then the window size is adjusted.
AndreB
Posts: 7
Joined: 27 Sep 2016, 06:58

Re: Limiting a window's startup size

23 Sep 2021, 07:53

Awesome, thanks a ton!
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size

23 Sep 2021, 07:58

Glad it works. One thing that I often do is re-read my scripts. I was thinking about whether this script would work without a GUI. I found that the following worked (testing with Notepad).

Code: Select all

#SingleInstance Force
DllCall("RegisterShellHookWindow", "UInt", A_ScriptHwnd)
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "winResize")

winResize(wParam, lParam) {
 If (wParam != WINDOWCREATED := 1)
  Return
 WinGet, pname, ProcessName, % wTitle := "ahk_id " lParam
 WinGetTitle, winTitle, %wTitle%
 If !Instr(winTitle, "Untitled") || pname != "notepad.exe"
  Return
 WinGetPos,,, w, h, %wTitle%
 WinMove, %wTitle%,,,, Min(w, MAXWIDTH := 500), Min(h, MAXHEIGHT := 300)
 SoundBeep, 1700
}
AndreB
Posts: 7
Joined: 27 Sep 2016, 06:58

Re: Limiting a window's startup size

23 Sep 2021, 08:09

I guess I'd have a lot to learn ... whats wrong with the GUI line?
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Limiting a window's startup size

23 Sep 2021, 08:10

Nothing at all, though the GUI is not needed in this script. The link that I posted has a script that uses a visible GUI to display all of the window messages. It's one way to learn about window messages by example. One can run that script and watch all of the various window messages being generated.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 194 guests