P
pamelafluente
Hi Guys,
I have a small application which uses a NotifyIcon. The user can set
the program so that when he clicks on the form-cancel button "X", the
program will instead be minimized in the icon try. See code below.
My problem is that my code is problably too naive. In fact there is a
problem when one tries to shut dow or restart Windows. Windows does not
want to shut down!
Could anybody by so kind as to suggest me the correct approach to do
this. Is there a way to detect the the closing is due to system restart
or shutdown?
Thank you very much in advance,
-Pam
-----------------------------------------------------------------------------
Private PreventClosing As Boolean
'if this is set TRUE by the user, the program minimizes (instead of
closing) when "X" is pressed
Private ShowInTaskbarSaved As Boolean
Private WindowStateSaved As FormWindowState
'...
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Me.PreventClosing Then
Me.HideInTrayIcon()
e.Cancel = True
End If
End Sub
Private Sub HideInTrayIcon()
Me.WindowStateSaved = Me.WindowState
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.NotifyIcon1.Visible = True
End Sub
'...
Private Sub RestoreWindow()
Me.NotifyIcon1.Visible = False
Me.ShowInTaskbar = Me.ShowInTaskbarSaved
Me.WindowState = Me.WindowStateSaved
Me.Show()
Me.Focus()
End Sub
I have a small application which uses a NotifyIcon. The user can set
the program so that when he clicks on the form-cancel button "X", the
program will instead be minimized in the icon try. See code below.
My problem is that my code is problably too naive. In fact there is a
problem when one tries to shut dow or restart Windows. Windows does not
want to shut down!
Could anybody by so kind as to suggest me the correct approach to do
this. Is there a way to detect the the closing is due to system restart
or shutdown?
Thank you very much in advance,
-Pam
-----------------------------------------------------------------------------
Private PreventClosing As Boolean
'if this is set TRUE by the user, the program minimizes (instead of
closing) when "X" is pressed
Private ShowInTaskbarSaved As Boolean
Private WindowStateSaved As FormWindowState
'...
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Me.PreventClosing Then
Me.HideInTrayIcon()
e.Cancel = True
End If
End Sub
Private Sub HideInTrayIcon()
Me.WindowStateSaved = Me.WindowState
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.NotifyIcon1.Visible = True
End Sub
'...
Private Sub RestoreWindow()
Me.NotifyIcon1.Visible = False
Me.ShowInTaskbar = Me.ShowInTaskbarSaved
Me.WindowState = Me.WindowStateSaved
Me.Show()
Me.Focus()
End Sub