VB.NET编程之托盘程序篇

由于本文中的托盘程序的图标并不是通过创建资源文件来实现的,而是通过创建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" )
  
   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    

你可能感兴趣的:(VB.NET编程之托盘程序篇)