C# WinForm中NotifyICon控件的用法【2】

继续前一节,本节实现系统托盘的图标闪烁功能:》》复习一下怎么做的:

【1】新建一个Windows WinForm应用程序

【2】在当前窗体添加一个contextMenuStrip上下文菜单控件,用做任务栏显示时的右键菜单,并且添加几个自定义子菜单,如显示、隐藏、关于和退出等。

【3】添加NotifyIcon1控件,用于显示任务栏图标,设置引入两张系统托盘需要显示的图标,并且设置ContextMenuStrip属性等于contextMenuStrip.

【4】当应用程序启动是在任务栏显示图标

private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.Icon = new System.Drawing.Icon("online.ico");
            notifyIcon1.Visible = true;
            notifyIcon1.Text = "Online";//图片名称
        }

【5】图标不停闪烁

添加一个timer控件,并且设置InterVal间隔执行时间1000(1s)。添加timer_Tick事件。当发生事件或需要闪烁的时候,可以设置timer1.Enable=true;,让timer开始运行

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (notifyIcon1.Text == "Online")
            {
                notifyIcon1.Icon = new System.Drawing.Icon("offline.ico");
                notifyIcon1.Text = "Offline";
            }
            else
            {
                notifyIcon1.Icon = new System.Drawing.Icon("online.ico");
                notifyIcon1.Text = "Online";
            }
        }

【6】双击系统托盘图标,显示正常窗体,添加notifyIcon_MouseDoubleClick事件

 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.Focus();
        }

 

 

 

你可能感兴趣的:(C# WinForm中NotifyICon控件的用法【2】)