Page 1 of 1

How to change where a window will apear

Posted: 14 Apr 2024, 01:25
by Spitzi
Hi there.

I am trying to automate a software, and I would like to change the location where windows of that software appear - before they appear.

I have the following code, but it only moves the window AFTER it has appeared, which is annoying to the users, because the window is visible for a split second in the wrong place:

Code: Select all

		Send("^b")								; opens the window of the app
		hwnd := WinWait("WinTitle", , 50)
		if hwnd {
			WinMove(0,0, 300, 300, hwnd)
		}
		WinClose(hwnd)
Is it possible to preselect where the window should appear?

Re: How to change where a window will apear

Posted: 14 Apr 2024, 07:08
by mikeyww
Hello,

I am not aware of a way to do that with a script because it would probably entail altering the target program, which does what it pleases! There could be workarounds such as moving the main window to a different location, but I think that this would not usually work.

Re: How to change where a window will apear

Posted: 14 Apr 2024, 09:32
by kunkel321
Here are some ideas for a work-around...
I got the idea from here
viewtopic.php?f=83&t=119688

I'm not sure I've used the commands/functions correctly though... Firstly, I experimented with just the SetWinDelay, using it with Notepad. I didn't see a big difference in how long the window appeared prior to moving though.

Then I added the transparency. As you might guess, the window still "appears" in the original spot... But it's not annoying because you can't see it. It seemed to work perfectly. But then I experimented with the WinSetExStyle. That sort of worked, but then the transparency stopped working for some reason. IDK why.

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+

hwnd := WinWait("Untitled - Notepad", , 50)
if hwnd {
	;WinSetExStyle(+32,hwnd) ; Make "clickthrough"
	WinSetTransparent(50, hwnd) ; Make opacity = 0
	sleep 3000 ; <--- remove later
	SetWinDelay(-1) ; Sets time between moves. -1 = no time
	WinMove(0,0, 300, 300, hwnd)
	;WinSetExStyle(-32,hwnd) ; Turn off "clickthrough"
	WinSetTransparent(255, hwnd) ; Make opacity = full
}
EDIT:... Actually... After experimenting more: When I thought it was working, I must've just forgotten to put the notepad window back in the start location.... I see now that there is an unavoidable delay between the window showing and whatever you do (move/transparency/click-through).
Sorry.

Re: How to change where a window will apear

Posted: 14 Apr 2024, 09:44
by mikeyww
Interesting idea, could be tested for various windows and programs.

Re: How to change where a window will apear

Posted: 14 Apr 2024, 09:56
by kunkel321
mikeyww wrote:
14 Apr 2024, 09:44
Interesting idea, could be tested for various windows and programs.
Another thought is to use the script to actually run the target app, but force it to start minimized. I'm not sure if that would help, though... Does Windows actually "create" the necessary application window when it starts the app minimized(?) And if so, can you go directly from "minimized" to "WinMove" ? -- IDK.

Re: How to change where a window will apear

Posted: 14 Apr 2024, 10:01
by mikeyww
Change the location where windows of that software appear - before they appear.
It seems that the issue is that the software creates multiple windows, and these might be created at various times, so this might not be entirely controllable by the user or the script.

Re: How to change where a window will apear

Posted: 14 Apr 2024, 10:53
by iPhilip
The approach below doesn't solve the problem in the original post but it's often sufficient for my purposes:

Code: Select all

#Requires AutoHotkey v2.0
Persistent

WinClass := 'Notepad'
WinX := 0
WinY := 0

DllCall('RegisterShellHookWindow', 'Ptr', A_ScriptHwnd)
MsgNum := DllCall('RegisterWindowMessage', 'WStr', 'SHELLHOOK')
OnMessage(MsgNum, ShellMessage)

ShellMessage(wParam, lParam, *) {
   static HSHELL_WINDOWCREATED := 1
   if wParam = HSHELL_WINDOWCREATED && WinGetClass(lParam) = WinClass
      WinMove WinX, WinY, , , lParam
}

Re: How to change where a window will apear

Posted: 14 Apr 2024, 11:28
by Descolada
You could try detecting EVENT_OBJECT_CREATE using SetWinEventHook to detect window creation and try to move it before it is actually shown (which triggers EVENT_OBJECT_SHOW). Here is an example of that using my WinEvent.ahk library:

Code: Select all

