| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1440 人关注过本帖
标题:[经验][原创][分享]WindowsXP的托盘使用
只看楼主 加入收藏
ryu
Rank: 1
等 级:新手上路
帖 子:124
专家分:0
注 册:2006-2-12
收藏
 问题点数:0 回复次数:5 
[经验][原创][分享]WindowsXP的托盘使用

Windows XP的托盘使用,绝对厚道.
模块:

Option Explicit
Public OldWindowProc As Long
Public TheForm As Form
Public TheMenu As Menu
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 Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Const WM_USER = &H400
Public Const WM_LBUTTONUP = &H202
Public Const WM_MBUTTONUP = &H208
Public Const WM_RBUTTONUP = &H205
Public Const WM_NOTIFYICON = WM_USER + 1
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 NIF_INFO = &H10
Public Const NIF_STATE = &H8
Public Const NIF_MESSAGE = &H1
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIM_SETFOCUS As Long = &H3
Public Const NIM_SETVERSION As Long = &H4
Public Const NIS_HIDDEN = &H1 ' 隐藏图标
Public Const NIS_SHAREDICON = &H2 ' 共享图标
Public Const NIIF_NONE = &H0 ' 无图标
Public Const NIIF_INFO = &H1 ' 消息图标
Public Const NIIF_WARNING = &H2 ' 警告图标
Public Const NIIF_ERROR = &H3 ' 错误图标
Public Const NIN_BALLOONUSERCLICK = (WM_USER + &H5)
Public Type NOTIFYICONDATA
cbSize As Long ' 结构大小(字节)
hwnd As Long ' 处理消息的窗口的句柄
uId As Long ' 唯一的标识符
uFlags As Long ' Flags
uCallBackMessage As Long ' 处理消息的窗口接收的消息
hIcon As Long ' 托盘图标句柄
szTip As String * 128 ' Tooltip 提示文本
dwState As Long ' 托盘图标状态
dwStateMask As Long ' 状态掩码
szInfo As String * 256 ' 气球提示文本
' 气球提示消失时间(单位:ms, 10000 -- 30000)
uTimeoutOrVersion As Long ' 版本(0 for V4, 3 for V5)
szInfoTitle As String * 64 ' 气球提示标题
dwInfoFlags As Long ' 气球提示图标
End Type
Public TheData As NOTIFYICONDATA

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
If lParam = WM_LBUTTONUP Then '点击左键
'添加代码
Exit Function
ElseIf lParam = WM_RBUTTONUP Then '点击右键
'添加代码
Exit Function
ElseIf lParam = NIN_BALLOONUSERCLICK Then '点击气球
'添加代码
Exit Function
End If
End If
NewWindowProc = CallWindowProc(OldWindowProc, hwnd, msg, wParam, lParam)
End Function

Public Sub AddToTray(Frm As Form, mnu As Menu) '添加图标
Set TheForm = Frm
Set TheMenu = mnu
OldWindowProc = SetWindowLong(Frm.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)
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

Public Sub RemoveFromTray() '移除图标
With TheData
.uFlags = 0
End With
Shell_NotifyIcon NIM_DELETE, TheData
SetWindowLong TheForm.hwnd, GWL_WNDPROC, OldWindowProc
End Sub

Public Sub SetTrayTip(tip As String) '添加提示
With TheData
.szTip = tip & vbNullChar
.uFlags = NIF_TIP
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub

Public Sub SetTrayIcon(pic As Picture) '更换图标
With TheData
.hIcon = pic.Handle
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub

Public Sub SetTrayBalloon(NIIFIcon As Long, Info As String, Title As String) '添加气球提示
With TheData
.uId = 0
.cbSize = Len(TheData)
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE Or NIF_INFO Or NIF_STATE
.uCallBackMessage = TRAY_CALLBACK
.dwState = 0
.dwStateMask = 0
.szInfoTitle = Info & vbNullChar
.szInfo = Title & vbNullChar
.dwInfoFlags = NIIFIcon
.uTimeoutOrVersion = 10000
End With
Shell_NotifyIcon NIM_MODIFY, TheData
End Sub
窗体:自己引用.

搜索更多相关主题的帖子: WindowsXP Long ByVal 托盘 Public 
2006-03-22 18:08
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
不错..收藏了..

我的msn: myfend@
2006-03-22 18:26
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
在这里提醒一句..在VB自带的API浏览器里..添加Shell_NotifyIcon函数有一个bug,
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
以上的正确的..
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias " Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
这是错误的...

关键就在于..别名它写错了..是
"Shell_NotifyIconA"
而不是这个.
" Shell_NotifyIconA"

大家注意了..

我的msn: myfend@
2006-03-22 19:29
lrd1986
Rank: 1
等 级:新手上路
帖 子:111
专家分:0
注 册:2007-9-11
收藏
得分:0 
能不能详细说说如何使用左右键?

例如单击左键显示主界面,单击右键可以选择功能

谢谢啦

走自己的路,让人说去
2007-09-21 11:48
随风逐流
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:passerby
等 级:版主
威 望:8
帖 子:4054
专家分:271
注 册:2007-6-13
收藏
得分:0 

这个事件不全面,没我上次发的全面


[url=http://www./html/6/6694/]极道金丹[/url][url=http://www./html/2/2849/]九阴九阳[/url][url=http://www./html/2/2596/]凡人修仙传[/url]
2007-09-21 12:09
lrd1986
Rank: 1
等 级:新手上路
帖 子:111
专家分:0
注 册:2007-9-11
收藏
得分:0 
回复:(随风逐流)这个事件不全面,没我上次发的全面
可以给你的我看看吗?

走自己的路,让人说去
2007-09-21 12:36
快速回复:[经验][原创][分享]WindowsXP的托盘使用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078870 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved