Here is a small plugin (windows only) that will intercept the close message and allow you to save files or what ever you require.
This small example will allow you to click the close button 3 times before exiting the app
// Project: Detect Window Close
// Created: 2020-08-03
// show all errors
SetErrorMode(2)
#import_plugin WindowClose as WC
// set window properties
SetWindowTitle( "Detect Window Close" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// register the window with the plugin, sets user callback etc
agk_hwnd = WC.Register("Detect Window Close")
close_count = 1
do
// did the user try to close the window?
if WC.GetClose()
message("Close attempt: "+Str(close_count))
// on 3rd attempt we close the window (for test)
if close_count = 3
close_count=1
WC.Close(agk_hwnd)
endif
inc close_count, 1
endif
Print( agk_hwnd )
Sync()
loop
WARNING: once you register the window "WC.Register" you are responsible for closing the window.
Edit: BTW this avoids using hooks, I opted to go with SetWindowLongPtr with GWLP_WNDPROC so this should keeps AV's happy.