Partial Class HiddenForm Inherits Form Private oIContainer As System.ComponentModel.IContainer = Nothing Private Shared WM_QUERYENDSESSION As Integer = &H11 Private Shared lSystemShutdown As Boolean = False Public Sub New() InitializeComponent() End Sub Private Sub InitializeComponent() SuspendLayout() AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F) AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font ClientSize = New System.Drawing.Size(0, 0) FormBorderStyle = System.Windows.Forms.FormBorderStyle.None Name = "HiddenForm" Text = "HiddenForm" WindowState = System.Windows.Forms.FormWindowState.Minimized ResumeLayout(False) End Sub Protected Overrides Sub WndProc(ByRef toMessage As System.Windows.Forms.Message) ' If this is a logoff, shutdown or reboot If toMessage.Msg = WM_QUERYENDSESSION Then MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot") lSystemShutdown = True End If ' If this is WM_QUERYENDSESSION, the closing event should be raised in the base WndProc MyBase.WndProc(toMessage) End Sub Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing ' Michel Fournier lSystemShutdown = True ' If this is a shutdown If lSystemShutdown Then ' Reset the variable because the user might cancel the shutdown lSystemShutdown = False If MessageBox.Show("My application", "Do you want to save your work before logging off?", MessageBoxButtons.YesNo) = _ System.Windows.Forms.DialogResult.Yes Then e.Cancel = True Else e.Cancel = False End If End If End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) If Disposing AndAlso Not (oIContainer Is Nothing) Then oIContainer.Dispose() End If MyBase.Dispose(Disposing) End Sub End Class