Private Sub Form_Load() Me.Show Me.Refresh With nid .cbSize = Len(nid) .hwnd = Me.hwnd .uId = vbNull .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE .uCallBackMessage = WM_MOUSEMOVE .hIcon = Me.Icon .szTip = "炸弹工作中" & vbNullChar End With Shell_NotifyIcon NIM_ADD, nid End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'this procedure receives the callbacks from the System Tray icon. Dim Result As Long Dim msg As Long 'the value of X will vary depending upon the scalemode setting If Me.ScaleMode = vbPixels Then msg = X Else msg = X / Screen.TwipsPerPixelX End If Select Case msg Case WM_LBUTTONUP '514 restore form window Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show Case WM_LBUTTONDBLCLK '515 restore form window Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show Case WM_RBUTTONUP '517 display popup menu Result = SetForegroundWindow(Me.hwnd) Me.PopupMenu Me.mPopupSys End Select End SubPrivate Sub Form_Resize() 'this is necessary to assure that the minimized window is hidden If Me.WindowState = vbMinimized Then Me.Hide End SubPrivate Sub Form_Unload(Cancel As Integer) 'this removes the icon from the system tray Shell_NotifyIcon NIM_DELETE, nid End SubPrivate Sub mPopExit_Click() 'called when user clicks the popup menu Exit command Unload Me End SubPrivate Sub mPopRestore_Click() 'called when the user clicks the popup menu Restore command Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show End Sub在FORM中编辑MENU EDITOR编辑一个INVISIBLE一及菜单和两个可见的二级彩单!具体命名见程序中所含!
声明的部分 Option Explicit Private 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 TypePrivate Const NIM_ADD = &H0 Private Const NIM_MODIFY = &H1 Private Const NIM_DELETE = &H2'The following constant is the message sent when a mouse event occurs 'within the rectangular boundaries of the icon in the taskbar status 'area. Private Const WM_MOUSEMOVE = &H200'The following constants are the flags that indicate the valid 'members of the NOTIFYICONDATA data type. Private Const NIF_MESSAGE = &H1 Private Const NIF_ICON = &H2 Private Const NIF_TIP = &H4'The following constants are used to determine the mouse input on the 'the icon in the taskbar status area.Private Const WM_LBUTTONDBLCLK = &H203 'Double-click Private Const WM_LBUTTONDOWN = &H201 'Button down Private Const WM_LBUTTONUP = &H202 'Button up Private Const WM_RBUTTONDBLCLK = &H206 'Double-click Private Const WM_RBUTTONDOWN = &H204 'Button down Private Const WM_RBUTTONUP = &H205 'Button upPrivate Declare Function Shell_NotifyIcon Lib "shell32" _ Alias "Shell_NotifyIconA" _ (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Dim nid As NOTIFYICONDATA
wjying(葡萄) 给的答案有一点小错误:Private Sub Form_Unload(Cancel As Integer) 'this removes the icon from the system tray Shell_NotifyIcon NIM_DELETE, nid End Sub在以上代码中应加上一句 Cancel=true 否则不能达到"按关闭按钮时缩小到托盘"的目的。
Private Sub Form_Unload(Cancel As Integer) 'this removes the icon from the system tray Shell_NotifyIcon NIM_DELETE, nid End Sub能达到"按关闭按钮时缩小到托盘"的目的?!Private Flags As BooleanPrivate Sub CmdExit_Click() Flags=True Unload me End SubPrivate Sub Form_Unload(Cancel As Integer) If Flage Then Shell_NotifyIcon NIM_DELETE, nid Else Cancel=True …… Shell_NotifyIcon NIM_ADD, nid '添加图标 End If End Sub
右上角关闭按钮触发Form_QueryUnload事件, Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)End Sub 然后是form_unload 所以你可以在Form_QueryUnload里通过UnloadMode 控制
Private Sub Form_Load()
Me.Show
Me.Refresh
With nid
.cbSize = Len(nid)
.hwnd = Me.hwnd
.uId = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallBackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon
.szTip = "炸弹工作中" & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, nid
End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'this procedure receives the callbacks from the System Tray icon.
Dim Result As Long
Dim msg As Long
'the value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg
Case WM_LBUTTONUP '514 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_LBUTTONDBLCLK '515 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_RBUTTONUP '517 display popup menu
Result = SetForegroundWindow(Me.hwnd)
Me.PopupMenu Me.mPopupSys
End Select
End SubPrivate Sub Form_Resize()
'this is necessary to assure that the minimized window is hidden
If Me.WindowState = vbMinimized Then Me.Hide
End SubPrivate Sub Form_Unload(Cancel As Integer)
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End SubPrivate Sub mPopExit_Click()
'called when user clicks the popup menu Exit command
Unload Me
End SubPrivate Sub mPopRestore_Click()
'called when the user clicks the popup menu Restore command
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
End Sub在FORM中编辑MENU EDITOR编辑一个INVISIBLE一及菜单和两个可见的二级彩单!具体命名见程序中所含!
Option Explicit
Private 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 TypePrivate Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2'The following constant is the message sent when a mouse event occurs
'within the rectangular boundaries of the icon in the taskbar status
'area.
Private Const WM_MOUSEMOVE = &H200'The following constants are the flags that indicate the valid
'members of the NOTIFYICONDATA data type.
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4'The following constants are used to determine the mouse input on the
'the icon in the taskbar status area.Private Const WM_LBUTTONDBLCLK = &H203 'Double-click
Private Const WM_LBUTTONDOWN = &H201 'Button down
Private Const WM_LBUTTONUP = &H202 'Button up
Private Const WM_RBUTTONDBLCLK = &H206 'Double-click
Private Const WM_RBUTTONDOWN = &H204 'Button down
Private Const WM_RBUTTONUP = &H205 'Button upPrivate Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Dim nid As NOTIFYICONDATA
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub在以上代码中应加上一句
Cancel=true
否则不能达到"按关闭按钮时缩小到托盘"的目的。
另外,我希望程序运行时任务栏和图标照样显示,不知上面的代码能否实现。
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub能达到"按关闭按钮时缩小到托盘"的目的?!Private Flags As BooleanPrivate Sub CmdExit_Click()
Flags=True
Unload me
End SubPrivate Sub Form_Unload(Cancel As Integer)
If Flage Then
Shell_NotifyIcon NIM_DELETE, nid
Else
Cancel=True
……
Shell_NotifyIcon NIM_ADD, nid '添加图标
End If
End Sub
上面那段有挺多错误
SetForegroundWindow 改成setwindowpos比较好具体声明你查一下吧
如果你不需要让窗体在最上层不用也可以
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)End Sub
然后是form_unload
所以你可以在Form_QueryUnload里通过UnloadMode 控制
顺便说一句,zyl910(910:分儿,我来了!中的代码中有一个地方把 flags 敲成了 flage,我记得在粘贴时用过并即时发现了,为方便以后的兄弟,特地说明一下,呵呵。
等该问题被挤出csdn前一页后,我会给分,谢谢各位。