在托盘图标上单击鼠标右键显示一个程序菜单,根据菜单单项进行处理。
而双击鼠标左键可以启动主窗口或应用程序。
该怎么实现,最好给点code,谢!

解决方案 »

  1.   

    系统托盘是吧
    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 Type
    Private Const NIM_ADD = &H0
    Private Const NIM_MODIFY = &H1
    Private Const NIM_DELETE = &H2
    Private Const NIF_MESSAGE = &H1
    Private Const NIF_ICON = &H2
    Private Const NIF_TIP = &H4
    Private Const WM_LBUTTONDBLCLK = &H203
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_RBUTTONUP = &H205
    Private Const WM_LBUTTONUP = &H202Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
    Private TrayI As NOTIFYICONDATA
    Private Sub Form_Load()
    Dim i As Integer    Me.Hide
        With TrayI
            .cbSize = Len(TrayI)
            .hWnd = Me.hWnd
            .uId = 1&
            .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
            .ucallbackMessage = WM_LBUTTONDOWN
            .hIcon = imgIcon(2).Picture '这里是显示的图标
            .szTip = "程序正在运行" & Chr$(0) '这里是任务栏显示的字
            Shell_NotifyIcon NIM_ADD, TrayI
        End With
        TimerReplaceIcon.Enabled = True
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
          With TrayI
            .uFlags = 0
          End With
          Shell_NotifyIcon NIM_DELETE, TrayI
    End Sub
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim Msg As Integer
    On Error Resume Next
            Msg = X / Screen.TwipsPerPixelX
           
                
            If Msg = WM_RBUTTONUP Then
                '弹出右键菜单。
                 'PopupMenu mnuNotifyRight
            ElseIf Msg = WM_LBUTTONDBLCLK Then MsgBox "您双击了我"
                
            End If
    End Sub
      

  2.   

    系统托盘
    Option ExplicitPublic OldWindowProc As Long
    Public TheForm As Form
    Public TheMenu As MenuDeclare 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 Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As LongPublic Const WM_USER = &H400
    Public Const WM_LBUTTONUP = &H202
    Public Const WM_MBUTTONUP = &H208
    Public Const WM_RBUTTONUP = &H205
    Public Const TRAY_CALLBACK = (WM_USER + 1001&)
    Public Const GWL_WNDPROC = (-4)
    Public Const GWL_USERDATA = (-21)
    Public Const NIF_MESSAGE = &H1
    Public Const NIF_ICON = &H2
    Public Const NIF_TIP = &H4
    Public Const NIM_ADD = &H0
    Public Const NIM_MODIFY = &H1
    Public Const NIM_DELETE = &H2Public 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 TheData As NOTIFYICONDATAPublic Function NewWindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If Msg = TRAY_CALLBACK Then
         
            If lParam = WM_LBUTTONUP Then
                TheForm.Visible = Not TheForm.Visible
                TheForm.WindowState = vbNormal
                      ElseIf lParam = WM_RBUTTONUP Then
                TheForm.PopupMenu TheMenu
            End If
        Else
            NewWindowProc = CallWindowProc(OldWindowProc, hwnd, Msg, wParam, lParam)
        End If
        End FunctionPublic Sub AddToTray(frm As Form, mnu As Menu)
        Set TheForm = frm
       Set TheMenu = mnu
        
        OldWindowProc = SetWindowLong(frm.hwnd, _
            GWL_WNDPROC, AddressOf NewWindowProc)
        
        With TheData
            .uID = 0
            .hwnd = frm.hwnd
            .cbSize = Len(TheData)
            .hIcon = frm.Icon.Handle
            .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
            .uCallbackMessage = TRAY_CALLBACK
            .cbSize = Len(TheData)
            .szTip = frm.Caption & vbNullChar
        End With
        Shell_NotifyIcon NIM_ADD, TheData
    End SubPublic Sub RemoveFromTray()
        With TheData
            .uFlags = 0
        End With
        Shell_NotifyIcon NIM_DELETE, TheData
        SetWindowLong TheForm.hwnd, GWL_WNDPROC, OldWindowProc
    End SubPublic Sub SetTrayTip(tip As String)
        With TheData
            .szTip = tip & vbNullChar
            .uFlags = NIF_TIP
        End With
        Shell_NotifyIcon NIM_MODIFY, TheData
    End SubPublic Sub SetTrayIcon(pic As Picture)
        If pic.Type <> vbPicTypeIcon Then Exit Sub
        With TheData
            .hIcon = pic.Handle
            .uFlags = NIF_ICON
        End With
        Shell_NotifyIcon NIM_MODIFY, TheData
    End Sub