1.VB,托盘图标,事件(窗口进入托盘图标后,点击图标打开窗口,这
Private Sub Form_Load()
With nfIconData
.hWnd = Me.hWnd
.uID = Me.Icon
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon.Handle
.szTip = App.Title + "(版本 " & App.Major & "." & App.Minor & "." & App.Revision & ")" & vbNullChar
.cbSize = Len(nfIconData)
End With
Call Shell_NotifyIcon(NIM_ADD, nfIconData)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static Message As Long
Static RR As Boolean
Message = X / Screen.TwipsPerPixelX
If RR = False Then
RR = True
Select Case Message
Case WM_LBUTTONUP
Me.Show
Me.WindowState = 0
Case WM_RBUTTONUP
Me.PopupMenu m '见注释
End Select
RR = False
End If
End Sub
'注释,m为右击鼠标时出现的菜单,可以用菜单编辑器根据你需要编,如果你想在双击图标时才显示窗口,可改为:
Case WM_LBUTTONDBLCLK
Me.Show
Me.WindowState = 0
2.vb 编程 系统托盘 加通知栏图标
'■■以下是窗体 Form1 的代码 ------------------------------------' 1.为窗体添加菜单' 为窗体添加菜单: mFast' 为 mFast 添加下级子菜单 mmFast,并将 mmFast 的索引设置为 0' 2.在窗体上添加四个控件,所有控件均采用默认设置:' Label1,Command1,Command2,Command3 Dim ctTrayIco As NotifyIconData Private Sub Form_Load() Me.Caption = "系统托盘例子" '为数组菜单 mmFast 添加条目 mFast.Visible = False LoadKjZu mmFast, "显示 " & Me.Caption & "(&R)", "mmFast-Run" LoadKjZu mmFast, "最小化窗口(&N)", "mmFast-Min" LoadKjZu mmFast, "菜单例子 1 (&A)", "mmFast-A" LoadKjZu mmFast, "菜单例子 2 (&B)", "mmFast-B" LoadKjZu mmFast, "-" '菜单分隔条 LoadKjZu mmFast, "退出(X)", "mmFast-Exit" Dim S As Single Label1.AutoSize = True: S = Label1.Height Label1.Caption = Me.Caption: Label1.Move S, S Command1.Caption = "添加到系统托盘": Command1.Move S, S * 3, S * 11, S * 2 Command2.Caption = "最小化到系统托盘": Command2.Move S, S * 6, S * 11, S * 2 Command3.Caption = "从系统托盘删除图标": Command3.Move S, S * 9, S * 11, S * 2 '将窗口函数的地址设置为模块中的 WndProc 过程,当用户 '在系统托盘图标单击鼠标右键时,弹出自定义的菜单 mFast WinAddress Me.hWnd ''如果需要程序一启动就添加到系统托盘,解除下面语句的注释即可 'Call SysTray End Sub Private Sub Form_Resize()'设置快捷菜单的是否可用状态 Dim nEnabled As Boolean On Error Resume Next nEnabled = Me.WindowState = vbMinimized mmFast(KjZuIndex(mmFast, "mmFast-Min")).Enabled = nEnabled mmFast(KjZuIndex(mmFast, "mmFast-Run")).Enabled = Not nEnabled End Sub Private Sub Form_Unload(Cancel As Integer) SysTray True '退出时,从系统托盘删除本程序图标 WinAddress Me.hWnd, True '退出时,将窗口地址还原 End Sub Private Sub LoadKjZu(Kj As Object, nCap As String, Optional nTag As String) '为数组控件添加一个成员 Dim I As Long I = Kj.UBound If Kj(I).Tag <> "" Then I = I + 1 Load Kj(I): Kj(I).Visible = True End If Kj(I).Caption = nCap If nCap = "-" Then Kj(I).Tag = "bar" Else Kj(I).Tag = nTag End Sub Private Function KjZuIndex(Kj As Object, nTag As String) As Long '返回数组控件中 Tag 属性为 nTag 的成员索引,没有找到返回 -1 Dim I As Long For I = Kj.LBound To Kj.UBound If Kj(I).Tag <> nTag Then KjZuIndex = I: Exit Function Next KjZuIndex = -1 End Function Private Sub mmFast_Click(Index As Integer) '系统托盘快捷菜单 Dim nCmd As String nCmd = Trim(mmFast(Index).Tag) Select Case UCase(nCmd) Case UCase("mmFast-Run"): Call WinNormal '正常显示主窗口 Case UCase("mmFast-Min"): Call WinMinimized '缩小到系统托盘 Case UCase("mmFast-Exit"): Unload Me '退出程序 Case UCase("mmFast-A"): MsgBox "这是系统托盘快捷菜单:mmFast-A", vbInformation, Me.Caption 'Case UCase("mmFast-B") Case Else: MsgBox "此命令在“mmFast_Click”中无效:" & vbCrLf & vbCrLf & "nCmd = " & nCmd, vbInformation, Me.Caption & " - 无效命令" End Select End Sub Private Sub Command1_Click() Call SysTray '添加到系统托盘 End Sub Private Sub Command2_Click() Call WinMinimized '缩小到系统托盘 End Sub Private Sub Command3_Click() Call SysTray(True) '从系统托盘删除本程序图标 End Sub Private Sub WinNormal() '正常显示主窗口 Me.Visible = True DoEvents Me.WindowState = 0 End Sub Private Sub WinMinimized()'缩小到系统托盘 Me.WindowState = vbMinimized Call SysTray: Me.Visible = False Label1.Caption = "缩小到系统图标" End Sub Private Sub SysTray(Optional IcoDel As Boolean)'在系统托盘为本程序添加图标 Dim dl As Long If IcoDel Then dl = Shell_NotifyIcon(NIM_DELETE, ctTrayIco) If dl = 0 Then Label1.Caption = "从系统托盘删除图标,失败" Else Label1.Caption = "已从系统托盘删除图标,成功" End If Exit Sub End If ctTrayIco.cbSize = Len(ctTrayIco) ctTrayIco.hWnd = Me.hWnd ctTrayIco.uID = SysTray_ID ctTrayIco.uFlags = NIF_MESSAGE + NIF_ICON + NIF_TIP ctTrayIco.uCallbackMessage = WM_Tray ctTrayIco.hIcon = Me.Icon.Handle ctTrayIco.szTip = "我的系统托盘图标" & Chr(0) dl = Shell_NotifyIcon(NIM_ADD, ctTrayIco) If dl = 0 Then Label1.Caption = "图标添加到系统托盘,失败" Else Label1.Caption = "图标已添加到系统托盘,成功" End If End Sub'■■以下是模块代码 ------------------------------------ Public Type NotifyIconData cbSize As Long '结构大小,设置为:Len(NotifyIconData) hWnd As Long '建立托盘图标窗体的句柄 uID As Long '托盘图标 ID 标识 uFlags As Long '对图标的操作方式: uCallbackMessage As 。