C#实现托盘动态图标

 

系统托盘是个特殊区域,通常在桌面的底部右侧。在那里,用户可以随时访问正在运行中的那些程序。在微软的Windows里,系统托盘常指任务栏的状态区域;在Gnome的桌面时,常指布告栏区域;在KDE桌面时,指系统托盘。在每个系统里,托盘是所有正运行在桌面环境里。当然,如果能够根据程序的运行状态动态修改系统托盘的图片,还能起到为用户提供系统状态的作用。

实现步骤:

1:新建WinForm项目

2:从工具栏中拖拽NotifyIcon控件,然后设置这个控件的icon属性。大运行项目时你会发现在系统的托盘里面出现了你所选择的图标。

NotifyIcon图标的使用说明:

1、          ContextMenu属性:

这个属性需要一个“上下文菜单”(可以从工具栏中拖拽到项目内,然后通过鼠标选择),当图标上单击右键时能够显示这个菜单

2、          Text属性

当鼠标移动到图标上时,系统将要自动显示的信息

3、          Icon属性

在托盘中显示的图标

4、          Visiable属性

决定图标是否可见

一种动态系统托盘的实现过程:

1、          拖拽NotifyIcon、timer、ContextMenu三个控件;

2、          Form1类的自动内容如下:

private System.Windows.Forms.NotifyIcon notifyIcon1;

      private System.Windows.Forms.ContextMenu contextMenu1;

      private System.Windows.Forms.MenuItem menuItem1;

      private System.Windows.Forms.MenuItem menuItem2;

      private System.Windows.Forms.MenuItem menuItem3;

      private System.Windows.Forms.MenuItem menuItem4;

      private System.Windows.Forms.Timer timer1;

      private System.ComponentModel.IContainer components;

      int ico =1;//记录当前的图标序号

3

#region Windows 窗体设计器生成的代码

      ///<summary>

      ///设计器支持所需的方法 - 不要使用代码编辑器修改

      ///此方法的内容。

      ///</summary>

      private void InitializeComponent()

      {

        this.components = new System.ComponentModel.Container();

        System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));

        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

        this.contextMenu1 = new System.Windows.Forms.ContextMenu();

        this.menuItem1 = new System.Windows.Forms.MenuItem();

        this.menuItem2 = new System.Windows.Forms.MenuItem();

        this.menuItem3 = new System.Windows.Forms.MenuItem();

        this.menuItem4 = new System.Windows.Forms.MenuItem();

        this.timer1 = new System.Windows.Forms.Timer(this.components);

        //

        // notifyIcon1

         //

        this.notifyIcon1.ContextMenu = this.contextMenu1;

        this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));

        this.notifyIcon1.Text = "动态托盘测试系统";

        this.notifyIcon1.Visible = true;

        //

        // contextMenu1

        //

        this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

                                                               this.menuItem1,

                                                               this.menuItem2,

                                                               this.menuItem3,

                                                               this.menuItem4});

        this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);

        //

        // menuItem1

        //

        this.menuItem1.Index = 0;

        this.menuItem1.Text = "打开";

        //

        // menuItem2

        //

        this.menuItem2.Index = 1;

        this.menuItem2.Text = "关闭";

        //

        // menuItem3

        //

        this.menuItem3.Index = 2;

        this.menuItem3.Text = "开始";

        this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);

        //

        // menuItem4

        //

        this.menuItem4.Index = 3;

        this.menuItem4.Text = "停止";

        //

        // timer1

        //

        this.timer1.Interval = 1000;

        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

        //

        // Form1

        //

        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

        this.ClientSize = new System.Drawing.Size(800, 414);

        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

        this.Name = "Form1";

        this.ShowInTaskbar = false;

        this.Text = "Form1";

        this.Load += new System.EventHandler(this.Form1_Load);

      }

      #endregion

5、          启动定时器

private void menuItem3_Click(object sender, System.EventArgs e)

      {

        this.timer1.Start();

         }

6、         动态更换Icon图标

private void timer1_Tick(object sender, System.EventArgs e)

      {

        string path;

        path = @"D:"Src"i2"i1""+ico.ToString()+@".ico";

        this.notifyIcon1.Icon = new Icon(path);

        ico++;

        if(ico > 5)

        {

           ico =1;

        }

     }

转载于:https://www.cnblogs.com/duanjingdi/archive/2007/10/08/917195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值