IanM's Matrix 1 plugin set contains commands to do this.
first bit...
// for IanM's catch the window close routine
#constant WM_CLOSE 0x0010
next bit...
// IanM's Stop the window closing method :)
set message callback "WndProc"
and then with your functions
//******************************************************************
// WndProc() - called by IanM's close window bit
//******************************************************************
function WndProc(hwnd as dword, msg as dword, wparam as dword, lparam as dword)
if msg = WM_CLOSE
stop current message
// Your code here to either clean up or jump to clean up bit
endif
endfunction 0
This is how I do it anyway.
Hope that helps