vb编的一个小程序,如何让他在点击右上角关闭按钮(就是那个叉号)时,在托盘 里继续运行?
2个回答
展开全部
VB6:
你可以在工程里多建一个窗体,操作的菜单写在这里,不显示9 OK了
当你要的窗体关闭后 再+载托盘
VB.NET里直接9有Closing和Closed事件
以下9是托盘:
Dim OldWindowProc As Long
Dim TheForm As Form
Dim TheMenu As Menu
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, _
ByVal dwFlags As Long) As Long
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
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
Private TheData As NOTIFYICONDATA
Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = (&H400 + 1001&) Then
If lParam = &H205 Then
FormM.SetFocus
TheForm.PopupMenu TheMenu
Exit Function
End If
End If
NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
End Function
Public Sub AddToTray(frm As Form, mnu As Menu)
Set TheForm = frm
Set TheMenu = mnu
OldWindowProc = SetWindowLong(frm.hwnd, (-4), AddressOf NewWindowProc)
With TheData
.Uid = 0
.hwnd = frm.hwnd
.cbSize = Len(TheData)
.HIcon = frm.Icon.Handle
.UFlags = 2
.UCallbackMessage = (&H400 + 1001&)
.UFlags = .UFlags Or 1
.cbSize = Len(TheData)
End With
Shell_NotifyIcon 0, TheData
End Sub
Public Sub RemoveFromTray()
With TheData
.UFlags = 0
End With
Shell_NotifyIcon 2, TheData
SetWindowLong TheForm.hwnd, (-4), OldWindowProc
End Sub
Public Sub SetTrayTip(tip As String)
With TheData
.SzTip = tip & vbNullChar
.UFlags = 4
End With
Shell_NotifyIcon 1, TheData
End Sub
你可以在工程里多建一个窗体,操作的菜单写在这里,不显示9 OK了
当你要的窗体关闭后 再+载托盘
VB.NET里直接9有Closing和Closed事件
以下9是托盘:
Dim OldWindowProc As Long
Dim TheForm As Form
Dim TheMenu As Menu
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, _
ByVal dwFlags As Long) As Long
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
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
Private TheData As NOTIFYICONDATA
Public Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = (&H400 + 1001&) Then
If lParam = &H205 Then
FormM.SetFocus
TheForm.PopupMenu TheMenu
Exit Function
End If
End If
NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
End Function
Public Sub AddToTray(frm As Form, mnu As Menu)
Set TheForm = frm
Set TheMenu = mnu
OldWindowProc = SetWindowLong(frm.hwnd, (-4), AddressOf NewWindowProc)
With TheData
.Uid = 0
.hwnd = frm.hwnd
.cbSize = Len(TheData)
.HIcon = frm.Icon.Handle
.UFlags = 2
.UCallbackMessage = (&H400 + 1001&)
.UFlags = .UFlags Or 1
.cbSize = Len(TheData)
End With
Shell_NotifyIcon 0, TheData
End Sub
Public Sub RemoveFromTray()
With TheData
.UFlags = 0
End With
Shell_NotifyIcon 2, TheData
SetWindowLong TheForm.hwnd, (-4), OldWindowProc
End Sub
Public Sub SetTrayTip(tip As String)
With TheData
.SzTip = tip & vbNullChar
.UFlags = 4
End With
Shell_NotifyIcon 1, TheData
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询