把图标放在系统托盘中
 
 在系统托盘中的图标又被称作“提示图标”(NotifyIcon)。这个小小的图标不仅可以启动程序,还可用来显示程序运行的状态。一个 hWnd被用来接收鼠标在提示图标上时所产生的事件所发出的消息。但如果用此方法,可能会触发其它控件的MouseMove 事件。在本例中,使用一个PictureBox来接收来自鼠标的消息。真正的事件被包含在MouseMove事件中的X参数中,然后通过代码来I 提取每一个事件。同时你还必须指定要显示图标的句柄。使用PictureBox的DragIcon属性可以轻松地实现。为了产生一个提示图标,你必须首先将图标的有关信息储存在一个自定义类型变量中。然后调用Shell_NotifyA。当创建了一个提示图标 后所有的鼠标事件将触发在PictureBox上的MouseMove事件。请注意在程序结束后删除提示图标。首先在你的窗体上放置一个PictureBox。本例假设该控件名为Picture1。将其Visible属性设为False。将其DragIcon属性设定为你想要显示的To 图标文件。使用下面的代码在Form_Load事件中创建提示图标。在Form_Unload事件中删除提示图标。把下面的代码放在一模块的声明段中。DefInt A-ZDeclare Function Shell_NotifyIconA Lib "SHELL32" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As LongType 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' commands & flags for NotifyIcons
Global Const NIM_ADD = &H0&
Global Const NIM_MODIFY = &H1
Global Const NIM_DELETE = &H2
Global Const NIF_MESSAGE = &H1
Global Const NIF_ICON = &H2
Global Const NIF_TIP = &H4
Global Const WM_MOUSEMOVE = &H200
Global NI as NOTIFYICONDATA创建提示图标将下面的代码放在Form_Load事件中以产生一个提示图标。所有的鼠标事件都将会传递到PictureBox的MouseMove事件中。' stock NOTIFYICONDATA structure
NI.cbSize = Len(NI) 'length of this structure       
NI.hWnd =Picture1.hwnd 'control to receive messages      
NI.uID = 0 ' uniqueID
NI.uFlags = NIF_MESSAGE or NIF_ICON or NIF_TIP ' Operation Flags
NI.uCallbackMessage = WM_MOUSEMOVE ' message to send to control 
NI.hIcon = Picture1.DragIcon ' handle to Icon
NI.szTip = "My Tool Tip" + Chr$(0) ' Tool Tip ' 必须给提示图标分配一个唯一的ID号
' so increment until creation is successfulDo
    NI.uID = NI.uID + 1
    result = Shell_NotifyIconA(NIM_ADD, NI)
Loop While result = 0修改提示图标Modifying NOTIFYICON下面的例子可以修改图标NI.hIcon = Picture2.DragIcon
NI.szTip = "Different Tool Tip" + Chr$(0)            
' modifies an existing NotifyIcon      
result = Shell_NotifyIconA(NIM_MODIFY, NI)删除提示图标Deleting NOTIFYICON将下面的代码放在Form_Unload事件中' 删除已有的提示图标
result = Shell_NotifyIconA(NIM_DELETE, NI)下面的代码放在 PictureBox的MouseMove事件中' 从提示图标接收消息
' 消息通过X参数传递Dim Msg as Long
     Msg = (X And &HFF) * &H100
     Select Case Msg           Case 0 ' 鼠标移动
                 ' 在此输入你的代码           Case &HF00 ' 鼠标左键被按下
                 ' 在此输入你的代码           Case &H1E00 ' 
                 ' 在此输入你的代码            Case &H2D00 ' 双击鼠标左键
                 ' 在此输入你的代码            Case &H3C00 ' 鼠标右键被按下
' 在此输入你的代码            Case &H4B00 ' 鼠标右键弹起
' 在此输入你的代码            Case &H5A00 ' 双击鼠标右键
                 ' 在此输入你的代码     End Select
 
   
 
  
 

