Know when a window opens

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Know when a window opens

Post by Archimede » 20 Nov 2022, 11:14

Hallo.
I need to know when a window opens on the desktop, without testing it continuously... is it possible?
I think is possible to set or detect a windows message, by Dll call or similar... do you know how is possible?
It it possible to detect when an opened window is modified?
Thank you very much.

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: Know when a window opens

Post by boiler » 20 Nov 2022, 11:34

Archimede wrote: I need to know when a window opens on the desktop, without testing it continuously... is it possible?
See WinWait.

Archimede wrote: It it possible to detect when an opened window is modified?
Not without testing it continuously, and that testing would most likely entail capturing an image of it and then continuously checking to see if that image is still there — if not, it has changed.

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Know when a window opens

Post by Archimede » 20 Nov 2022, 11:50

:-(
I'm sorry no explain well...
WinWait... stops the program until a window exists...
I need to detect any opening window while the program works...

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: Know when a window opens

Post by boiler » 20 Nov 2022, 11:56

Check out this library. It might allow you to check for changes without continuously checking as well.

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

Re: Know when a window opens

Post by mikeyww » 20 Nov 2022, 12:22

Or:

Code: Select all

DllCall("RegisterShellHookWindow", "UInt", A_ScriptHwnd)
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "done")
SetKeyDelay, 100
Loop
 Send x

F3::Run, notepad

done(wParam, lParam) {
 If (wParam = WINDOWCREATED := 1) {
  WinGet, pname, ProcessName, ahk_id %lParam%
  If (pname = "notepad.exe") {
   MsgBox, 64, Done, Done!
   ExitApp
  }
 }
}

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Know when a window opens

Post by Archimede » 20 Nov 2022, 12:37

Thank you very much.
Very interesting.
For you, about the speed, what is better:
- that libray solution
- another solution based on another independent program with WinWait.. (or similar) and PostMessage... to the working program with OnMessage... ?
The PostMessage solution is quick or not?

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

Re: Know when a window opens

Post by mikeyww » 20 Nov 2022, 12:45

You can test your idea by computing the elapsed times via:

https://www.autohotkey.com/docs/Variables.htm#TickCount

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Know when a window opens

Post by Archimede » 22 Nov 2022, 09:20

I don't understand what is the syntax meaning of this:
(wParam = WINDOWCREATED := 1)

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

Re: Know when a window opens

Post by mikeyww » 22 Nov 2022, 09:27

WINDOWCREATED is assigned to the value of 1. A comparison then occurs between this value and the value of wParam. The result is true (1) if the values are equal; the result is otherwise zero.

WINDOWCREATED is assigned only as an aid to the reader of the script. It helps the reader to understand that when a window is created, the value of wParam is 1. Otherwise, the value of wParam is not 1.

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Know when a window opens

Post by Archimede » 24 Nov 2022, 11:30

It seem very interesting.
I am testing it.
Please can you explain the meaning of the 2 dll calls?
I am searching it on the Microsoft site, but that explanations are very poor...
Thank you very much.

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

Re: Know when a window opens

Post by mikeyww » 24 Nov 2022, 11:44

This page has more details: https://autohotkey.com/board/topic/32628-tool-shellhook-messages/

My knowledge in this area is crude at best. I believe that the idea is that the Windows shell hook is registered to the script (window) itself. When the hook is triggered by a window action (e.g., window created, activated, redrawn, or destroyed), the script receives a message and then calls the "done" function. The page cited above shows other kinds of windowing codes that can be processed.

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Know when a window opens

Post by Archimede » 24 Nov 2022, 12:45

The WINDOWCREATED message is very useful.
Can you show me all other possible values?
I need to know when a window is moved or modified:is it possible?
Thank you very much.

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Know when a window opens

Post by Xtra » 24 Nov 2022, 13:08


Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Know when a window opens

Post by Archimede » 24 Nov 2022, 18:33

