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实例
  1<system.diagnostics.debuggerstepthrough (="" )=""> 
  2        Private Sub InitializeComponent ( ) 
  3    	Me.components = New System.ComponentModel.Container ( ) 
  4    	Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon ( Me.components ) 
  5    		Me.NotifyIcon1.Text = "NotifyIcon1"
  6    		Me.NotifyIcon1.Visible = True
  7    		'对NotifyIcon实例的Icon属性赋值,完成简单托盘程序
  8    		Me.NotifyIcon1.Icon = TrayIcon
  9    		Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 ) 
 10    		Me.ClientSize = New System.Drawing.Size ( 292 , 273 ) 
 11    		Me.Name = "Form1"
 12    		Me.Text = "Form1"
 13    	    End Sub
 14    	#End Region
 15    	End Class
 16    	'启动程序
 17    	Module Module1
 18    	Sub Main ( ) 
 19    	  Application.Run ( new Form1 ( ) ) 
 20    	End sub
 21    	End Module  
 22  
 23---  
 24  
 25  
 26但是这个托盘程序还不是真正意义上的托盘程序,因为它还有很多具体功能没有实现,下面就列出这些功能,并介绍具体的实现方法   
 27  
 28(1).托盘程序是隐藏窗口后,程序不应该显示在任务栏中,并且一般运行后都不显示窗口:   
 29  
 30这是通过设定窗体的属性来完成的,具体如下:   
 31  
 32
 33    
 34    
 35    '设定程序不应该显示在任务栏
 36    Me.ShowInTaskbar = False
 37    '设定程序运行后最小化
 38       Me.WindowState = System.Windows.Forms.FormWindowState.Minimized  
 39  
 40---  
 41  
 42  
 43(2).定义托盘程序中的菜单和相关事件:   
 44  
 45往NotifyIcon实例中添加菜单,首先要创建ContextMenu实例,此实例主要作用是表示快捷菜单,其中的菜单项是通过创建MenuItem实例来实现,托盘程序中的菜单有几个菜单项,就创建几个MenuItem实例。然后把这些菜单项加入到ContextMenu实例,并把此实例赋值给NotifyIcon实例的ContextMenu属性,这样托盘程序右键点击弹出的菜单就完成了。下面是具体代码:   
 46  
 47
 48    
 49    
 50    创建ContextMenu实例和MenuItem实例:
 51        Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
 52        Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
 53        Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
 54        Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem  
 55  
 56---  
 57  
 58  
 59把这些菜单项加入到ContextMenu实例,并把ContextMenu实例赋值给NotifyIcon实例的ContextMenu属性:   
 60  
 61
 62    
 63    
 64    Me.MenuItem1 = New System.Windows.Forms.MenuItem ( ) 
 65            Me.MenuItem2 = New System.Windows.Forms.MenuItem ( ) 
 66            Me.MenuItem3 = New System.Windows.Forms.MenuItem ( ) 
 67            Me.NotifyIcon1.ContextMenu = Me.ContextMenu1
 68            Me.NotifyIcon1.Text = "VB.NET的托盘程序"
 69            Me.NotifyIcon1.Visible = True
 70    	'设定托盘程序托盘区位置显示图标
 71            Me.NotifyIcon1.Icon = TrayIcon
 72    	'在ContextMenu实例中加入菜单项
 73            Me.ContextMenu1.MenuItems.Add ( Me.MenuItem1 )  
 74            Me.ContextMenu1.MenuItems.Add ( Me.MenuItem2 ) 
 75            Me.ContextMenu1.MenuItems.Add ( Me.MenuItem3 ) 
 76            Me.MenuItem1.Index = 0
 77            Me.MenuItem1.Text = "显示窗体"
 78            Me.MenuItem2.Index = 1
 79            Me.MenuItem2.Text = "隐藏窗体"
 80            Me.MenuItem3.Index = 2
 81            Me.MenuItem3.Text = "退出"  
 82  
 83---  
 84  
 85  
 86当把ContextMenu实例赋值给NotifyIcon实例的ContextMenu属性后,托盘程序的缺省状态是当鼠标右击托盘图标,就会弹出对应的菜单。这时就可以对其中的每一个菜单项定义相应的事件以及具体的处理方法。一个完整的静态托盘程序的编写过程就完成了。   
 87  
 88最后要请读者注意的是,由于本文中的托盘程序的图标并不是通过创建资源文件来实现的,而是通过创建Icon实例完成的。所以在程序运行的时候,必须在程序的当前目录存在一个图标文件,并且此图标文件的名称为"Tray.ico"。下面是这个静态托盘程序的完整的代码清单   
 89(Form2.vb):   
 90Public Class Form1   
 91Inherits System.Windows.Forms.Form   
 92#Region " Windows 窗体设计器生成的代码 "   
 93Public Sub New ( )   
 94MyBase.New ( )   
 95'该调用是 Windows 窗体设计器所必需的。   
 96InitializeComponent ( )   
 97'在 InitializeComponent ( ) 调用之后添加任何初始化   
 98End Sub   
 99'窗体重写处置以清理组件列表。   
100Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )   
101If disposing Then   
102If Not ( components Is Nothing ) Then   
103components.Dispose ( )   
104End If   
105End If   
106MyBase.Dispose ( disposing )   
107End Sub   
108'Windows 窗体设计器所必需的   
109Private components As System.ComponentModel.IContainer   
110'注意:以下过程是 Windows 窗体设计器所必需的   
111'可以使用 Windows 窗体设计器修改此过程。   
112'不要使用代码编辑器修改它。   
113Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon   
114Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu   
115Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem   
116Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem   
117Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem   
118Friend TrayIcon = New Icon ( "Tray.ico" )   
119<system.diagnostics.debuggerstepthrough (="" )="">   
120Private Sub InitializeComponent ( )   
121Me.components = New System.ComponentModel.Container ( )   
122Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon ( Me.components )   
123Me.ContextMenu1 = New System.Windows.Forms.ContextMenu ( )   
124Me.MenuItem1 = New System.Windows.Forms.MenuItem ( )   
125Me.MenuItem2 = New System.Windows.Forms.MenuItem ( )   
126Me.MenuItem3 = New System.Windows.Forms.MenuItem ( )   
127Me.NotifyIcon1.ContextMenu = Me.ContextMenu1   
128Me.NotifyIcon1.Text = "VB.NET的托盘程序"   
129Me.NotifyIcon1.Visible = True   
130'设定托盘程序托盘区位置显示图标   
131Me.NotifyIcon1.Icon = TrayIcon   
132'在ContextMenu实例中加入菜单项   
133Me.ContextMenu1.MenuItems.Add ( Me.MenuItem1 )   
134Me.ContextMenu1.MenuItems.Add ( Me.MenuItem2 )   
135Me.ContextMenu1.MenuItems.Add ( Me.MenuItem3 )   
136Me.MenuItem1.Index = 0   
137Me.MenuItem1.Text = "显示窗体"   
138Me.MenuItem2.Index = 1   
139Me.MenuItem2.Text = "隐藏窗体"   
140Me.MenuItem3.Index = 2   
141Me.MenuItem3.Text = "退出"   
142Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 )   
143Me.ClientSize = New System.Drawing.Size ( 292 , 273 )   
144Me.Name = "Form1"   
145'设定程序不应该显示在任务栏   
146Me.ShowInTaskbar = False   
147Me.Text = "VB.NET之WinForm编程托盘程序篇"   
148'设定程序运行后最小化   
149Me.WindowState = System.Windows.Forms.FormWindowState.Minimized   
150End Sub   
151#End Region   
152'显示托盘程序窗口   
153Private Sub MenuItem1_Click ( ByVal sender As System.Object ,   
154ByVal e As System.EventArgs ) Handles MenuItem1.Click   
155Me.WindowState = FormWindowState.Normal   
156Me.Show ( )   
157End Sub   
158'隐藏托盘程序窗口   
159Private Sub MenuItem2_Click ( ByVal sender As Object ,   
160ByVal e As System.EventArgs ) Handles MenuItem2.Click   
161Me.Hide ( )   
162End Sub   
163'推出托盘程序窗口   
164Private Sub MenuItem3_Click ( ByVal sender As Object ,   
165ByVal e As System.EventArgs ) Handles MenuItem3.Click   
166NotifyIcon1.Dispose ( )   
167Application.Exit ( )   
168End Sub   
169'双击图标显示窗体   
170Private Sub NotifyIcon1_DoubleClick ( ByVal sender As Object ,   
171ByVal e As System.EventArgs ) Handles NotifyIcon1.DoubleClick   
172Me.WindowState = FormWindowState.Normal   
173Me.Show ( )   
174End Sub   
175End Class   
176'启动程序   
177Module Module1   
178Sub Main ( )   
179Application.Run ( new Form1 ( ) )   
180End sub   
181End Module   
182  
183Form2.vb经过了下列命令编译、连接后:   
184  
185Vbc /r:system.dll /r:system.windows.froms.dll /r:system.drawing.dll form2.vb   
186  
187就可以得到Form2.exe,下图是From2.exe运行的界面:   
188  
189
190
191![](http://tech.ccidnet.com/pub/attachment/2002/10/36990.jpg)
192
193  
194  
195
196
197图01:托盘程序运行界面01 
198
199  
200  
201
202
203**动态托盘程序的编写过程**
204
205  
206  
207动态托盘程序中的托盘图标之所以能够呈现动画效果,是因为程序中的一个定时器组   
208  
209件每隔一段时间都不断切换托盘图标。本文是通过二个图标的切换来表现动态效果的,读者如果有好的、连续的图标,也可以设定多个图标的切换,而这只需要修改Timer1定时器中的"Tick"事件中的代码就可以了。下面是此动态托盘程序的具体编制步骤:   
210  
211**要创建程序中的实例和变量:**   
212  
213
214    
215    
216    Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
217        Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
218        Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
219       Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
220        Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
221        '创建Icon实例,用以切换图标
222        Friend Icon1 = New Icon ( "icon1.ico" ) 
223        Friend Icon2 = New Icon ( "icon2.ico" ) 
224        '为不同图标的切换提供标识符
225        Dim BeginFlag As Boolean = True
226        '定时器
227        Friend WithEvents Timer1 As System.Windows.Forms.Timer  
228  
229---  
230  
231  
232**初始化实例:**   
233  
234
235    
236    
237    Me.components = New System.ComponentModel.Container ( ) 
238            Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon ( Me.components ) 
239            Me.ContextMenu1 = New System.Windows.Forms.ContextMenu ( ) 
240            Me.MenuItem1 = New System.Windows.Forms.MenuItem ( ) 
241            Me.MenuItem2 = New System.Windows.Forms.MenuItem ( ) 
242            Me.MenuItem3 = New System.Windows.Forms.MenuItem ( ) 
243            Me.Timer1 = New System.Windows.Forms.Timer ( Me.components ) 
244            Me.NotifyIcon1.ContextMenu = Me.ContextMenu1
245            Me.NotifyIcon1.Text = "VB.NET的托盘程序"
246            Me.NotifyIcon1.Visible = True
247    	'设定托盘程序托盘区位置显示缺省图标
248            Me.NotifyIcon1.Icon = Icon1
249    	'在ContextMenu实例中加入菜单项
250            Me.ContextMenu1.MenuItems.Add  (  Me.MenuItem1  )   
251            Me.ContextMenu1.MenuItems.Add  (  Me.MenuItem2  )  
252            Me.ContextMenu1.MenuItems.Add  (  Me.MenuItem3  )  
253            Me.MenuItem1.Index = 0
254            Me.MenuItem1.Text = "开始转动"
255            Me.MenuItem2.Index = 1
256            Me.MenuItem2.Text = "停止转动"
257            Me.MenuItem3.Index = 2
258            Me.MenuItem3.Text = "退出"
259            Me.AutoScaleBaseSize = New System.Drawing.Size ( 6 , 14 ) 
260            Me.ClientSize = New System.Drawing.Size ( 292 , 273 ) 
261            Me.Name = "Form1"
262            Me.ShowInTaskbar = False
263            Me.Text = "VB.NET之WinForm编程动态托盘程序"
264            Me.WindowState = System.Windows.Forms.FormWindowState.Minimized  
265  
266---  
267  
268  
269**定义托盘程序中菜单项对应的事件,以及具体的处理方法:**   
270  
271
272    
273    
274    '开始托盘图标的转动
275        Private Sub MenuItem1_Click ( ByVal sender As System.Object , 
276        ByVal e As System.EventArgs )  Handles MenuItem1.Click
277            Timer1.Enabled = True
278        End Sub
279        '停止托盘图标的转动
280        Private Sub MenuItem2_Click ( ByVal sender As Object ,
281        ByVal e As System.EventArgs )  Handles MenuItem2.Click
282            Timer1.Enabled = False
283        End Sub
284        '关闭程序,清除托盘资源
285        Private Sub MenuItem3_Click ( ByVal sender As Object , 
286        ByVal e As System.EventArgs )  Handles MenuItem3.Click
287            NotifyIcon1.Dispose ( ) 
288            Application.Exit ( ) 
289        End Sub
290        '根据标识符,来确定托盘图标类型
291        Private Sub Timer1_Tick ( ByVal sender As Object ,
292        ByVal e As System.EventArgs )  Handles Timer1.Tick
293            If BeginFlag = True Then
294                NotifyIcon1.Icon = Icon2
295                BeginFlag = False
296            Else
297                NotifyIcon1.Icon = Icon1
298                BeginFlag = True
299            End If
300        End Sub  
301  
302---  
303  
304  
305至此编写动态托盘程序的主要步骤就介绍完了,和上面的静态托盘程序相同,在运行此程序的时候必须保证此程序当前目录存在二个Icon文件,并且名称为"Icon1.ico"和"Icon2.ico"。这里并没有给出动态托盘程序的完整代码清单,读者只需要把上面这些关键步骤的代码覆盖到Form2.vb中的相应位置就可以得到动态托盘程序的源程序文件Form3.vb。这应该不算很难,下面是编译Form3.vb的命令:   
306  
307
308    
309    
310    Vbc /r:system.dll /r:system.windows.froms.dll /r:system.drawing.dll form2.vb  
311  
312---  
313  
314  
315成功编译、连接后就得到Form3.exe,下图是Form3.exe的运行界面:   
316  
317
318
319![](http://tech.ccidnet.com/pub/attachment/2002/10/36991.jpg)
320
321  
322  
323
324
325图02:托盘程序运行界面02 
326
327  
328  
329
330
331**总结**
332
333  
334  
335托盘程序是现在比较流行的一种程序类型。本文中介绍的二个程序,也是托盘程序中比较典型的二个,希望对各位了解并掌握编写托盘程序有所帮助。</system.diagnostics.debuggerstepthrough></system.diagnostics.debuggerstepthrough>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus