Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'Author:Vishal Kulkarni
'This code can be freely distributed and used by anyone.
'This is a real neat code and easy to understand
'This code puts your project icon in the system tray.
'When you close the application the icon in the systray
'is removed.
'This also gives various events to use.
'In case if you need any help please contact [email protected]
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 WM_MOUSEMOVE = &H200
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const WM_LBUTTONDBLCLK = &H203   'Double-click
Private Const WM_LBUTTONDOWN = &H201     'Button down
Private Const WM_LBUTTONUP = &H202       'Button up
Private Const WM_RBUTTONDBLCLK = &H206   'Double-click
Private Const WM_RBUTTONDOWN = &H204     'Button down
Private Const WM_RBUTTONUP = &H205       'Button up
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Dim nid As NOTIFYICONDATA
''Private Sub Form_Load()
''Open "c:\file.txt" For Output As #1
'''insert this code in timer event
''Dim i As Integer, key As Integer
''For i = 65 To 91
''key = GetAsyncKeyState(i)
''If key = -32767 Or key = -32768 Then
''Print #1, Chr(i)
''End If
''Next
'''insert this code in form's unload event
'''Yuo cannot see the text in a file until file is open
''Close #1
''End Sub
Private Sub Form_Load()
'If the application dosen't have a previous instance then load the form
If App.PrevInstance = False Then
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 = "Double Click To Restore Your application.." & vbNullChar
Shell_NotifyIcon NIM_ADD, nid
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lMsg As Long
Dim sFilter As String
lMsg = X / Screen.TwipsPerPixelX
Select Case lMsg
'you can play with other events as I did as per your use
Case WM_LBUTTONDOWN
Case WM_LBUTTONUP
Case WM_LBUTTONDBLCLK
Form1.WindowState = vbMaximized
Form1.Show
Case WM_RBUTTONDOWN
Case WM_RBUTTONUP
Case WM_RBUTTONDBLCLK
End Select
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Ok now this is the time to remove the icon from systray
Shell_NotifyIcon NIM_DELETE, nid
End
End Sub

解决方案 »

  1.   

    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_ICON = &H2
    Public Const NIF_TIP = &H4
    Public Const NIM_ADD = &H0
    Public Const NIF_MESSAGE = &H1
    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 NOTIFYICONDATA
    ' *********************************************
    ' The replacement window proc.
    ' *********************************************
    Public 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
            ' The user clicked on the tray icon.
            ' Look for click events.
            If lParam = WM_LBUTTONUP Then
                ' On left click, show the form.
                If TheForm.WindowState = vbMinimized Then _
                    TheForm.WindowState = TheForm.LastState
                TheForm.SetFocus
                Exit Function
            End If
            If lParam = WM_RBUTTONUP Then
                ' On right click, show the menu.
                TheForm.PopupMenu TheMenu
                Exit Function
            End If
        End If
        
        ' Send other messages to the original
        ' window proc.
        NewWindowProc = CallWindowProc( _
            OldWindowProc, hwnd, Msg, _
            wParam, lParam)
    End Function
    ' *********************************************
    ' Add the form's icon to the tray.
    ' *********************************************
    Public Sub AddToTray(frm As Form, mnu As Menu)
        ' ShowInTaskbar must be set to False at
        ' design time because it is read-only at
        ' run time.    ' Save the form and menu for later use.
        Set TheForm = frm
        Set TheMenu = mnu
        
        ' Install the new WindowProc.
        OldWindowProc = SetWindowLong(frm.hwnd, _
            GWL_WNDPROC, AddressOf NewWindowProc)
        
        ' Install the form's icon in the tray.
        With TheData
            .uID = 0
            .hwnd = frm.hwnd
            .cbSize = Len(TheData)
            .hIcon = frm.Icon.Handle
            .uFlags = NIF_ICON
            .uCallbackMessage = TRAY_CALLBACK
            .uFlags = .uFlags Or NIF_MESSAGE
            .cbSize = Len(TheData)
        End With
        Shell_NotifyIcon NIM_ADD, TheData
    End Sub
    ' *********************************************
    ' Remove the icon from the system tray.
    ' *********************************************
    Public Sub RemoveFromTray()
        ' Remove the icon from the tray.
        With TheData
            .uFlags = 0
        End With
        Shell_NotifyIcon NIM_DELETE, TheData
        
        ' Restore the original window proc.
        SetWindowLong TheForm.hwnd, GWL_WNDPROC, _
            OldWindowProc
    End Sub
    ' *********************************************
    ' Set a new tray tip.
    ' *********************************************
    Public Sub SetTrayTip(tip As String)
        With TheData
            .szTip = tip & vbNullChar
            .uFlags = NIF_TIP
        End With
        Shell_NotifyIcon NIM_MODIFY, TheData
    End Sub
    ' *********************************************
    ' Set a new tray icon.
    ' *********************************************
    Public Sub SetTrayIcon(pic As Picture)
        ' Do nothing if the picture is not an icon.
        If pic.Type <> vbPicTypeIcon Then Exit Sub    ' Update the tray icon.
        With TheData
            .hIcon = pic.Handle
            .uFlags = NIF_ICON
        End With
        Shell_NotifyIcon NIM_MODIFY, TheData
    End Sub
      

  2.   

    把图标放在WIN95的系统托盘中在系统托盘中的图标又被称作“提示图标”(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