#Requires AutoHotkey v2
#include WinEvent.ahk

WinEvent.Create(OnNotepadCreate, "ahk_exe notepad.exe")
Persistent()

OnNotepadCreate(eventObj, hWnd, *) {
    SetWinDelay -1
    WinMove(0, 0,,, hWnd)
    ToolTip "Notepad moved!"
    SetTimer ToolTip, -3000
}
This method has the downside of not always being able to categorize windows by their titles, as window titles might be assigned just before showing and not on creation. You'd need to experiment with your target program to see whether that is a concern or not.

If you want to move the window before it is created, you'd need to use SetWindowsHookEx and change the position before window creation, which will be much more difficult.

Re: How to change where a window will apear

Posted: 15 Apr 2024, 03:14
by Spitzi
@Descolada , @iPhilip , @kunkel321 , @mikeyww

Thanks for your input. I tried your suggestions, but still a flicker of the appearing window in the original spot remains... maybe because the window does not contain much and is ready very quickly...

I really appreciate your suggestions though. Thanks alot.

Re: How to change where a window will apear

Posted: 15 Apr 2024, 04:02
by xMaxrayx
why just use something that more native to windows? like with PowerShell?

Re: How to change where a window will apear

Posted: 15 Apr 2024, 05:11
by Spitzi
Hi @xMaxrayx.

I use Autohotkey because it is opensource and has this incredible forum and discord channels. But by far the most important reason is that it can be used standalone/portable on any computer where you do not have administator privileges and you can't install any software without going through a tedious and often unsuccessful process with the IT-department.

But would my problem even be solvable using Powershell??

Re: How to change where a window will apear

Posted: 15 Apr 2024, 06:38
by Seven0528
 When I used to use Photoshop or similar software, there were times when I wanted to move the new window to a different position.
Even though I would immediately move the window as soon as it appeared, the slight flickering was really annoying.
(Blinking hundreds of times a day can drive a person crazy.)

Although it wasn't a fundamental solution to the problem, at the time, I would capture the entire screen with something like GDI+, set it to AlwaysOnTop, then manipulate the window as desired before removing the overlay.
It wasn't a fundamental solution like adjusting the position before the window appeared, and there was a slight delay since I had to capture the entire monitor, but regardless, I could draw comfortably.
(0.3-second of patience was better than a 0.1-second flashbang.)

 @Spitzi
If you consider this approach, I can help.
Would you like that?

Re: How to change where a window will apear

Posted: 15 Apr 2024, 11:27
by xMaxrayx
@Spitzi
I see yeah you can do it in PowerShell but sadly powershell scripts are not allowed by default.

if you really want it in AHK only solution is using using API/DLL , you can use this
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-movewindow
but im not good with dll, so you find someone can help you with that.
you can get inspired by this
https://stackoverflow.com/questions/41366817/open-file-explorer-at-coordinates

Re: How to change where a window will apear

Posted: 15 Apr 2024, 15:44
by Spitzi
@Seven0528. Thanks for the input, but a workaround like this is not doable in my case. When the window pops up, the user is usually doing stuff in another and might be dragging stuff around. I can't overlay something...
It would be nice, actually, if i could control where the window appears AND prevent it's activation.

@xMaxrayx , thanks for the links. API/DLL is not my strongsuit either. I think the links that you provided move the window AFTER it has appeared - that's easy.

Re: How to change where a window will apear

Posted: 15 Apr 2024, 17:31
by Seven0528
 I understand your point.
However, applying various attributes to overlays can prevent them from interfering with the user's actions.
For example, allowing clicks to pass through or making them non-activate.
You could also give them transparency to make them appear slightly overlapped.
Moreover, since overlays are displayed for a very short time, in reality, the user might only perceive it as a momentary lag on the computer, making the monitor appear frozen.
(The user's ongoing activity itself is not disrupted).
Just in case, I'll leave my opinion here.

Re: How to change where a window will apear

Posted: 16 Apr 2024, 08:33
by Noitalommi_2
Hi.

@Spitzi
If the program saves the last known window position somewhere when closing, then you could change the coordinates directly, all you have to do is find the save location, could be file or a registry entry.
You can find out something like this using the Process Monitor. You should look out for a write or create operation before the thread exit. I did this once for the SciTE Editor, it was simple but not every program is the same so i don't know if it will work on your end but maybe it's worth the try.