Couldn´t you just define a variable userClose or something like that
and set it to true if the user presses the close button? In the OnClose event
you then just query the variable and can through this determine if the user
shuts the program down or the system.
The only problem is if you allow the user to press the x in the upper right corner.
At the moment the only thing I could imagine is to analyse the mouse coordinates
and if the mouse is hovering above the x and a click occurs, set the variable to true.
Hope this helps.
Greetings, timtos.