Sorry I can't answer your question exactly. I've looked through several VB books that I have, including Microsoft's own, and none of them mention how the caption bar icons are treated.
Without loading VB5 (which I haven't done for many years), I can't check it out - but if you click on the Form to bring up the code editor, you will probably find the event code for Private Sub Form_Load().
Somewhere around there you might be able to spot an option to set up an event handler for when the Form_Close is clicked - then you could place your shut down code in that routine.
Certainly, when you click the 'X' icon, a message will be sent to Windows which you should be able to intercept.
In the Basic I'm currently using, you would use ..
' closing the window
.... whatever code you wish to execute ...
One of the problems with GUI systems like VB and RealBasic, is they don't let you get at all that's going on under the hood in Windows.
I like to have access to all of it, as in the bit of code above.
all the best,