Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Detect window close


  • Please log in to reply
5 replies to this topic
cmulcahy
  • Guests
  • Last active:
  • Joined: --
Hello, all.

I would like to detect when a window closes. I have an app that needs to refresh a window whenever a child window closes. I would like to send the refresh automatically.

Can someone point me towards detecting when a window closes so I can perform an action?

Thanks

ian_what
  • Members
  • 20 posts
  • Last active: May 13 2008 08:37 AM
  • Joined: 29 Nov 2007
Theres a few functions in the FAQ which may help

heres one

IfWinExist <!-- m -->http://www.autohotke.../IfWinExist.htm<!-- m -->

IfWinExist, MyWindow, 
Msgbox, Window still Running.

IfWinNotExist, MyWindow,
Msgbox, Window Has Been Closed PANIC!!!!

:D hope that helps

erm code untested but i think it should work, im still new to AHK

Ian

  • Guests
  • Last active:
  • Joined: --
Thanks for the very quick reply!

So, basically, I wait for the window to be opened, set a flag, then wait for it to be closed via IfWinNotExist?

In the Windows message APIs, there is an event fired when the window is closed. Is that available in AHK?

ian_what
  • Members
  • 20 posts
  • Last active: May 13 2008 08:37 AM
  • Joined: 29 Nov 2007
thats how id do it, theres probably a better way, but as the Gurus seem to be away atm im still waiting for a solution to what ive asked, i figured id try my knowlege Have you checked out the A-Z command list thats helped me loads since ive started

<!-- m -->http://www.autohotke...cs/commands.htm<!-- m -->

you may have to tweak the code i posted to work as i didnt test it with anything ^^ but the faq explains all the switches that are avaliable

Post a bit of your code as an example so i get an idea of whats happeing if you want

Conquer
  • Members
  • 385 posts
  • Last active: Jan 10 2013 02:14 AM
  • Joined: 27 Jun 2006
Note. This wont be any sense/fun for beginners.

I'd go with RegisterShellHook DLL Call :)

That way, your script gets notified ASAP when a window opens or closes. Heres an example script about how to register a "Shell Hook" as well as what the messages your script will receive.

; SHELL MESSAGES

#Persistent
Menu,Tray,Add
Menu,Tray,Add, &Show, GuiShow
Menu,Tray,Default, &Show
Gui, Font, s9, Courier New

Gui +ToolWindow +AlwaysOnTop +Resize +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessages" )

Gui, Add, Edit, w512 h512 vMsgs hwndEditC +ReadOnly
Gui, Show, x10 y10, Shell Spy

MsgNames =
(
HSHELL_WINDOWCREATED
HSHELL_WINDOWDESTROYED
HSHELL_ACTIVATESHELLWINDOW
HSHELL_WINDOWACTIVATED
HSHELL_GETMINRECT
HSHELL_REDRAW
HSHELL_TASKMAN
HSHELL_LANGUAGE
HSHELL_SYSMENU
HSHELL_ENDTASK
HSHELL_ACCESSIBILITYSTATE
HSHELL_APPCOMMAND
HSHELL_WINDOWREPLACED
HSHELL_WINDOWREPLACING
HSHELL_HIGHBIT
HSHELL_FLASH
HSHELL_RUDEAPPACTIVATED
)

