您现在的位置是: 软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > C#专区 > 正文


-Win xp中的多种网络
-试验试验试验试验
-用Freehand实现位图矢量化
-网络电话面面观

用Visual C#做托盘程序
2002-01-10· ·阿虎··yesky

上一页  1 2 3  


  二. 本文介绍的程序源代码( Tray.cs )和编译后程序运行界面:

下图时Tray.cs成功编译后的运行界面:


  图01:程序运行界面

  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 ;

 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程序开发语言有了施展自身功能更广阔的舞台。


  本文源代码下载


上一页  1 2 3  

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 C#简介
 让窗体飘动起来--C#中Timer组件用法
 C#中的远程执行和分布式计算
 Visual C#的剪切板编程
 Visual C#的剪切板编程
 C#中的远程执行和分布式计算
 用C#实现基于TCP协议的网络通讯
 在C#应用程序中控制输入法
 C#:消息队列应用程序
 用Visual C#发送电子邮件
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!