Hi, at first, I was searching for code to detect launch of a window & got this from a friend - http://p.ahkscript.org/?p=fbcb4bde
The code above works when an application is launched. But I can't get it to work when an application is closed. I tried setting wParam=2 but it didn't help. What more changes need to made to that code so that it can detect closing of a window properly? Thanks.
Detecting Window close using shell hook Topic is solved
-
- Posts: 34
- Joined: 09 Jun 2017, 09:19
Re: Detecting Window close using shell hook
I would think that WinGetTitle doesn't make any sense anymore after the window is destroyed, because the windows is already gone at this point and no window title can be determined anymore.
You could probably add this to the Shellmessage function
But you would get only the Ahk_id (named ID in this case) of the destroyed window, if that is good enough for you. Then you could add if (ID = "The window ID you want to check") like in the original script, if you are looking for a specific window ID you already know. (Note that the ID of a specific window will be different every time it is created)
But of course, If you saved the IDs together with the titles of the previously existing/created windows, you could also determine the title of the destroyed window. For saving IDs between function calls you could use global or static(?) variables or arrays, depending on your goal.
(In the end, you could move ID:=lParam in front of all the Ifs because you need it in both cases.)
HTH
You could probably add this to the Shellmessage function
Code: Select all
else If (wParam=2)
{
ID:=lParam
MsgBox, The window %ID% has been destroyed!
}
But of course, If you saved the IDs together with the titles of the previously existing/created windows, you could also determine the title of the destroyed window. For saving IDs between function calls you could use global or static(?) variables or arrays, depending on your goal.
(In the end, you could move ID:=lParam in front of all the Ifs because you need it in both cases.)
HTH
-
- Posts: 34
- Joined: 09 Jun 2017, 09:19
Re: Detecting Window close using shell hook Topic is solved
Thanks for your reply, was able to get it working.gregster wrote: But you would get only the Ahk_id (named ID in this case) of the destroyed window, if that is good enough for you. Then you could add if (ID = "The window ID you want to check") like in the original script, if you are looking for a specific window ID you already know. (Note that the ID of a specific window will be different every time it is created)
But of course, If you saved the IDs together with the titles of the previously existing/created windows, you could also determine the title of the destroyed window. For saving IDs between function calls you could use global or static(?) variables or arrays, depending on your goal.
Code: Select all
; ----------------------------------------------
; ------------- SHELL HOOK EXAMPLE -------------
; ----------------------------------------------
; 1 HSHELL_WINDOWCREATED
; 2 HSHELL_WINDOWDESTROYED
; 3 HSHELL_ACTIVATESHELLWINDOW
; 4 HSHELL_WINDOWACTIVATED
; 5 HSHELL_GETMINRECT
; 6 HSHELL_REDRAW
; 7 HSHELL_TASKMAN
; 8 HSHELL_LANGUAGE
; 9 HSHELL_SYSMENU
; 10 HSHELL_ENDTASK
; 11 HSHELL_ACCESSIBILITYSTATE
; 12 HSHELL_APPCOMMAND
; 13 HSHELL_WINDOWREPLACED
; 14 HSHELL_WINDOWREPLACING
; 15 HSHELL_HIGHBIT
; 16 HSHELL_FLASH
; 17 HSHELL_RUDEAPPACTIVATED
#Persistent
SetBatchLines, -1
DllCall("RegisterShellHookWindow", "ptr", A_ScriptHwnd)
MsgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
OnMessage(MsgNum, "ShellMessage")
Return
global nil
ShellMessage(wParam, lParam)
{
If (wParam=1) ; HSHELL_WINDOWCREATED := 1
{
ID:=lParam
WinGetTitle, title, Ahk_id %ID%
if (title == "Title of the program") ; Enter the program title between quotes
{
nil = %ID%
MsgBox, %ID% opened.
}
}
If (wParam=2) ; HSHELL_WINDOWDESTROYED := 2
{
ID:=lParam
if (nil == ID)
{
MsgBox, %ID% closed.
}
}
}