C#winform实现系统托盘后台运行功能

C#winform实现系统托盘后台运行功能,就是一运行,就直接到系统托盘的位置,程序在后台运行

窗体的Shown方法里加上this.Hide();就可以了

对这个需求完全可以在单击“关闭”按钮的时候弹出一个对话框,来让用户确定是否真的要退出。这是一个很好的解决方法,并且实现也是很容易的。但是人家不想这样,想要拥有类似QQ在托盘区后台运行的那种效果,没办法,只能想办法来实现了。www.2cto.com

其实,这个在网上也有很多的实现方法,但是我试了试有些直接复制过来并不能直接运行,或者能运行的吧又没有注释。今天在这里就给大家贴一下我的代码,也有不足之处,希望大家给出意见.

[csharp]

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Windows;

namespace winform窗体托盘后台运行

{

public partial class Form1 : Form   

{  

    //这里在窗体上没有拖拽一个NotifyIcon控件,而是在这里定义了一个变量  

    private NotifyIcon notifyIcon = null;  



    public Form1()  

    {  

        InitializeComponent();  

        //调用初始化托盘显示函数  

        InitialTray();  

    }  



    private void Form1_Load(object sender, EventArgs e)  

    {  

        //这里写其他代码  

    }  www.2cto.com



    /// <summary>  

    /// 窗体关闭的单击事件  

    /// </summary>  

    /// <param name="sender"></param>  

    /// <param name="e"></param>  

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)  

    {  

        e.Cancel = true;  

        //通过这里可以看出,这里的关闭其实不是真正意义上的“关闭”,而是将窗体隐藏,实现一个“伪关闭”  

        this.Hide();  

    }  



    private void InitialTray()  

    {  

        //隐藏主窗体  

        this.Hide();  



        //实例化一个NotifyIcon对象  

        notifyIcon = new NotifyIcon();  

        //托盘图标气泡显示的内容  

        notifyIcon.BalloonTipText = "正在后台运行";     

        //托盘图标显示的内容  

        notifyIcon.Text = "窗体托盘后台运行测试程序";  

        //注意:下面的路径可以是绝对路径、相对路径。但是需要注意的是:文件必须是一个.ico格式  

        notifyIcon.Icon = new System.Drawing.Icon("E:/ASP项目/images/3.5 inch Floppy.ico");  

        //true表示在托盘区可见,false表示在托盘区不可见  

        notifyIcon.Visible = true;  

        //气泡显示的时间(单位是毫秒)  

        notifyIcon.ShowBalloonTip(2000);              

        notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);  



        ////设置二级菜单  

        //MenuItem setting1 = new MenuItem("二级菜单1");  

        //MenuItem setting2 = new MenuItem("二级菜单2");  

        //MenuItem setting = new MenuItem("一级菜单", new MenuItem[]{setting1,setting2});  



        //帮助选项,这里只是“有名无实”在菜单上只是显示,单击没有效果,可以参照下面的“退出菜单”实现单击事件  

        MenuItem help = new MenuItem("帮助");  



        //关于选项  

        MenuItem about = new MenuItem("关于");  



        //退出菜单项  

        MenuItem exit = new MenuItem("退出");  

        exit.Click += new EventHandler(exit_Click);  



        ////关联托盘控件  

        //注释的这一行与下一行的区别就是参数不同,setting这个参数是为了实现二级菜单  

        //MenuItem[] childen = new MenuItem[] { setting, help, about, exit };  

        MenuItem[] childen = new MenuItem[] {help,about,exit};  

        notifyIcon.ContextMenu = new ContextMenu(childen);  



        //窗体关闭时触发  

        this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);  

    }  



    /// <summary>  

    /// 鼠标单击  

    /// </summary>  

    /// <param name="sender"></param>  

    /// <param name="e"></param>  

    private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)  

    {  

        //鼠标左键单击  

        if (e.Button == MouseButtons.Left)  

        {  

            //如果窗体是可见的,那么鼠标左击托盘区图标后,窗体为不可见  

            if (this.Visible == true )  

            {  

                this.Visible = false;  

            }  

            else  

            {  

                this.Visible = true;  

                this.Activate();  

            }  

        }  

    }  



    /// <summary>  

    /// 退出选项  

    /// </summary>  

    /// <param name="sender"></param>  

    /// <param name="e"></param>  

    private void exit_Click(object sender, EventArgs e)  

    {  

        //退出程序  

       System.Environment.Exit(0);    

    }  

}  

}

用NotifyIcon控件。在其中的click写this.Show
在FormLoad里面写this.Hide

NotifyIcon托盘控件
在form_load方法里面 this.hide(); 隐藏主界面