Windows关闭时,VB 6.0系统托盘应用程序无法正常关闭

时间:2008-12-15 04:07:11

标签: winapi vb6

我在VB 6.0应用程序中使用以下代码,以允许为应用程序提供系统托盘图标:

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

当您单击Window的X关闭它时,我希望应用程序最小化到系统托盘。我使用表单的QueryUnload事件中的以下代码完成此操作:

Me.WindowState = vbMinimized
Me.Hide

在表单的Unload事件中,我执行以下操作:

Shell_NotifyIcon NIM_DELETE, nid

问题是,当我关闭操作系统并且Windows将WM_CLOSE消息发送到应用程序时,QueryUnload正在被触发,但显然不是Unload事件,因为Windows提示我它想要结束我的任务。

有关如何在Windows关闭时正常关闭应用程序的任何想法?

由于

2 个答案:

答案 0 :(得分:4)

这是我在关闭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

如果是案例2(Windows Shutting Down),我继续让程序终止。

如果这有帮助,请告诉我! JFV

答案 1 :(得分:1)