並把此數組同時賦值給ContextMenu對象
MenuItem [ ] mnuItms = new MenuItem [ 3 ] ;
mnuItms [ 0 ] = new MenuItem ( ) ;
mnuItms [ 0 ] .Text = "用Visual C#做托盤程序!" ;
mnuItms [ 0 ] .Click += new System.EventHandler ( this.showmessage ) ;
mnuItms [ 1 ] = new MenuItem ( "-" ) ;
mnuItms [ 2 ] = new MenuItem ( ) ;
mnuItms [ 2 ] .Text = "退出系統" ;
mnuItms [ 2 ] .Click += new System.EventHandler ( this.ExitSelect ) ;
mnuItms [ 2 ] .DefaultItem = true ;
notifyiconMnu = new ContextMenu ( mnuItms ) ;
TrayIcon.ContextMenu = notifyiconMnu ;
//為托盤程序加入設定好的ContextMenu對象
當成功加入了ContextMenu對象後,在程序編譯完成運行時,當鼠標右鍵點擊托盤圖標,程序會自動彈出ContextMenu對象封裝好的菜單。
二. 本文介紹的程序源代碼( Tray.cs ):
Tray.cs源程序代碼:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
//導入在程序中使用到的名稱空間
public class Tray : Form
{
private System.ComponentModel.Container components = null ;
private Icon mNetTrayIcon = new Icon ( "Tray.ico" ) ;
private NotifyIcon TrayIcon ;
private ContextMenu notifyiconMnu ;
public Tray()
{
//初始化窗體中使用到的組件
InitializeComponent ( ) ;
//初始化托盤程序的各個要素
Initializenotifyicon ( ) ;
}
private void Initializenotifyicon ( )
{
//設定托盤程序的各個屬性
TrayIcon = new NotifyIcon ( ) ;
TrayIcon.Icon = mNetTrayIcon ;
TrayIcon.Text = "用Visual C#做托盤程序" + "\n" + "作者:馬金虎於2001.12.08" ;
TrayIcon.Visible = true ;
TrayIcon.Click += new System.EventHandler ( this.click ) ;
//定義一個MenuItem數組,並把此數組同時賦值給ContextMenu對象
MenuItem [ ] mnuItms = new MenuItem [ 3 ] ;
mnuItms [ 0 ] = new MenuItem ( ) ;
mnuItms [ 0 ] .Text = "用Visual C#做托盤程序!" ;
mnuItms [ 0 ] .Click += new System.EventHandler ( this.showmessage ) ;
mnuItms [ 1 ] = new MenuItem ( "-" ) ;
mnuItms [ 2 ] = new MenuItem ( ) ;
mnuItms [ 2 ] .Text = "退出系統" ;
mnuItms [ 2 ] .Click += new System.EventHandler ( this.ExitSelect ) ;
mnuItms [ 2 ] .DefaultItem = true ; r>
notifyiconMnu = new ContextMenu ( mnuItms ) ;
TrayIcon.ContextMenu = notifyiconMnu ;
//為托盤程序加入設定好的ContextMenu對象
}
public void click ( object sender , System.EventArgs e )
{
MessageBox.Show ( "Visual C#編寫托盤程序中的事件響應" ) ;
}
public void showmessage ( object sender , System.EventArgs e )
{
MessageBox.Show ( "你點擊了菜單的第一個選項" ) ;
}
public void ExitSelect ( object sender , System.EventArgs e )
{
//隱藏托盤程序中的圖標
TrayIcon.Visible = false ;
//關閉系統
this.Close ( ) ;
}
//清除程序中使用過的資源
public override void Dispose ( )
{
base.Dispose ( ) ;
if ( components != null )
components.Dispose ( ) ;
}
private void InitializeComponent ( )
{
this.SuspendLayout ( ) ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.ClIEntSize = new System.Drawing.Size ( 320 , 56 ) ;
this.ControlBox = false ;
this.MaximizeBox = false ;
this.MinimizeBox = false ;
this.WindowState = System.Windows.Forms.FormWindowstate.Minimized ;
this.Name = "tray" ;
this.ShowInTaskbar = false ;
this.Text = "用Visual C#做托盤程序!" ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Tray ( ) ) ;
}
}
三. 總結:
通過以上介紹,可以看出用Visual C#做一個托盤程序並不是一件復雜的事情,而是一件比較輕松的事情。同樣也可使我們明白,Visual C#雖然是一種功能強大的程序設計語言,但它只是一個打開.Net FrameWork SDK的鑰匙,正是這個內容豐富的軟件包,才使得各個.Net程序開發語言有了施展自身功能更廣闊的舞台。