欢迎光临
我们一直在努力

在托盘图标中添加气球提示-.NET教程,评论及其它

建站超值云服务器,限时71元/月

很多朋友都见到过能在托盘图标上出现气球提示的软件,不说软件,就是在“磁盘空间不足”时windows给出的提示就属于气球提示,那么怎样在自己的程序中添加这样的气球提示呢?

其实并不难,关键就在添加托盘图标时所使用的notifyicondata结构,源代码如下:

窗体模块:

option explicit

private declare function shell_notifyicon lib "shell32.dll" alias "shell_notifyicona" (byval dwmessage as long, lpdata as notifyicondata) as long

private 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 气球提示文本

utimeoutorversion as long 气球提示消失时间或版本

utimeout – 气球提示消失时间(单位:ms, 10000 — 30000)

uversion – 版本(0 for v4, 3 for v5)

szinfotitle as string * 64 气球提示标题

dwinfoflags as long 气球提示图标

end type

dwstate to notifyicondata structure

private const nis_hidden = &h1 隐藏图标

private const nis_sharedicon = &h2 共享图标

dwinfoflags to notifiicondata structure

private const niif_none = &h0 无图标

private const niif_info = &h1 "消息"图标

private const niif_warning = &h2 "警告"图标

private const niif_error = &h3 "错误"图标

uflags to notifyicondata structure

private const nif_icon as long = &h2

private const nif_info as long = &h10

private const nif_message as long = &h1

private const nif_state as long = &h8

private const nif_tip as long = &h4

dwmessage to shell_notifyicon

private const nim_add as long = &h0

private const nim_delete as long = &h2

private const nim_modify as long = &h1

private const nim_setfocus as long = &h3

private const nim_setversion as long = &h4

private sub form_load()

向托盘区添加图标

dim icondata as notifyicondata

dim title as string

title = "托盘程序" & vbnullchar

with icondata

.cbsize = len(icondata)

.hwnd = me.hwnd

.uid = 0

.uflags = nif_tip or nif_icon or nif_message or nif_info or nif_state

.ucallbackmessage = wm_notifyicon

.sztip = title

.hicon = me.icon.handle

.dwstate = 0

.dwstatemask = 0

.szinfo = "这是气球提示" & vbnullchar

.szinfotitle = title

.dwinfoflags = niif_info

.utimeoutorversion = 10000

end with

shell_notifyicon nim_add, icondata

prewndproc = setwindowlong(me.hwnd, gwl_wndproc, addressof windowproc)

end sub

private sub form_unload(cancel as integer)

删除托盘区图标

dim icondata as notifyicondata

with icondata

.cbsize = len(icondata)

.hwnd = me.hwnd

.uid = 0

.uflags = nif_tip or nif_icon or nif_message

.ucallbackmessage = wm_notifyicon

.sztip = "托盘程序"

.hicon = me.icon.handle

end with

shell_notifyicon nim_delete, icondata

setwindowlong me.hwnd, gwl_wndproc, prewndproc

卸载所有窗体

dim frm as form

for each frm in forms

unload frm

next

end sub

标准模块:

option explicit

public declare function setwindowlong lib "user32.dll" alias "setwindowlonga" (byval hwnd as long, byval nindex as long, byval dwnewlong as long) as long

private declare function callwindowproc lib "user32.dll" 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 const wm_rbuttonup = &h205

public const wm_user = &h400

public const wm_notifyicon = wm_user + 1 自定义消息

public const wm_lbuttondblclk = &h203

public const gwl_wndproc = (-4)

关于气球提示的自定义消息, 2000下不产生这些消息

public const nin_balloonshow = (wm_user + &h2) 当 balloon tips 弹出时执行

public const nin_balloonhide = (wm_user + &h3) 当 balloon tips 消失时执行(如 systrayicon 被删除),

但指定的 timeout 时间到或鼠标点击 balloon tips 后的消失不发送此消息

public const nin_balloontimeout = (wm_user + &h4) 当 balloon tips 的 timeout 时间到时执行

public const nin_balloonuserclick = (wm_user + &h5) 当鼠标点击 balloon tips 时执行。

注意:在xp下执行时 balloon tips 上有个关闭按钮,

如果鼠标点在按钮上将接收到 nin_balloontimeout 消息。

public prewndproc as long

form1 窗口入口函数

function windowproc(byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as long

拦截 wm_notifyicon 消息

if msg = wm_notifyicon then

select case lparam

case wm_rbuttonup

右键单击图标是运行这里的代码, 可以在这里添加弹出右键菜单的代码

case wm_lbuttondblclk

unload form1

case nin_balloonshow

debug.print "显示气球提示"

case nin_balloonhide

debug.print "删除托盘图标"

case nin_balloontimeout

debug.print "气球提示消失"

case nin_balloonuserclick

debug.print "单击气球提示"

end select

end if

windowproc = callwindowproc(prewndproc, hwnd, msg, wparam, lparam)

end function

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在托盘图标中添加气球提示-.NET教程,评论及其它
分享到: 更多 (0)

相关推荐

  • 暂无文章