好的简单方法是使用以下概念:
Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'check the close reason why form is closing
If e.CloseReason <> CloseReason.WindowsShutDown Then
' if windows is NOT shutting down then
'code what to do if user clicks X goes here
'example:
If MsgBox("Do you want to exit application?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Application exit") = MsgBoxResult.No Then e.Cancel = True
Else
'shuts down your application quietly
End If
End Sub