用Visual C#做托盘程序
private Icon mNetTrayIcon = new Icon ( "tb.ico" );
private NotifyIcon TrayIcon=new NotifyIcon();
private ContextMenu notifyiconMnu ;
private void DataReceive_Resize(object sender, System.EventArgs e)
{
if(this.WindowState==FormWindowState.Minimized)
{
this.ShowInTaskbar=false;
this.Visible=false;
this.TrayIcon.Visible=true;
}
}
private void Initializenotifyicon()
{
//设定托盘程序的各个属性
TrayIcon = new NotifyIcon ( );
TrayIcon.Icon = mNetTrayIcon;
TrayIcon.Text = "数据传输程序" + "/n" + "吉林丰元";
TrayIcon.Visible = true;
TrayIcon.Click += new System.EventHandler ( this.click );
TrayIcon.DoubleClick+=new System.EventHandler ( this.showmessage);
//定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
MenuItem [ ] mnuItms = new MenuItem [ 3 ];
mnuItms [ 0 ] = new MenuItem ( );
mnuItms [ 0 ] .Text = "显示控制台";
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 )
{
}
public void showmessage ( object sender , System.EventArgs e )
{
this.ShowInTaskbar=true;
this.Visible=true;
this.SetTopLevel(true);
this.WindowState=FormWindowState.Normal;
this.Show();
this.Activate();
}
public void ExitSelect ( object sender , System.EventArgs e )
{
//隐藏托盘程序中的图标
TrayIcon.Visible = false ;
//关闭系统
Application.Exit();
}