Sign in to follow this  
Followers 0
VAN0

Detect how window was closed?

3 posts in this topic

Hello.

Normally there are 3 ways to close a window:

1. click on X button

2. click on window icon and select close

3. alt+f4

Is there a way detect each of these methods separately?

Thank you.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Is it for a AutoIt GUI or an external window ?

For a GUI made with AutoIt, you can try something like this :

#include <WindowsConstants.au3>
#include <Misc.au3>

Global Const $SC_CLOSE = 0xF060
GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")

GUICreate("gui")
Local $msg
Local $exit = GUICtrlCreateButton("Exit", 10, 10, 200, 25)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $exit
            ConsoleWrite("Closed with Exit button" & @CRLF)
            Exit
    EndSwitch
WEnd



Func WM_SYSCOMMAND($hWndGUI, $MsgID, $WParam, $LParam)
    If $WParam = $SC_CLOSE Then
        If _IsPressed("12") And _IsPressed("73") Then
            ConsoleWrite("Closed with ALT + F4" & @CRLF)
        ElseIf _IsPressed("1B") Then
            ConsoleWrite("Closed with ESC" & @CRLF)
        Else
            ConsoleWrite("Closed with X " & @CRLF)
        EndIf

        Exit
    EndIf
EndFunc
Edited by jguinch
1 person likes this

Share this post


Link to post
Share on other sites

Very good. Thank you.

Any way capture if it was closed via window icon menu? In your example it shows as "Close with X"

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0