Private m_Nfd As NOTIFYICONDATAPrivate Sub Form_Load() Dim lnPrevHwnd As Long
App.TaskVisible = False With m_Nfd .hIcon = Form1.Icon ‘任务栏的图标 .cbSize = Len(m_Nfd) .uCallbackMessage = WM_USER + 100 ’任务栏响应的消息 .uID = 0 .szTip = "Written by Jackyin" + Chr(0) ‘提示 .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP ’可以响应消息 .hwnd = Me.hwnd End WithShell_NotifyIcon NIM_ADD, m_Nfd ‘添加到任务栏 oldWndAdress = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf trayProc) ’将窗口处理消息的函数转换到trayProc,并获得窗口原来的地址 End Sub‘消息处理 函数,必须写在 模块里 Public Function trayProc(ByVal hwnd As Long, ByVal intMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long If intMsg = WM_USER + 100 Then Select Case lParam Case WM_LBUTTONDBLCLK Form1.Show Form1.WindowState = 0 Case WM_RBUTTONDOWN Form1.PopupMenu Form1.mnuMain End Select Else trayProc = CallWindowProc(oldWndAdress, hwnd, intMsg, wParam, lParam) End If End Function
以下是模块:module1 Public Const WM_MOUSEISMOVING = &H200 ' Mouse is moving Public Const WM_LBUTTONDOWN = &H201 'Button down Public Const WM_LBUTTONUP = &H202 'Button up Public Const WM_LBUTTONDBLCLK = &H203 'Double-click Public Const WM_RBUTTONDOWN = &H204 'Button down Public Const WM_RBUTTONUP = &H205 'Button up Public Const WM_RBUTTONDBLCLK = &H206 'Double-click Public Const WM_SETHOTKEY = &H32 ' The API Call Private Declare Function Shell_NotifyIcon Lib "shell32" _ Alias "Shell_NotifyIconA" _ (ByVal dwMessage As enm_NIM_Shell, pnid As NOTIFYICONDATA) As _ Boolean' User defined type required by Shell_NotifyIcon API call 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' This is an Enum that tells the API what to do... ' Constants required by Shell_NotifyIcon API call: Private Enum enm_NIM_Shell NIM_ADD = &H0 NIM_MODIFY = &H1 NIM_DELETE = &H2 NIF_MESSAGE = &H1 NIF_ICON = &H2 NIF_TIP = &H4 WM_MOUSEMOVE = &H200End EnumPrivate nidprogramdata As NOTIFYICONDATAPublic Sub AddIconToTray(ByVal Aform As Form) Dim nidprogramdata As NOTIFYICONDATA With nidprogramdata .cbSize = Len(nidprogramdata) .hwnd = Aform.hwnd .uId = vbNull .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE ' This is the event that will trigger when stuff happens .uCallbackMessage = WM_MOUSEMOVE .hIcon = Aform.Icon .szTip = "Lock Your System" & vbNullChar End With ' Call Notify... Shell_NotifyIcon NIM_ADD, nidprogramdata Aform.Hide End Sub 以下是窗体:form1.窗体有一菜单mMain private sub form_load() module1.AddIconToTray me end subPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As _ Single, Y As Single) On Error GoTo Form_MouseMove_err:
' This procedure receives the callbacks from the System Tray icon. Dim Result As Long Dim msg As Long
' The value of X will vary depending upon the scalemode setting If Me.ScaleMode = vbPixels Then msg = X Else msg = X / Screen.TwipsPerPixelX End If Select Case msg Case WM_LBUTTONUP ' process single click on your icon 'Call Command1_Click
Case WM_LBUTTONDBLCLK ' Process double click on your icon
Case WM_RBUTTONUP ' Usually display popup menu PopupMenu mMain Case WM_MOUSEISMOVING ' Do Somthing...
End Select Exit Sub
Form_MouseMove_err: ' Your Error handler goes here!
http://www.applevb.com/sourcecode/baricon.zip
Dim lnPrevHwnd As Long
App.TaskVisible = False
With m_Nfd
.hIcon = Form1.Icon ‘任务栏的图标
.cbSize = Len(m_Nfd)
.uCallbackMessage = WM_USER + 100 ’任务栏响应的消息
.uID = 0
.szTip = "Written by Jackyin" + Chr(0) ‘提示
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP ’可以响应消息
.hwnd = Me.hwnd
End WithShell_NotifyIcon NIM_ADD, m_Nfd ‘添加到任务栏
oldWndAdress = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf trayProc) ’将窗口处理消息的函数转换到trayProc,并获得窗口原来的地址
End Sub‘消息处理 函数,必须写在 模块里
Public Function trayProc(ByVal hwnd As Long, ByVal intMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If intMsg = WM_USER + 100 Then
Select Case lParam
Case WM_LBUTTONDBLCLK
Form1.Show
Form1.WindowState = 0
Case WM_RBUTTONDOWN
Form1.PopupMenu Form1.mnuMain
End Select
Else
trayProc = CallWindowProc(oldWndAdress, hwnd, intMsg, wParam, lParam)
End If
End Function
Public Const WM_MOUSEISMOVING = &H200 ' Mouse is moving
Public Const WM_LBUTTONDOWN = &H201 'Button down
Public Const WM_LBUTTONUP = &H202 'Button up
Public Const WM_LBUTTONDBLCLK = &H203 'Double-click
Public Const WM_RBUTTONDOWN = &H204 'Button down
Public Const WM_RBUTTONUP = &H205 'Button up
Public Const WM_RBUTTONDBLCLK = &H206 'Double-click
Public Const WM_SETHOTKEY = &H32
' The API Call
Private Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As enm_NIM_Shell, pnid As NOTIFYICONDATA) As _
Boolean' User defined type required by Shell_NotifyIcon API call
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' This is an Enum that tells the API what to do...
' Constants required by Shell_NotifyIcon API call:
Private Enum enm_NIM_Shell
NIM_ADD = &H0 NIM_MODIFY = &H1 NIM_DELETE = &H2 NIF_MESSAGE = &H1 NIF_ICON = &H2 NIF_TIP = &H4 WM_MOUSEMOVE = &H200End EnumPrivate nidprogramdata As NOTIFYICONDATAPublic Sub AddIconToTray(ByVal Aform As Form)
Dim nidprogramdata As NOTIFYICONDATA
With nidprogramdata
.cbSize = Len(nidprogramdata)
.hwnd = Aform.hwnd
.uId = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
' This is the event that will trigger when stuff happens
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = Aform.Icon
.szTip = "Lock Your System" & vbNullChar
End With ' Call Notify...
Shell_NotifyIcon NIM_ADD, nidprogramdata
Aform.Hide
End Sub
以下是窗体:form1.窗体有一菜单mMain
private sub form_load()
module1.AddIconToTray me
end subPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As _
Single, Y As Single) On Error GoTo Form_MouseMove_err:
' This procedure receives the callbacks from the System Tray icon.
Dim Result As Long
Dim msg As Long
' The value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg
Case WM_LBUTTONUP
' process single click on your icon
'Call Command1_Click
Case WM_LBUTTONDBLCLK
' Process double click on your icon
Case WM_RBUTTONUP
' Usually display popup menu
PopupMenu mMain
Case WM_MOUSEISMOVING
' Do Somthing...
End Select Exit Sub
Form_MouseMove_err:
' Your Error handler goes here!
End Sub