How to change where a window will apear

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

How to change where a window will apear

Post by Spitzi » 14 Apr 2024, 01:25

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?

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

Re: How to change where a window will apear

Post by mikeyww » 14 Apr 2024, 07:08

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.

User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

Re: How to change where a window will apear

Post by kunkel321 » 14 Apr 2024, 09:32

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.
ste(phen|ve) kunkel

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

Re: How to change where a window will apear

Post by mikeyww » 14 Apr 2024, 09:44

Interesting idea, could be tested for various windows and programs.

User avatar
kunkel321
Posts: 1062
Joined: 30 Nov 2015, 21:19

Re: How to change where a window will apear

Post by kunkel321 » 14 Apr 2024, 09:56

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.
ste(phen|ve) kunkel

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

Re: How to change where a window will apear

Post by mikeyww » 14 Apr 2024, 10:01

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.

iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: How to change where a window will apear

Post by iPhilip » 14 Apr 2024, 10:53

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
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Descolada
Posts: 1144
Joined: 23 Dec 2021, 02:30

Re: How to change where a window will apear

Post by Descolada » 14 Apr 2024, 11:28

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.

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: How to change where a window will apear

Post by Spitzi » 15 Apr 2024, 03:14

@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.

User avatar
xMaxrayx
Posts: 168
Joined: 06 Dec 2022, 02:56
Contact:

Re: How to change where a window will apear

Post by xMaxrayx » 15 Apr 2024, 04:02

why just use something that more native to windows? like with PowerShell?
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: How to change where a window will apear

Post by Spitzi » 15 Apr 2024, 05:11

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??

User avatar
Seven0528
Posts: 347
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How to change where a window will apear

Post by Seven0528 » 15 Apr 2024, 06:38

 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?
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

User avatar
xMaxrayx
Posts: 168
Joined: 06 Dec 2022, 02:56
Contact:

Re: How to change where a window will apear

Post by xMaxrayx » 15 Apr 2024, 11:27

@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
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: How to change where a window will apear

Post by Spitzi » 15 Apr 2024, 15:44

@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.

User avatar
Seven0528
Posts: 347
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: How to change where a window will apear

Post by Seven0528 » 15 Apr 2024, 17:31

 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.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

Noitalommi_2
Posts: 225
Joined: 16 Aug 2023, 10:58

Re: How to change where a window will apear

Post by Noitalommi_2 » 16 Apr 2024, 08:33

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.

Post Reply

Return to “Ask for Help (v2)”