程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB.NET >> VB.NET編程之托盤順序篇

VB.NET編程之托盤順序篇

編輯:VB.NET

VB.NET編程之托盤順序篇。本站提示廣大學習愛好者:(VB.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.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總結

托盤順序是如今比擬盛行的一種順序類型。本文中引見的二個順序,也是托盤順序中比擬典型的二個,希望對各位理解並掌握編寫托盤順序有所協助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved