C#实现闪动托盘图标示例

在用户正在登录QQ或者使用Firemail邮件系统自动收取邮件的时候,托盘图标会闪动提示用户正在运行的任务。

闪动图标可以使用定时切换托盘图标的方式实现,托盘图标可以从ImageList控件中获取。在ImageList控件里面添加三个icon,第一个icon表示窗体启动以后的托盘图标。第二个和第三个图标分别表示当特定的任务发生的时候,定时切换的图标。

1)设置托盘的图标可以从ImageList控件中的Image对象转换为Icon对象,

        ///

        /// 设置托盘显示的图标

        ///

        /// 图像列表中图片的索引

        private void setIconImg(int index)

        {

            Image img = this.imgIcon.Images[index];

            Bitmap b = new Bitmap(img);

            Icon icon = Icon.FromHandle(b.GetHicon());

            this.niMain.Icon = icon;

        }

 

 

2)定时器事件实现图标的切换

       ///

        /// 定时器切换图标显示

        ///

        ///

        ///

        private void tmrIcon_Tick(object sender, EventArgs e)

        {

            if (iconFlag)

            {

                this.setIconImg(1);

                iconFlag = !iconFlag;

            }

            else

            {

                this.setIconImg(2);

                iconFlag = !iconFlag;

 

            }

        }

 

 

3)窗体的最小化按钮和关闭按钮实现隐藏窗体的功能,窗体关闭时,会执行FormClosing 事件,释放与此窗体关联的所有资源。因此需要取消关闭事件,实现窗体的隐藏和托盘的显示功能。

        ///

        /// 关闭窗体时隐藏窗体显示托盘图标

        ///

        ///

        ///

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)

        {

            e.Cancel = true;

            this.Hide();

            this.niMain.Visible = true;

        }

 

4)托盘关联菜单有四个功能:

l         “显示” :显示主窗体、隐藏托盘图标。

l         “运行”:图标闪动效果,模拟任务正在运行。

l         “停止”:恢复初始化的图标,模拟任务停止。

l         “退出”:提示用户并退出应用系统。

完整的代码如下:

namespace NotifyIconDemo

{

    public partial class FrmMain : Form

    {

 

        //切换图片的标识

        private bool iconFlag = false;

        //系统是否运行

        private bool isRun = false;

        ///

        ///

        ///

        public FrmMain()

        {

            InitializeComponent();

            //设置icon显示的图片

            this.setIconImg(0);

        }

        ///

        /// 设置托盘显示的图标

        ///

        /// 图像列表中图片的索引

        private void setIconImg(int index)

        {

            Image img = this.imgIcon.Images[index];

            Bitmap b = new Bitmap(img);

            Icon icon = Icon.FromHandle(b.GetHicon());

            this.niMain.Icon = icon;

        }

 

        ///

        /// 显示主窗体

        ///

        ///

        ///

        private void tsmiMain_Click(object sender, EventArgs e)

        {

            //显示主窗体

            this.Visible = true;

            this.WindowState = System.Windows.Forms.FormWindowState.Normal;

            //隐藏托盘图标

            this.niMain.Visible = false;

            this.Show();

        }

        ///

        /// 退出

        ///

        ///

        ///

        private void tsmiExit_Click(object sender, EventArgs e)

        {

            //设置托盘的提示信息

            this.niMain.BalloonTipText = "成功退出Demo!";

            this.niMain.BalloonTipTitle = "退出";

            this.niMain.ShowBalloonTip(1000*3);

            //延迟退出

            Thread.Sleep(1000 * 2);

            //释放托盘图标资源

            this.niMain.Dispose();

            //终止线程

            Application.ExitThread();

        }

        ///

        /// 最小化时隐藏窗体,显示托盘图标

        ///

        ///

        ///

        private void FrmMain_SizeChanged(object sender, EventArgs e)

        {

            if (this.WindowState == FormWindowState.Minimized)

            {

                this.Hide();

                this.niMain.Visible = true;

            }

 

        }

 

 

        ///

        /// 关闭窗体时隐藏窗体显示托盘图标

        ///

        ///

        ///

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)

        {

            e.Cancel = true;

            this.Hide();

            //this.ShowInTaskbar = false;//取消窗体在任务栏的显示

            this.niMain.Visible = true;

        }

        ///

        /// 加载窗体时隐藏窗体

        ///

        ///

        ///

        private void FrmMain_Load(object sender, EventArgs e)

        {

            this.Hide();

        }

 

 

        ///

        /// 定时器切换图标显示

        ///

        ///

        ///

        private void tmrIcon_Tick(object sender, EventArgs e)

        {

            if (!this.isRun)

            {

                return;

            }

            if (iconFlag)

            {

                this.setIconImg(1);

                iconFlag = !iconFlag;

            }

            else

            {

                this.setIconImg(2);

                iconFlag = !iconFlag;

            }

        }

        ///

        /// 点击运行菜单

        ///

        ///

        ///

        private void tsmiRun_Click(object sender, EventArgs e)

        {

            this.tsmiRun.Enabled = false;

            this.tsmiStop.Enabled = true;

            //设置运行状态

            this.isRun = true;

        }

        ///

        /// 点击停止菜单

        ///

        ///

        ///

        private void tsmiStop_Click(object sender, EventArgs e)

        {

            this.tsmiRun.Enabled = true;

            this.tsmiStop.Enabled = false;

            //设置为停止状态

            this.isRun = false;

            //恢复图标显示

            this.setIconImg(0);

        }

    }

}

 

你可能感兴趣的:(.NET)