Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
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 NIF_ICON = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_TIP = &H4
Private Const NIM_ADD = &H0
Private Const NIM_DELETE = &H2
Private Const NIM_MODIFY = &H1Dim nidIcon As NOTIFYICONDATAPrivate Sub Command1_Click()
    '添加
    Shell_NotifyIcon NIM_ADD, nidIcon
End SubPrivate Sub Command2_Click()
    '删除
    Shell_NotifyIcon NIM_DELETE, nidIcon
End SubPrivate Sub Form_Load()
    With nidIcon
        .cbSize = Len(nidIcon)
        .hIcon = Me.Icon
        .hwnd = Me.hwnd
        .szTip = "Sample" & Chr(0)
        .uCallbackMessage = 0
        .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
        .uID = 1
    End With
End Sub

解决方案 »

  1.   

    Steps To Create Sample Program:1、Start Visual Basic. If it is already running, go to the File menu and click New Project. 2、Place two CommandButtons and a common dialog box control on Form1. 3、Copy the following code to the Form1 code window: 
          'Declare a user-defined variable to pass to the Shell_NotifyIcon
          'function.
          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      'Declare the constants for the API function. These constants can be
          'found in the header file Shellapi.h.      'The following constants are the messages sent to the
          'Shell_NotifyIcon function to add, modify, or delete an icon from the
          'taskbar status area.
          Private 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.      'Left-click constants.
          Private Const WM_LBUTTONDBLCLK = &H203   'Double-click
          Private Const WM_LBUTTONDOWN = &H201     'Button down
          Private Const WM_LBUTTONUP = &H202       'Button up      'Right-click constants.
          Private Const WM_RBUTTONDBLCLK = &H206   'Double-click
          Private Const WM_RBUTTONDOWN = &H204     'Button down
          Private Const WM_RBUTTONUP = &H205       'Button up      'Declare the API function call.
          Private Declare Function Shell_NotifyIcon Lib "shell32" _
             Alias "Shell_NotifyIconA" _
             (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean      'Dimension a variable as the user-defined data type.
          Dim nid As NOTIFYICONDATA      Private Sub Command1_Click()
             'Click this button to add an icon to the taskbar status area.         'Set the individual values of the NOTIFYICONDATA data type.
             nid.cbSize = Len(nid)
             nid.hWnd = Form1.hWnd
             nid.uId = vbNull
             nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
             nid.uCallBackMessage = WM_MOUSEMOVE
             nid.hIcon = Form1.Icon
             nid.szTip = "Taskbar Status Area Sample Program" & vbNullChar         'Call the Shell_NotifyIcon function to add the icon to the taskbar
             'status area.
             Shell_NotifyIcon NIM_ADD, nid
          End Sub      Private Sub Command2_Click()
             'Click this button to delete the added icon from the taskbar
             'status area by calling the Shell_NotifyIcon function.
             Shell_NotifyIcon NIM_DELETE, nid
          End Sub      Private Sub Form_Load()
             'Set the captions of the command button when the form loads.
             Command1.Caption = "Add an Icon"
             Command2.Caption = "Delete Icon"
          End Sub      Private Sub Form_Terminate()
             'Delete the added icon from the taskbar status area when the
             'program ends.
             Shell_NotifyIcon NIM_DELETE, nid
          End Sub      Private Sub Form_MouseMove _
             (Button As Integer, _
              Shift As Integer, _
              X As Single, _
              Y As Single)
              'Event occurs when the mouse pointer is within the rectangular
              'boundaries of the icon in the taskbar status area.
              Dim msg As Long
              Dim sFilter As String
              msg = X / Screen.TwipsPerPixelX
              Select Case msg
                 Case WM_LBUTTONDOWN
                 Case WM_LBUTTONUP
                 Case WM_LBUTTONDBLCLK
                 CommonDialog1.DialogTitle = "Select an Icon"
                 sFilter = "Icon Files (*.ico)|*.ico"
                 sFilter = sFilter & "|All Files (*.*)|*.*"
                 CommonDialog1.Filter = sFilter
                 CommonDialog1.ShowOpen
                 If CommonDialog1.filename <> "" Then
                    Form1.Icon = LoadPicture(CommonDialog1.filename)
                    nid.hIcon = Form1.Icon
                    Shell_NotifyIcon NIM_MODIFY, nid
                 End If
                 Case WM_RBUTTONDOWN
                    Dim ToolTipString As String
                    ToolTipString = InputBox("Enter the new ToolTip:", _
                                      "Change ToolTip")
                    If ToolTipString <> "" Then
                       nid.szTip = ToolTipString & vbNullChar
                       Shell_NotifyIcon NIM_MODIFY, nid
                    End If
                 Case WM_RBUTTONUP
                 Case WM_RBUTTONDBLCLK
              End Select
          End Sub4、Press the F5 key to run the project or on the Tools menu, click Run Project. Click Add Icon to add an icon to the taskbar status area. Double-click the icon to change the icon. Right-click the icon to change the ToolTip string. Click Delete Icon to remove the icon from the taskbar status area. 这是MSDN上面的例子,你不妨看看
      

  2.   

    要像双击再打开界面这很难做到,一般是单击和右键单击。
    把楼上那位的代码:
            .uCallbackMessage = 0
    改为:
            .uCallbackMessage = WM_LBUTTONUP
    或者:
            .uCallbackMessage = WM_RBUTTONUP
    就可以回调了。
    在Form_MouseMove事件中对加上这样的代码:
    Me.WindowState=2就可以放大程序了。
      

  3.   

    TO:icyer() 
    双击可以做到的呀
      

  4.   

    谢谢各位的回复,我试过hhdsq(笨笨)的这一段代码,但是没有达到我的要求
    我是想达到瑞星防火墙哪样,当点击add_icon按钮时程序本身在桌面不可见,当双击任务栏上的这一个图标是,程序又可见。而你这一段代码还需要修改一点吧?
      

  5.   

    而阿木这一段代码,当双击任务栏这一个图标,程序没有反应。
    1、我想点击隐藏时form1.visuble=false;任务栏出现图标。
    2、当双击任务栏上的这一个图标是form1.visuble=true;
    3、当右键在任务栏上的这个图标上点击时弹出一个菜单,可以退出程序,任务栏的图标消失。
    4、当退出程序时任务栏的图示消失。
      

  6.   

    而阿木这一段代码,当双击任务栏这一个图标,程序没有反应。
    1、我想点击隐藏时form1.visuble=false;任务栏出现图标。
    2、当双击任务栏上的这一个图标是form1.visuble=true;
    3、当右键在任务栏上的这个图标上点击时弹出一个菜单,可以退出程序,任务栏的图标消失。
    4、当退出程序时任务栏的图示消失。 
      

  7.   

    这样吧,干脆用个OCX控件,方便一些.需要,留下依妹儿!
      

  8.   

    我不是很想用ocx控件,上面的一段程序我想应该是可以解决的。
    主要是: 1、在双击任务栏图标事件中添加一条语句,即:form1.visible=true;
            2、在右键点击任务栏图标事件中添加一条语句:popmenu...
    但具体怎么添加呢,请大侠帮忙。
     email:[email protected]
      

  9.   

    好了啦,我把我正在用的代码给你好了:
    ‘添加模块:
    Public Const NIM_ADD = &H0
    Public Const NIM_MODIFY = &H1
    Public Const NIM_DELETE = &H2
    Public Const WM_MOUSEMOVE = &H200
    Public Const NIF_MESSAGE = &H1
    Public Const NIF_ICON = &H2
    Public Const NIF_TIP = &H4Public Const WM_LBUTTONDBLCLK = &H203
    Public Const WM_LBUTTONDOWN = &H201
    Public Const WM_LBUTTONUP = &H202
    Public Const WM_RBUTTONDBLCLK = &H206
    Public Const WM_RBUTTONDOWN = &H204
    Public Const WM_RBUTTONUP = &H205
    Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean'在Form里Dim t As NOTIFYICONDATA          '托盘装载图标参数结构Dim Form_Flag As Boolean         '窗口是否已经显示
    '显示图标
    Private Sub Load_Ico()
        t.cbSize = Len(t)
        t.hWnd = Me.hWnd
        t.uId = 1&
        t.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        t.ucallbackMessage = WM_MOUSEMOVE
        t.hIcon = Me.Icon
        t.szTip = FrmCaption & Chr$(0)
        Shell_NotifyIcon NIM_ADD, t
    End Sub'卸载图标
    Private Sub UnLoad_Ico()
        t.cbSize = Len(t)
        t.hWnd = Me.hWnd
        t.uId = 1&
        Shell_NotifyIcon NIM_DELETE, t
    End Sub'鼠标在图标上双击事件
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Select Case CLng(X)
        Case WM_LBUTTONDBLCLK
            If Form_Flag Then
                Hide_Form
            Else
                Show_Form
            End If
        Case WM_LBUTTONDOWN
        Case WM_LBUTTONUP
        Case WM_RBUTTONDBLCLK
        Case WM_RBUTTONDOWN
            Me.PopupMenu nnn
        Case WM_RBUTTONUP
    End Select
    End Sub'显示窗口
    Private Sub Show_Form()
        '使激活
        'ForceForegroundWindow Me.hWnd     '此函数你先别用,如果这段代码调试通过,我再给你这个函数的具体代码,其作用是在你双击图标时,窗口显示为激活窗口
        Me.Show
        Me.WindowState = 0
        Form_Flag = True
    End Sub'隐藏窗口
    Private Sub Hide_Form()
        If Me.WindowState <> 1 Then
            Me.WindowState = 1
        End If
        Me.Hide
        Form_Flag = False
    End Sub