xtra:
Thanks, it seem very interesting but I am not able to make an object program.
:(

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

Re: Know when a window opens

Post by mikeyww » 24 Nov 2022, 20:00

Code: Select all

#SingleInstance Force
wTitle = ahk_exe notepad.exe
Process, Priority,, B
Gui, +AlwaysOnTop
Gui, Font, s20
Gui, Color, FFFF9E
Gui, Add, Text, w300 Center vtext
Loop {
 WinWait, %wTitle%
 SetTimer, Check, 250
 show("Open")
 SoundBeep, 1500
 WinWaitClose
 SetTimer, Check, Off
 show("Closed")
 SoundBeep, 1000
 last := current := ""
}
Check:
last := current
WinGetPos, x, y, w, h, %wTitle%
current = %x%,%y%,%w%,%h%
(last) && (current != last) && show("Moved")
Return

show(text) {
 GuiControl,, text, %text%
 Sleep, 150
 Gui, Show, NoActivate, Status
 SetTimer, Hide, -900
 Return
 Hide:
 Gui, Hide
 Return
}
The library that boiler mentioned might be a better fit for what you need.

Archimede
Posts: 503
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: Know when a window opens

Post by Archimede » 25 Nov 2022, 03:41

Sorry...
What is "boiler"?
Your idea is interesting, but is very similar to my idea...
I need to detect the resizing or moving of *any* window, and execute a code only in that cases...
I am searching a better idea to no load the computer ( the test every 250 mS ) when no necessary.

Is it possible to use this API call
GetGUIThreadInfo
It seem useful, but very complicated...

I found this info:
"There is no event for capturing the window move, so your option is to query the position frequently."
Is it right?

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

Re: Know when a window opens

Post by mikeyww » 25 Nov 2022, 05:41

I don't know a lot about the boiler, but it seems to be a thing that writes a lot of posts in this forum and answers people's questions, including yours! :) 8-) viewtopic.php?p=492204#p492204

Code: Select all

#Include D:\utils\WinEvents.ahk
Process, Priority,, B
Gui, +AlwaysOnTop
Gui, Font, s20
Gui, Color, FFFF9E
Gui, Add, Text, w300 Center vtext
HookEvent("adjusting", [EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND])

adjusting(hHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) {
 If (event = EVENT_SYSTEM_MOVESIZESTART) {
  WinGet, pname, ProcessName, ahk_id %hwnd%
  GuiControl,, text, % SubStr(pname, 1, -4)
  Gui, Show, x100 y100 NoActivate, Adjusting window
 } Else Gui, Hide
}

User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: Know when a window opens

Post by boiler » 25 Nov 2022, 07:10

mikeyww wrote:
25 Nov 2022, 05:41
I don't know a lot about the boiler, but it seems to be a thing that writes a lot of posts in this forum and answers people's questions, including yours! :) 8-)
:D

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

Re: Know when a window opens

Post by mikeyww » 25 Nov 2022, 07:14

One edit here that can (frequently) capture the maximize event.

Code: Select all

#Include D:\utils\WinEvents.ahk
Process, Priority,, B
Gui, +AlwaysOnTop
Gui, Font, s20
Gui, Color, FFFF9E
Gui, Add, Text, w300 Center vtext
HookEvent("adjusting", [EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND])

~LButton::
CoordMode, Mouse
MouseGetPos, x, y, hWnd
SendMessage, WM_NCHITTEST := 0x84,, (x & 0xFFFF) | (y & 0xFFFF) << 16,, ahk_id %hWnd%
If ErrorLevel in 8,9
 WinGet, pname, ProcessName, ahk_id %hWnd%
Return

#If pname
~LButton Up::
MsgBox, 64, Changed, % SubStr(pname, 1, -4)
pname =
Return
#If

adjusting(hHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) {
 If (event = EVENT_SYSTEM_MOVESIZESTART) {
  WinGet, pname, ProcessName, ahk_id %hwnd%
  GuiControl,, text, % SubStr(pname, 1, -4)
  Gui, Show, x100 y100 NoActivate, Adjusting window
 } Else Gui, Hide
}

MancioDellaVega
Posts: 83
Joined: 16 May 2020, 12:27
Location: Italy

Re: Know when a window opens

Post by MancioDellaVega » 26 Nov 2022, 07:51

@mikeyww
Is it possible use that technique to intercept the opening of a new tab in the chrome browser
and know what is the title of that tab?
Courses on AutoHotkey

Post Reply

Return to “Ask for Help (v1)”