using System ;using System.Drawing ;using System.Collections ;using System.ComponentModel ;using System.Windows.Forms ;using System.Data ;//导入在程序中使用到的名称空间public class Form1 : Form{ private System.ComponentModel.Container components = null ; private Icon mNetTrayIcon = new Icon ( "Tray.ico" ) ; private NotifyIcon TrayIcon ; private ContextMenu notifyiconMnu ; public Form1() { //初始化窗体中使用到的组件 InitializeComponent ( ) ; //初始化托盘程序的各个要素 Initializenotifyicon ( ) ; } private void Initializenotifyicon ( ) { //设定托盘程序的各个属性 TrayIcon = new NotifyIcon ( ) ; TrayIcon.Icon = mNetTrayIcon ; 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 ( ) ; } //清除程序中使用过的资源 protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent ( ) { // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(409, 64); this.ControlBox = false; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.ShowInTaskbar = false; this.Text = "用Visual C#做托盘程序!";// this.WindowState = System.Windows.Forms.FormWindowState.Minimized; } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; }}
private void Form1_Load(object sender, System.EventArgs e)
{
//生成4个菜单项对象,显示文本分别为"显示窗口"、"隐藏窗口"、"执行程序"、"退出程序"
MenuItem menuItem1=new MenuItem("显示窗口");
MenuItem menuItem2=new MenuItem("隐藏窗口");
MenuItem menuItem3=new MenuItem("执行程序");
MenuItem menuItem4=new MenuItem("退出程序");
//分别为4个菜单项添加Click事件响应函数
menuItem1.Click+=new System.EventHandler(this.menuItem1_Click);
menuItem2.Click+=new System.EventHandler(this.menuItem2_Click);
menuItem3.Click+=new System.EventHandler(this.menuItem3_Click);
menuItem4.Click+=new System.EventHandler(this.menuItem4_Click);
//设置NotifyIcon对象的ContextMenu属性为生面的弹出菜单对象
notifyIcon.ContextMenu=new ContextMenu(new MenuItem[]{menuItem1,menuItem2,menuItem3,menuItem4});
//当用户双击程序图标时将执行相应的函数
notifyIcon.DoubleClick+=new System.EventHandler(this.notifyIcon_DBClick);
} private void menuItem1_Click(object sender,System.EventArgs e)//“显示窗口”菜单的响应方法
{
if(this.Visible==false)this.Visible=true;//假如当前窗口没显示则显示当前窗口
}
private void menuItem2_Click(object sender,System.EventArgs e)//"隐藏窗口"菜单的响应方法
{
if(this.Visible==true)this.Visible=false;//假如当前窗口为显示的则隐藏窗口
}
private void menuItem3_Click(object sender,System.EventArgs e)//"执行程序"菜单的响应方法
{
//显示一个提示信息框,表示事件已经得到响应
MessageBox.Show("演示程序已经执行,此处功能就是显示一个提示框!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void menuItem4_Click(object sender,System.EventArgs e)//“退出程序”菜单的响应方法
{
this.Close();//关闭当前对象(即窗体)
Application.Exit();//通过Application类的静态方法Exit()退出应用程序
}
private void notifyIcon_DBClick(object sender, System.EventArgs e)//用户双击应用程序图标进的响应方法
{
this.Visible=true;//显示当前主窗口
this.notifyIcon.Visible = false;
}如果还不知道,再问。
{
if ( miShow.Text.CompareTo( "显示服务器" ) == 0 )
{
Show();
this.Activate();
miShow.Text = "隐藏服务器";
}
}
private void FormServerMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Hide();
miShow.Text = "显示服务器";
e.Cancel = true;
} private void miShow_Click(object sender, System.EventArgs e)
{
if ( miShow.Text.CompareTo( "显示服务器" ) == 0 )
{
Show();
miShow.Text = "隐藏服务器";
}
else
{
Hide();
miShow.Text = "显示服务器";
}
}
using System.Drawing;
using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components; [STAThread]
static void Main()
{
Application.Run(new Form1());
} public Form1()
{
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem(); // Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {this.menuItem1}); // Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click); // Set up how the form should be displayed.
this.ClientSize = new System.Drawing.Size(292, 266);
this.Text = "Notify Icon Example"; // Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); // The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = new Icon("appicon.ico"); // The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1; // The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Form1 (NotifyIcon example)";
notifyIcon1.Visible = true; // Handle the DoubleClick event to activate the form.
notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick); } protected override void Dispose( bool disposing )
{
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose(); base.Dispose( disposing );
} private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon. // Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal; // Activate the form.
this.Activate();
} private void menuItem1_Click(object Sender, EventArgs e) {
// Close the form, which closes the application.
this.Close();
}
}
form.visible=false时候
它.visible就为true
form.visible=true时候
它.visible就为false
然后给它的双击事件写方法探出菜单等,简单吧!