AppCommands =
(
APPCOMMAND_BROWSER_BACKWARD = 1
APPCOMMAND_BROWSER_FORWARD = 2
APPCOMMAND_BROWSER_REFRESH = 3
APPCOMMAND_BROWSER_STOP = 4
APPCOMMAND_BROWSER_SEARCH = 5
APPCOMMAND_BROWSER_FAVORITES = 6
APPCOMMAND_BROWSER_HOME = 7
APPCOMMAND_VOLUME_MUTE = 8
APPCOMMAND_VOLUME_DOWN = 9
APPCOMMAND_VOLUME_UP = 10
APPCOMMAND_MEDIA_NEXTTRACK = 11
APPCOMMAND_MEDIA_PREVIOUSTRACK = 12
APPCOMMAND_MEDIA_STOP = 13
APPCOMMAND_MEDIA_PLAY_PAUSE = 14
APPCOMMAND_LAUNCH_MAIL = 15
APPCOMMAND_LAUNCH_MEDIA_SELECT = 16
APPCOMMAND_LAUNCH_APP1 = 17
APPCOMMAND_LAUNCH_APP2 = 18
APPCOMMAND_BASS_DOWN = 19
APPCOMMAND_BASS_BOOST = 20
APPCOMMAND_BASS_UP = 21
APPCOMMAND_TREBLE_DOWN = 22
APPCOMMAND_TREBLE_UP = 23
APPCOMMAND_MICROPHONE_VOLUME_MUTE = 24
APPCOMMAND_MICROPHONE_VOLUME_DOWN = 25
APPCOMMAND_MICROPHONE_VOLUME_UP = 26
APPCOMMAND_HELP = 27
APPCOMMAND_FIND = 28
APPCOMMAND_NEW = 29
APPCOMMAND_OPEN = 30
APPCOMMAND_CLOSE = 31
APPCOMMAND_SAVE = 32
APPCOMMAND_PRINT = 33
APPCOMMAND_UNDO = 34
APPCOMMAND_REDO = 35
APPCOMMAND_COPY = 36
APPCOMMAND_CUT = 37
APPCOMMAND_PASTE = 38
APPCOMMAND_REPLY_TO_MAIL = 39
APPCOMMAND_FORWARD_MAIL = 40
APPCOMMAND_SEND_MAIL = 41
APPCOMMAND_SPELL_CHECK = 42
APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE = 43
APPCOMMAND_MIC_ON_OFF_TOGGLE = 44
APPCOMMAND_CORRECTION_LIST = 45
)
Return


ShellMessages( wP,lP ) {
  Global EditC
  Global mVal := lP
  GuiControlGet, Msgs
  Routine := GetMessageName( wP )
  IfEqual,Routine,, SetEnv, Routine, UNKNOWN
  GuiControl,, Msgs, % Msgs "`n`n" Routine "  [" wP "]"
  If IsLabel(Routine)
     GoSub, %Routine%
  ControlSend,, ^{End}, ahk_id %EditC%
}


GetMessageName( FieldN=0 ) {
  Global MsgNames
  Loop, Parse, MsgNames, `n
        IfEqual, A_Index, %FieldN%, Return, A_LoopField
}

GetAppCommand( FieldN=0 ) {
  Global AppCommands
  Loop, Parse, AppCommands, `n
        IfEqual, A_Index, %FieldN%, Return, A_LoopField
}

UNKNOWN:
HSHELL_WINDOWCREATED:
HSHELL_WINDOWACTIVATED:
HSHELL_WINDOWDESTROYED:
HSHELL_REDRAW:
HSHELL_FLASH:
HSHELL_ENDTASK:
HSHELL_WINDOWREPLACING:
HSHELL_WINDOWREPLACED:   
HSHELL_RUDEAPPACTIVATED:   

 WinGetTitle, Title, ahk_id %mVal%
 WinGetClass, Class, ahk_id %mVal%
 GuiControlGet, Msgs
 GuiControl,, Msgs
   , % Msgs "`n`nhWnd`t: " WinExist("ahk_id" mVal) "`nTitle`t: " Title "`nClass`t: " Class

Return

HSHELL_GETMINRECT:
Return

HSHELL_APPCOMMAND:
 GuiControlGet, Msgs
 GuiControl,, Msgs, % Msgs " // " GetAppCommand( mVal >> 16 )
Return

GuiClose:
 Gui, Show, Hide
Return
    
GuiShow:
 Gui, Show, 
Return
Note that the above code is not by me, it seems the creator did not leave his name in comments. :/

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
@Conquer

I would like to detect when a window closes. I have an app that needs to refresh a window whenever a child window closes. I would like to send the refresh automatically.


SHELLHOOK will not work for Child Windows and BTW the relevant post is here : How to Hook on to Shell to receive its messages ?

:)