VB 6.0 system tray application does not close gracefully when Windows shuts down

I use the following code in a VB 6.0 application to provide the application with a system tray icon:

Option Explicit 'user defined type required by Shell_NotifyIcon API call Public Type NOTIFYICONDATA cbSize As Long hwnd As Long uId As Long uFlags As Long uCallBackMessage As Long hIcon As Long szTip As String * 64 End Type 'constants required by Shell_NotifyIcon API call: Public Const NIM_ADD = &H0 Public Const NIM_MODIFY = &H1 Public Const NIM_DELETE = &H2 Public Const NIF_MESSAGE = &H1 Public Const NIF_ICON = &H2 Public Const NIF_TIP = &H4 Public Const WM_MOUSEMOVE = &H200 Public Const WM_LBUTTONDOWN = &H201 'Button down Public Const WM_LBUTTONUP = &H202 'Button up Public Const WM_LBUTTONDBLCLK = &H203 'Double-click Public Const WM_RBUTTONDOWN = &H204 'Button down Public Const WM_RBUTTONUP = &H205 'Button up Public Const WM_RBUTTONDBLCLK = &H206 'Double-click Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Public nid As NOTIFYICONDATA 

I want the application to be minimized to the system tray when you click Window X to close it. I am doing this with the following code in the form of a QueryUnload event:

 Me.WindowState = vbMinimized Me.Hide 

In the Unload event form, I do the following:

 Shell_NotifyIcon NIM_DELETE, nid 

The PROBLEM is that when I turn off the operating system and Windows sends the WM_CLOSE message to the application, QueryUnload fires, but apparently this is not an Unload event, as Windows asks for it to complete my task.

Any ideas on how to get the application to close correctly when Windows shuts down?

thanks

+3
source share
2 answers

This is what I use when I close my programs for Query_Unload:

 Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Select Case UnloadMode Case 1, 2, 3 'If the program is being terminated by Code, Windows shutting down, or Task Manager Cancel = False 'Allow the program termination Unload Me Case Else Cancel = True 'Else disallow the termination End Select End Sub 

If case 2 ("Shutting down Windows"), I continue and end the program.

Let me know if this helps! Jfv

+4
source

Here is an example implementation from vbnet http://vbnet.mvps.org/index.html?code/subclass/shellnotifybasic.htm

+1
source

All Articles