首页 > 编程 > .NET > 正文

VB.NET编程之托盘程序篇

2024-07-10 13:01:06
字体:
来源:转载
供稿:网友
菜鸟学堂:
本文程序的设计及运行环境

(1)windows 2000 service

(2)net framework sdk 正式版



静态托盘程序的编写过程


所谓静态托盘程序是指程序运行后,在系统托盘区的图标处于静止状态的托盘程序。动态托盘程序正好与之相反,它是指在系统托盘区图标呈现动画效果的一类托盘程序。下面就来探讨一下vb.net是如何实现静态托盘程序。

.net framework sdk为编写托盘程序提供了一个组件:notifyicon组件。notifyicon组件是一个winform组件,位于命名空间"system.windows.forms"中,在vb.net程序中,只要创建一个notifyicon组件的实例,并且对notifyicon实例的"icon"属性赋值,这样一个简单的托盘程序就完成了。下面就是这个简单托盘程序对于的代码(form1.vb):

public class form1
inherits system.windows.forms.form
#region " windows 窗体设计器生成的代码 "
public sub new ( )
mybase.new ( )
'该调用是 windows 窗体设计器所必需的。
initializecomponent ( )
'在 initializecomponent ( ) 调用之后添加任何初始化
end sub
'窗体重写处置以清理组件列表。
protected overloads overrides sub dispose ( byval disposing as boolean )
if disposing then
if not ( components is nothing ) then
components.dispose ( )
end if
end if
mybase.dispose ( disposing )
end sub
'windows 窗体设计器所必需的
private components as system.componentmodel.icontainer
'注意:以下过程是 windows 窗体设计器所必需的
'可以使用 windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
friend withevents notifyicon1 as system.windows.forms.notifyicon
'创建一个notifyicon实例
friend trayicon = new icon ( "tray.ico" )
'创建一个icon实例
<system.diagnostics.debuggerstepthrough ( ) >
private sub initializecomponent ( )
me.components = new system.componentmodel.container ( )
me.notifyicon1 = new system.windows.forms.notifyicon ( me.components )
me.notifyicon1.text = "notifyicon1"
me.notifyicon1.visible = true
'对notifyicon实例的icon属性赋值,完成简单托盘程序
me.notifyicon1.icon = trayicon
me.autoscalebasesize = new system.drawing.size ( 6 , 14 )
me.clientsize = new system.drawing.size ( 292 , 273 )
me.name = "form1"
me.text = "form1"
end sub
#end region
end class
'启动程序
module module1
sub main ( )
application.run ( new form1 ( ) )
end sub
end module



但是这个托盘程序还不是真正意义上的托盘程序,因为它还有很多具体功能没有实现,下面就列出这些功能,并介绍具体的实现方法

(1).托盘程序是隐藏窗口后,程序不应该显示在任务栏中,并且一般运行后都不显示窗口:

这是通过设定窗体的属性来完成的,具体如下:

'设定程序不应该显示在任务栏
me.showintaskbar = false
'设定程序运行后最小化
me.windowstate = system.windows.forms.formwindowstate.minimized



(2).定义托盘程序中的菜单和相关事件:

往notifyicon实例中添加菜单,首先要创建contextmenu实例,此实例主要作用是表示快捷菜单,其中的菜单项是通过创建menuitem实例来实现,托盘程序中的菜单有几个菜单项,就创建几个menuitem实例。然后把这些菜单项加入到contextmenu实例,并把此实例赋值给notifyicon实例的contextmenu属性,这样托盘程序右键点击弹出的菜单就完成了。下面是具体代码:

创建contextmenu实例和menuitem实例:
friend withevents contextmenu1 as system.windows.forms.contextmenu
friend withevents menuitem1 as system.windows.forms.menuitem
friend withevents menuitem2 as system.windows.forms.menuitem
friend withevents menuitem3 as system.windows.forms.menuitem



把这些菜单项加入到contextmenu实例,并把contextmenu实例赋值给notifyicon实例的contextmenu属性:

me.menuitem1 = new system.windows.forms.menuitem ( )
me.menuitem2 = new system.windows.forms.menuitem ( )
me.menuitem3 = new system.windows.forms.menuitem ( )
me.notifyicon1.contextmenu = me.contextmenu1
me.notifyicon1.text = "vb.net的托盘程序"
me.notifyicon1.visible = true
'设定托盘程序托盘区位置显示图标
me.notifyicon1.icon = trayicon
'在contextmenu实例中加入菜单项
me.contextmenu1.menuitems.add ( me.menuitem1 )
me.contextmenu1.menuitems.add ( me.menuitem2 )
me.contextmenu1.menuitems.add ( me.menuitem3 )
me.menuitem1.index = 0
me.menuitem1.text = "显示窗体"
me.menuitem2.index = 1
me.menuitem2.text = "隐藏窗体"
me.menuitem3.index = 2
me.menuitem3.text = "退出"



