托盘技术!msdn中的例子: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 Global Const NIM_ADD = 0 Global Const NIM_MODIFY = 1 Global Const NIM_DELETE = 2 Global Const NIF_MESSAGE = 1 Global Const NIF_ICON = 2 Global Const NIF_TIP = 4 Declare Function Shell_NotifyIconA Lib "SHELL32" _ (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer The following code is a function that takes the parameters that need to be set for the NOTIFYICONDATA type and returns a variable of this type. Add to Form1: Private Function setNOTIFYICONDATA(hWnd As Long, ID As Long, _ Flags As Long, CallbackMessage As Long, Icon As Long, _ Tip As String) As NOTIFYICONDATA Dim nidTemp As NOTIFYICONDATA nidTemp.cbSize = Len(nidTemp) nidTemp.hWnd = hWnd nidTemp.uID = ID nidTemp.uFlags = Flags nidTemp.uCallbackMessage = CallbackMessage nidTemp.hIcon = Icon nidTemp.szTip = Tip & Chr$(0) setNOTIFYICONDATA = nidTemp End Function The three procedures in this block of code call the function created in step 5 to add, modify, and remove systray icons. Add this code to Form1 also: Private Sub Command1_Click() 'Add an icon. This procedure uses the icon specified in 'the Icon property of Form1. This can be modified as desired. Dim i As Integer Dim s As String Dim nid As NOTIFYICONDATA s = InputBox("Enter string:") nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _ ID:=vbNull, _ Flags:=NIF_MESSAGE Or NIF_ICON _ Or NIF_TIP, _ CallbackMessage:=vbNull, _ Icon:=Form1.Icon, _ Tip:=s) i = Shell_NotifyIconA(NIM_ADD, nid) End Sub Private Sub Command2_Click() 'Modify an existing icon. This procedure uses the icon 'specified in the Icon property of Form1. This can be modified 'as desired. Dim i As Integer Dim s As String Dim nid As NOTIFYICONDATA s = InputBox("Enter string:") nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _ ID:=vbNull, _ Flags:=NIF_MESSAGE Or NIF_ICON _ Or NIF_TIP, _ CallbackMessage:=vbNull, _ Icon:=Form1.Icon, _ Tip:=s) i = Shell_NotifyIconA(NIM_MODIFY, nid) End Sub Private Sub Command3_Click() 'Delete an existing icon. Dim i As Integer Dim nid As NOTIFYICONDATA nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _ ID:=vbNull, _ Flags:=NIF_MESSAGE Or NIF_ICON _ Or NIF_TIP, _ CallbackMessage:=vbNull, _ Icon:=Form1.Icon, _ Tip:="") i = Shell_NotifyIconA(NIM_DELETE, nid) End Sub
使用API:Shell_NotifyIcon 函 数(Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Long) ---- 该 函 数 给 系 统 发 送 添 加, 修 改, 删 除 任 务 栏 提 示 区 图 标 的 消 息, 系 统 根 据 发 送 的 消 息 进 行 相 应 的 处 理。 可 以 说 该 函 数 是 任 务 栏 提 示 区 编 程 的 核 心, 掌 握 它 就 可 以 轻 松 的 编 出 符 合 要 求 的 程 序 来。 下 面 先 对 该 函 数 中 的 参 数 解 释 如 下: ---- (1) 参 数dwMessage (ByVal dwMessage As Long) ---- 该 参 数 通 知 系 统 进 行 何 种 操 作, 取 值 如 下: NIM_ADD 添加图标到任务栏提示区 NIM_DELETE 删除图标 NIM_MODIFY 发送图标特性已改变的消息---- (2) 参 数pnid(pnid As NOTIFYICONDATA) ---- 存 储 图 标 特 性 数 据,NOTIFYICONDATA 定 义 如 下: Private Type NOTIFYICONDATA CbSize As Long '该数据结构的大小 hWnd As Long '处理图标通知消息的窗口句柄 uID As Long '应用程序定义的图标号 uFlags As Long '决定图标的运行特性,一般取组合 'NIF_ICON Or NIF_TIP Or NIF_MESSAGE '其意义为显示图标及提示,并对消息进行处理 uCallbackMessage As Long '消息处理回调函数 hIcon As Long '图标句柄 szTip As String * 64 '图标提示 End Type 具体的实现跟楼上差不多。一定行地。。
我的老天,我又要 COPY/PASTE 了! 这个问题已经有人问过多次,可能搜索起来太困难,把我的代码关键部分贴出来吧:Windows 相关 API 声明:Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Public Declare 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自定义消息常量和全局变量: Public g_lPrevProc As LongPublic Const WM_SYSTRAYMESSAGE = WM_USER + 1975FORM 初始化:Private Sub Form_Load() g_lPrevProc = SetWindowLong(frmMain.hwnd, GWL_WNDPROC, AddressOf WindowProc) End SubWindowProc:Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long if uMsg = WM_SYSTRAYMESSAGE Then If lParam = WM_LBUTTONUP Then DeleteSysTrayIcon hwnd frmMain.WindowState = vbNormal frmMain.Show ElseIf lParam = WM_RBUTTONUP Then SetForegroundWindow frmMain.hwnd frmMain.PopupMenu frmMain.mnuPopup ElseIf lParam = WM_MOUSEMOVE Then If Not g_bMouseMove Then g_bMouseMove = True g_strTipText = Format$(Now, "mm/dd/yyyy hh:nn:ss") ModifySysTrayTip frmMain.hwnd, g_strTipText End If End If Else WindowProc = CallWindowProc(g_lPrevProc, hwnd, uMsg, wParam, lParam) End If End FunctionSYSTRAY 函数:Public Sub ModifySysTrayTip(hwnd As Long, strTipText As String) Dim stNotify As NOTIFYICONDATA
With stNotify .cbSize = Len(stNotify) .hwnd = hwnd .szTip = strTipText & vbNullChar .uFlags = NIF_TIP End With Call Shell_NotifyIcon(NIM_MODIFY, stNotify) End SubPublic Sub AddSysTrayIcon(hwnd As Long, hIcon As Long, strTipText As String) Dim stNotify As NOTIFYICONDATA
With stNotify .cbSize = Len(stNotify) .hIcon = hIcon .hwnd = hwnd .szTip = strTipText & vbNullChar .uCallbackMessage = WM_SYSTRAYMESSAGE .uFlags = NIF_ICON + NIF_TIP + NIF_MESSAGE End With Call Shell_NotifyIcon(NIM_ADD, stNotify) End SubPublic Sub DeleteSysTrayIcon(hwnd As Long) Dim stNotify As NOTIFYICONDATA
With stNotify .cbSize = Len(stNotify) .hwnd = hwnd End With Call Shell_NotifyIcon(NIM_DELETE, stNotify) End Sub仅供参考。
注意如果你用vb,API Viewer 里面的声明的别名前有空格。这个空格曾经害了我
2个小时。
http://support.microsoft.com/support/kb/articles/Q176/0/85.ASP - 微软全球技术中心 VB技术支持本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type Global Const NIM_ADD = 0
Global Const NIM_MODIFY = 1
Global Const NIM_DELETE = 2
Global Const NIF_MESSAGE = 1
Global Const NIF_ICON = 2
Global Const NIF_TIP = 4 Declare Function Shell_NotifyIconA Lib "SHELL32" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer
The following code is a function that takes the parameters that need to be set for the NOTIFYICONDATA type and returns a variable of this type. Add to Form1: Private Function setNOTIFYICONDATA(hWnd As Long, ID As Long, _
Flags As Long, CallbackMessage As Long, Icon As Long, _
Tip As String) As NOTIFYICONDATA Dim nidTemp As NOTIFYICONDATA nidTemp.cbSize = Len(nidTemp)
nidTemp.hWnd = hWnd
nidTemp.uID = ID
nidTemp.uFlags = Flags
nidTemp.uCallbackMessage = CallbackMessage
nidTemp.hIcon = Icon
nidTemp.szTip = Tip & Chr$(0) setNOTIFYICONDATA = nidTemp
End Function
The three procedures in this block of code call the function created in step 5 to add, modify, and remove systray icons. Add this code to Form1 also: Private Sub Command1_Click()
'Add an icon. This procedure uses the icon specified in
'the Icon property of Form1. This can be modified as desired. Dim i As Integer
Dim s As String
Dim nid As NOTIFYICONDATA s = InputBox("Enter string:")
nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:=s) i = Shell_NotifyIconA(NIM_ADD, nid)
End Sub Private Sub Command2_Click()
'Modify an existing icon. This procedure uses the icon
'specified in the Icon property of Form1. This can be modified
'as desired. Dim i As Integer
Dim s As String
Dim nid As NOTIFYICONDATA s = InputBox("Enter string:")
nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:=s) i = Shell_NotifyIconA(NIM_MODIFY, nid)
End Sub Private Sub Command3_Click()
'Delete an existing icon. Dim i As Integer
Dim nid As NOTIFYICONDATA nid = setNOTIFYICONDATA(hWnd:=Form1.hWnd, _
ID:=vbNull, _
Flags:=NIF_MESSAGE Or NIF_ICON _
Or NIF_TIP, _
CallbackMessage:=vbNull, _
Icon:=Form1.Icon, _
Tip:="") i = Shell_NotifyIconA(NIM_DELETE, nid)
End Sub
使用API:Shell_NotifyIcon 函 数(Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Long)
---- 该 函 数 给 系 统 发 送 添 加, 修 改, 删 除 任 务 栏 提 示 区 图 标 的 消 息, 系 统 根 据 发 送 的 消 息 进 行 相 应 的 处 理。 可 以 说 该 函 数 是 任 务 栏 提 示 区 编 程 的 核 心, 掌 握 它 就 可 以 轻 松 的 编 出 符 合 要 求 的 程 序 来。 下 面 先 对 该 函 数 中 的 参 数 解 释 如 下: ---- (1) 参 数dwMessage (ByVal dwMessage As Long) ---- 该 参 数 通 知 系 统 进 行 何 种 操 作, 取 值 如 下: NIM_ADD 添加图标到任务栏提示区
NIM_DELETE 删除图标
NIM_MODIFY 发送图标特性已改变的消息---- (2) 参 数pnid(pnid As NOTIFYICONDATA)
---- 存 储 图 标 特 性 数 据,NOTIFYICONDATA 定 义 如 下: Private Type NOTIFYICONDATA
CbSize As Long '该数据结构的大小
hWnd As Long '处理图标通知消息的窗口句柄
uID As Long '应用程序定义的图标号
uFlags As Long '决定图标的运行特性,一般取组合
'NIF_ICON Or NIF_TIP Or NIF_MESSAGE
'其意义为显示图标及提示,并对消息进行处理
uCallbackMessage As Long '消息处理回调函数
hIcon As Long '图标句柄
szTip As String * 64 '图标提示
End Type
具体的实现跟楼上差不多。一定行地。。
这个问题已经有人问过多次,可能搜索起来太困难,把我的代码关键部分贴出来吧:Windows 相关 API 声明:Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare 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自定义消息常量和全局变量:
Public g_lPrevProc As LongPublic Const WM_SYSTRAYMESSAGE = WM_USER + 1975FORM 初始化:Private Sub Form_Load()
g_lPrevProc = SetWindowLong(frmMain.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End SubWindowProc:Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
if uMsg = WM_SYSTRAYMESSAGE Then
If lParam = WM_LBUTTONUP Then
DeleteSysTrayIcon hwnd
frmMain.WindowState = vbNormal
frmMain.Show
ElseIf lParam = WM_RBUTTONUP Then
SetForegroundWindow frmMain.hwnd
frmMain.PopupMenu frmMain.mnuPopup
ElseIf lParam = WM_MOUSEMOVE Then
If Not g_bMouseMove Then
g_bMouseMove = True
g_strTipText = Format$(Now, "mm/dd/yyyy hh:nn:ss")
ModifySysTrayTip frmMain.hwnd, g_strTipText
End If
End If
Else
WindowProc = CallWindowProc(g_lPrevProc, hwnd, uMsg, wParam, lParam)
End If
End FunctionSYSTRAY 函数:Public Sub ModifySysTrayTip(hwnd As Long, strTipText As String)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hwnd = hwnd
.szTip = strTipText & vbNullChar
.uFlags = NIF_TIP
End With
Call Shell_NotifyIcon(NIM_MODIFY, stNotify)
End SubPublic Sub AddSysTrayIcon(hwnd As Long, hIcon As Long, strTipText As String)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hIcon = hIcon
.hwnd = hwnd
.szTip = strTipText & vbNullChar
.uCallbackMessage = WM_SYSTRAYMESSAGE
.uFlags = NIF_ICON + NIF_TIP + NIF_MESSAGE
End With
Call Shell_NotifyIcon(NIM_ADD, stNotify)
End SubPublic Sub DeleteSysTrayIcon(hwnd As Long)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hwnd = hwnd
End With
Call Shell_NotifyIcon(NIM_DELETE, stNotify)
End Sub仅供参考。