C#程序在托盘后台运行

源代码如下:

请在工具栏把notifyIcon控件拖入窗体再使用:

//隐藏窗体  
        private bool windowCreate = true;  
        private void toolStripMenuItem1_Click(object sender, EventArgs e) //这是菜单选项的一个item点击事件  
        {  
            if (windowCreate)  
            {  
                base.Visible = false;  
                windowCreate = false;  
            }  
            this.Hide();  
            base.OnActivated(e);   
        }  
        //显示回窗体(notifyIcon控件双击事件,注:请选择一个ico图标,这样隐藏后在右下角显示有相关图标)  
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)  
        {  
            if (this.Visible == true)  
            {  
                this.Hide();  
                this.ShowInTaskbar = false;  
            }  
            else  
            {  
                this.Visible = true;  
                this.ShowInTaskbar = true;  
                this.WindowState = FormWindowState.Normal;  
                //this.Show();  
                this.BringToFront();  
                windowCreate = true;  
            }  
        }  

若想直接打开程序后,自动后台运行的话,以下代码实现:

load函数中
this.Hide();
this.ShowInTaskbar = false;


你可能感兴趣的:(C#程序在托盘后台运行)