当把contextmenu实例赋值给notifyicon实例的contextmenu属性后,托盘程序的缺省状态是当鼠标右击托盘图标,就会弹出对应的菜单。这时就可以对其中的每一个菜单项定义相应的事件以及具体的处理方法。一个完整的静态托盘程序的编写过程就完成了。

最后要请读者注意的是,由于本文中的托盘程序的图标并不是通过创建资源文件来实现的,而是通过创建icon实例完成的。所以在程序运行的时候,必须在程序的当前目录存在一个图标文件,并且此图标文件的名称为"tray.ico"。下面是这个静态托盘程序的完整的代码清单
(form2.vb):
public class form1
inherits system.windows.forms.form
#region " windows 窗体设计器生成的代码 "
public sub new ( )
mybase.new ( )
'该调用是 windows 窗体设计器所必需的。
initializecomponent ( )
'在 initializecomponent ( ) 调用之后添加任何初始化
end sub
'窗体重写处置以清理组件列表。
protected overloads overrides sub dispose ( byval disposing as boolean )
if disposing then
if not ( components is nothing ) then
components.dispose ( )
end if
end if
mybase.dispose ( disposing )
end sub
'windows 窗体设计器所必需的
private components as system.componentmodel.icontainer
'注意:以下过程是 windows 窗体设计器所必需的
'可以使用 windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
friend withevents notifyicon1 as system.windows.forms.notifyicon
friend withevents contextmenu1 as system.windows.forms.contextmenu
friend withevents menuitem1 as system.windows.forms.menuitem
friend withevents menuitem2 as system.windows.forms.menuitem
friend withevents menuitem3 as system.windows.forms.menuitem
friend trayicon = new icon ( "tray.ico" )
<system.diagnostics.debuggerstepthrough ( ) >
private sub initializecomponent ( )
me.components = new system.componentmodel.container ( )
me.notifyicon1 = new system.windows.forms.notifyicon ( me.components )
me.contextmenu1 = new system.windows.forms.contextmenu ( )
me.menuitem1 = new system.windows.forms.menuitem ( )
me.menuitem2 = new system.windows.forms.menuitem ( )
me.menuitem3 = new system.windows.forms.menuitem ( )
me.notifyicon1.contextmenu = me.contextmenu1
me.notifyicon1.text = "vb.net的托盘程序"
me.notifyicon1.visible = true
'设定托盘程序托盘区位置显示图标
me.notifyicon1.icon = trayicon
'在contextmenu实例中加入菜单项
me.contextmenu1.menuitems.add ( me.menuitem1 )
me.contextmenu1.menuitems.add ( me.menuitem2 )
me.contextmenu1.menuitems.add ( me.menuitem3 )
me.menuitem1.index = 0
me.menuitem1.text = "显示窗体"
me.menuitem2.index = 1
me.menuitem2.text = "隐藏窗体"
me.menuitem3.index = 2
me.menuitem3.text = "退出"
me.autoscalebasesize = new system.drawing.size ( 6 , 14 )
me.clientsize = new system.drawing.size ( 292 , 273 )
me.name = "form1"
'设定程序不应该显示在任务栏
me.showintaskbar = false
me.text = "vb.net之winform编程托盘程序篇"
'设定程序运行后最小化
me.windowstate = system.windows.forms.formwindowstate.minimized
end sub
#end region
'显示托盘程序窗口
private sub menuitem1_click ( byval sender as system.object ,
byval e as system.eventargs ) handles menuitem1.click
me.windowstate = formwindowstate.normal
me.show ( )
end sub
'隐藏托盘程序窗口
private sub menuitem2_click ( byval sender as object ,
byval e as system.eventargs ) handles menuitem2.click
me.hide ( )
end sub
'推出托盘程序窗口
private sub menuitem3_click ( byval sender as object ,
byval e as system.eventargs ) handles menuitem3.click
notifyicon1.dispose ( )
application.exit ( )
end sub
'双击图标显示窗体
private sub notifyicon1_doubleclick ( byval sender as object ,
byval e as system.eventargs ) handles notifyicon1.doubleclick
me.windowstate = formwindowstate.normal
me.show ( )
end sub
end class
'启动程序
module module1
sub main ( )
application.run ( new form1 ( ) )
end sub
end module

form2.vb经过了下列命令编译、连接后:

vbc /r:system.dll /r:system.windows.froms.dll /r:system.drawing.dll form2.vb

就可以得到form2.exe,下图是from2.exe运行的界面:








图01:托盘程序运行界面01




动态托盘程序的编写过程


动态托盘程序中的托盘图标之所以能够呈现动画效果,是因为程序中的一个定时器组

件每隔一段时间都不断切换托盘图标。本文是通过二个图标的切换来表现动态效果的,读者如果有好的、连续的图标,也可以设定多个图标的切换,而这只需要修改timer1定时器中的"tick"事件中的代码就可以了。下面是此动态托盘程序的具体编制步骤:

要创建程序中的实例和变量:

friend withevents notifyicon1 as system.windows.forms.notifyicon
friend withevents contextmenu1 as system.windows.forms.contextmenu
friend withevents menuitem1 as system.windows.forms.menuitem
friend withevents menuitem2 as system.windows.forms.menuitem
friend withevents menuitem3 as system.windows.forms.menuitem
'创建icon实例,用以切换图标
friend icon1 = new icon ( "icon1.ico" )
friend icon2 = new icon ( "icon2.ico" )
'为不同图标的切换提供标识符
dim beginflag as boolean = true
'定时器
friend withevents timer1 as system.windows.forms.timer



初始化实例:

me.components = new system.componentmodel.container ( )
me.notifyicon1 = new system.windows.forms.notifyicon ( me.components )
me.contextmenu1 = new system.windows.forms.contextmenu ( )
me.menuitem1 = new system.windows.forms.menuitem ( )
me.menuitem2 = new system.windows.forms.menuitem ( )
me.menuitem3 = new system.windows.forms.menuitem ( )
me.timer1 = new system.windows.forms.timer ( me.components )
me.notifyicon1.contextmenu = me.contextmenu1
me.notifyicon1.text = "vb.net的托盘程序"
me.notifyicon1.visible = true
'设定托盘程序托盘区位置显示缺省图标
me.notifyicon1.icon = icon1
'在contextmenu实例中加入菜单项
me.contextmenu1.menuitems.add ( me.menuitem1 )
me.contextmenu1.menuitems.add ( me.menuitem2 )
me.contextmenu1.menuitems.add ( me.menuitem3 )
me.menuitem1.index = 0
me.menuitem1.text = "开始转动"
me.menuitem2.index = 1
me.menuitem2.text = "停止转动"
me.menuitem3.index = 2
me.menuitem3.text = "退出"
me.autoscalebasesize = new system.drawing.size ( 6 , 14 )
me.clientsize = new system.drawing.size ( 292 , 273 )
me.name = "form1"
me.showintaskbar = false
me.text = "vb.net之winform编程动态托盘程序"
me.windowstate = system.windows.forms.formwindowstate.minimized



定义托盘程序中菜单项对应的事件,以及具体的处理方法:

'开始托盘图标的转动
private sub menuitem1_click ( byval sender as system.object ,
byval e as system.eventargs ) handles menuitem1.click
timer1.enabled = true
end sub
'停止托盘图标的转动
private sub menuitem2_click ( byval sender as object ,
byval e as system.eventargs ) handles menuitem2.click
timer1.enabled = false
end sub
'关闭程序,清除托盘资源
private sub menuitem3_click ( byval sender as object ,
byval e as system.eventargs ) handles menuitem3.click
notifyicon1.dispose ( )
application.exit ( )
end sub
'根据标识符,来确定托盘图标类型
private sub timer1_tick ( byval sender as object ,
byval e as system.eventargs ) handles timer1.tick
if beginflag = true then
notifyicon1.icon = icon2
beginflag = false
else
notifyicon1.icon = icon1
beginflag = true
end if
end sub



至此编写动态托盘程序的主要步骤就介绍完了,和上面的静态托盘程序相同,在运行此程序的时候必须保证此程序当前目录存在二个icon文件,并且名称为"icon1.ico"和"icon2.ico"。这里并没有给出动态托盘程序的完整代码清单,读者只需要把上面这些关键步骤的代码覆盖到form2.vb中的相应位置就可以得到动态托盘程序的源程序文件form3.vb。这应该不算很难,下面是编译form3.vb的命令:

vbc /r:system.dll /r:system.windows.froms.dll /r:system.drawing.dll form2.vb



成功编译、连接后就得到form3.exe,下图是form3.exe的运行界面:








图02:托盘程序运行界面02




总结


托盘程序是现在比较流行的一种程序类型。本文中介绍的二个程序,也是托盘程序中比较典型的二个,希望对各位了解并掌握编写托盘程序有所帮助。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表