解决方案 »

  1.   

    http://www.21code.com/school/?pos=view&id=830
    用VB实现托盘动画图标 
    http://www.21code.com/school/?pos=view&id=1493
    托盘程序详解(一) 
    http://www.21code.com/school/?pos=view&id=1492
    托盘程序详解(二) 
    http://www.21code.com/school/?pos=view&id=737
    用VISUAL BASIC编写托盘程序
      

  2.   

    使用添加系统托盘的API函数!
    '申明
    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
    Private Const NIF_MESSAGE = &H1
    Private Const NIF_ICON = &H2
    Private Const NIF_TIP = &H4Private Const WM_LBUTTONDBLCLK = &H203
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_RBUTTONUP = &H205Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As BooleanDim ty_NO As NOTIFYICONDATA
    '窗口操作
    Private Sub Form_Load()
        ty_NO.cbSize = Len(ty_NO)
        ty_NO.hwnd = frmCon.hwnd
        ty_NO.uId = 1&
        ty_NO.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        ty_NO.ucallbackMessage = WM_LBUTTONDOWN
        ty_NO.hIcon = Me.Icon
        ty_NO.szTip = "数据更新...." & Chr$(0)
        Shell_NotifyIcon NIM_ADD, ty_NOEnd Sub
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
        Msg = x / Screen.TwipsPerPixelX
        If Msg = WM_LBUTTONDBLCLK Then
            'Left button double click
            mnuPop_Click 0
        ElseIf Msg = WM_RBUTTONUP Then
            'Right button click
            PopupMenu mnuPopup
            
        End IfEnd SubPrivate Sub Form_Unload(Cancel As Integer)
        'remove the icon
        TrayI.cbSize = Len(TrayI)
        TrayI.hwnd = Form1.hwnd
        TrayI.uId = 1&
        Shell_NotifyIcon NIM_DELETE, TrayI
        End
    End Sub
      

  3.   

    'Download the full source+pictures+... at http://www.allapi.net/php/redirect/redirect.php?action=download&id=38
    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
    Private Const NIF_MESSAGE = &H1
    Private Const NIF_ICON = &H2
    Private Const NIF_TIP = &H4Private Const WM_LBUTTONDBLCLK = &H203
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_RBUTTONUP = &H205Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
    Dim TrayI As NOTIFYICONDATA
    Private Sub Form_Load()
        TrayI.cbSize = Len(TrayI)
        'Set the window's handle (this will be used to hook the specified window)
        TrayI.hWnd = pichook.hWnd
        'Application-defined identifier of the taskbar icon
        TrayI.uId = 1&
        'Set the flags
        TrayI.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        'Set the callback message
        TrayI.ucallbackMessage = WM_LBUTTONDOWN
        'Set the picture (must be an icon!)
        TrayI.hIcon = imgIcon(2).Picture
        'Set the tooltiptext
        TrayI.szTip = "Recent" & Chr$(0)
        'Create the icon
        Shell_NotifyIcon NIM_ADD, TrayI    Me.Hide
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'remove the icon
        TrayI.cbSize = Len(TrayI)
        TrayI.hWnd = pichook.hWnd
        TrayI.uId = 1&
        Shell_NotifyIcon NIM_DELETE, TrayI
        End
    End Sub
    Private Sub mnuPop_Click(Index As Integer)
        Select Case Index
            Case 0
                MsgBox "KPD-Team 1998" + Chr$(13) + "URL: http://www.allapi.net/" + Chr$(13) + "E-Mail: [email protected]", vbInformation + vbOKOnly
            Case 2
                Unload Me
        End Select
    End Sub
    Private Sub pichook_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Msg = X / Screen.TwipsPerPixelX
        If Msg = WM_LBUTTONDBLCLK Then
            'Left button double click
            mnuPop_Click 0
        ElseIf Msg = WM_RBUTTONUP Then
            'Right button click
            Me.PopupMenu mnuPopUp
        End If
    End Sub
    Private Sub Timer1_Timer()
        Static Tek As Integer
        'Animate the icon
        Me.Icon = imgIcon(Tek).Picture
        TrayI.hIcon = imgIcon(Tek).Picture
        Tek = Tek + 1
        If Tek = 3 Then Tek = 0
        Shell_NotifyIcon NIM_MODIFY, TrayI
    End Sub
      

  4.   

    使用控件吧
    http://www.sxnw.gov.cn/personal/vbworld/source/openfile.asp?kind=api&id=24&filename=TraySample.zip