当前位置:编程学习 > C#/ASP.NET >>

VB.NET编程之托盘程序篇

答案:托盘程序是指这样一类程序:当程序运行后,会在系统的托盘区(也有说是状态区域)创建此程序的图标,使用者可以通过点击图标出现的菜单来控制程序的运行状态。托盘程序有很多优点,如不占屏幕,后台运行,便于控制等。所以现在越来越多的程序都做成了托盘程序。在VB.NET中,编写托盘程序是比较方便和简单的。这是因为VB.NET没有自身类库,它所使用的类库是.Net框架中为所有.Net平台开发语言提供的公用类库——.Net FrameWork SDK。在这个类库中,为编写托盘程序提供了具体的类,调用这些类就可以实现程序的托盘效果了。从而也就摆脱了它的前身VB在处理这类问题时候的烦琐。下面就通过二个具体例子来了解并掌握如何用VB.NET编写托盘程序。

本文程序的设计及运行环境

(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.Men

上一个:VB中定制DllRegisterServer、DllUnregisterServer
下一个:持续集成 .Net手册

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,