Code: Select all
run, Notepad
OnMessage(0x10, WM_Close)
return
WM_Close:
Msgbox, Notepad was closed
return
esc::ExitApp
Code: Select all
run, Notepad
OnMessage(0x10, WM_Close)
return
WM_Close:
Msgbox, Notepad was closed
return
esc::ExitApp
Code: Select all
Run, notepad.exe
WinWait, ahk_class Notepad
WinWaitClose, ahk_class Notepad
MsgBox, Notepad is now closed.
Code: Select all
if (A_PtrSize = 8)
throw "Please run the script using 32-bit AutoHotkey"
SetWorkingDir, %A_ScriptDir%
if !FileExist("hook.dll")
throw "hook.dll is missing"
Run, Notepad,,, pid
WinWait, % "ahk_pid " pid
hWnd := WinExist()
hook1 := new HookMsg(hWnd, 0x10, "WM_CLOSE")
Return
WM_CLOSE(wParam, lParam, msg, hWnd) {
global hook1
MsgBox, close?
hook1 := "" ; remove hook
return 0
}
class HookMsg
{
__New(hWnd, MsgNumber, FuncName)
{
; hook.dll -- https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=7166
static _ := DllCall("LoadLibrary", "Str", "hook.dll", "Ptr")
static WH_CALLWNDPROC := 4
, WH_GETMESSAGE := 3
, UM_ADDMESSAGE := (WM_USER := 1024) + 0x100
this.hWnd := hWnd
this.iThreadIdTarget := DllCall("GetWindowThreadProcessId", "Ptr", this.hWnd, "Ptr", 0)
hook := DllCall("hook\InstallFilterDLL", "Int", WH_CALLWNDPROC, "Int", this.iThreadIdTarget, "Ptr", this.hWnd) ; 0 = Ok
hookG := DllCall("hook\InstallFilterDLL", "Int", WH_GETMESSAGE, "Int", this.iThreadIdTarget, "Ptr", this.hWnd) ; 0 = Ok
DllCall("SendMessage", "Ptr", this.hWnd, "UInt", UM_ADDMESSAGE, "UPtr", MsgNumber, "Ptr", A_ScriptHwnd, "Ptr")
OnMessage(MsgNumber, FuncName)
}
__Delete()
{
DllCall("hook\UnInstallFilterDLL", "Int", this.iThreadIdTarget, "Ptr", this.hWnd, "Ptr", A_ScriptHwnd)
}
}
Code: Select all
active := true
run,notepad
SetTimer, look, 100
return
look:
process, exist, notepad.exe
if (!ErrorLevel && active) {
MsgBox % "Notepad has been closed"
active := false
return
} else if ErrorLevel{
active := true
}
return
Code: Select all
#Persistent
Run, Notepad,,, pid
WinWait, % "ahk_pid " pid
global g_hWnd := WinExist()
DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
MsgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
OnMessage(MsgNum, "ShellMessage")
Return
ShellMessage(wParam, lParam) {
if (wParam = 2) ; HSHELL_WINDOWDESTROYED
&& (lParam = g_hWnd)
MsgBox Notepad closed
}
Code: Select all
ShellMessage(wParam, lParam) {
WinGet,hwnd, ID, ahk_class Notepad
if (wParam = 2) ; HSHELL_WINDOWDESTROYED
&& (lParam = hWnd)
MsgBox Notepad closed
}
Labels aren't supported.A function's name or, in [v1.1.20+], a function object. To pass a literal function name, it must be enclosed in quotes.
Code: Select all
ShellMessage(wParam, lParam) {
if (wParam = 2) ; HSHELL_WINDOWDESTROYED
{
if (lParam = g_hWnd)
MsgBox Notepad closed
}
else if (wParam = 1) ; HSHELL_WINDOWCREATED
{
if h := WinExist("ahk_exe notepad.exe")
g_hWnd := h
}
}
First of all, many thanks for all the help. You guys are amazing! This code worked perfectly for Notepad so I thought it would be a simple matter to adapt the script to OpenOffice (which was my real intention) by plugging in soffice.exe but I couldn't make the code work with my changes...@Odlanir Right. But it's better to get the target hwnd before OnMessage started, since ShellMessage function will be triggered frequently by many events such as HSHELL_WINDOWACTIVATED, etc..
When HSHELL_WINDOWDESTROYED event is received, the window is already destroyed, so WinGet, hwnd, ID, ahk_class Notepad will fail.
Maybe something like this:
Code: Select all
ShellMessage(wParam, lParam) { if (wParam = 2) ; HSHELL_WINDOWDESTROYED { if (lParam = g_hWnd) MsgBox Notepad closed } else if (wParam = 1) ; HSHELL_WINDOWCREATED { if h := WinExist("ahk_exe notepad.exe")
Code: Select all
#Persistent
Run, soffice.exe,,, pid
WinWait, % "ahk_pid " pid
global g_hWnd := WinExist()
DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
MsgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
OnMessage(MsgNum, "ShellMessage")
Return
ShellMessage(wParam, lParam) {
if (wParam = 2) ; HSHELL_WINDOWDESTROYED
{
if (lParam = g_hWnd)
MsgBox OpenOffice closed
}
else if (wParam = 1) ; HSHELL_WINDOWCREATED
{
if h := WinExist("ahk_exe soffice.bin")
g_hWnd := h
}
}
Code: Select all
WinWait, % "ahk_exe soffice.bin"
Code: Select all
WinWait, % "ahk_class SALFRAME"
Code: Select all
#Persistent
;based on:
;Is possible to get path when Explorer window is closing and restore when is opening? - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=49842&p=221423#p221423
OnNotepadStartEnd(hWinEventHook, vEvent, hWnd)
{
;EVENT_OBJECT_CREATE := 0x8000
;EVENT_OBJECT_DESTROY := 0x8001
static _ := DllCall("user32\SetWinEventHook", "UInt",0x8000, "UInt",0x8001, "Ptr",0, "Ptr",RegisterCallback("OnNotepadStartEnd"), "UInt",0, "UInt",0, "UInt",0, "Ptr")
DetectHiddenWindows, On
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "Notepad")
return
if (vEvent = 0x8000)
SoundPlay, *64
else if (vEvent = 0x8001)
{
WinGetTitle, vWinTitle, % "ahk_id " hWnd
SoundPlay, *48
MsgBox, % vWinTitle
}
}
Code: Select all
#Persistent
DllCall("user32\RegisterShellHookWindow", "Ptr",A_ScriptHwnd)
vMsg := DllCall("user32\RegisterWindowMessage", "Str","SHELLHOOK", "UInt")
OnMessage(vMsg, "OnNotepadStartEndAlt")
return
OnNotepadStartEndAlt(vEvent, hWnd)
{
if !(vEvent = 1) ;HSHELL_WINDOWCREATED := 1
&& !(vEvent = 2) ;HSHELL_WINDOWDESTROYED := 2
return
DetectHiddenWindows, On
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "Notepad")
return
if (vEvent = 1)
SoundPlay, *64
else
{
WinGetTitle, vWinTitle, % "ahk_id " hWnd
SoundPlay, *48
MsgBox, % vWinTitle
}
}
Users browsing this forum: Tvlao and 325 guests