【学习笔记】C# 动态系统托盘图标的实现 - NotifyIcon控件

操作步骤:

1、创建一个C# Windows窗体应用项目,命名为“IconTwinkle”:

2、双击解决方案中的“Resources.resx”,点击“添加资源”,选择图标并导入:

3、可见选定的图标已导入项目中:

4、双击右侧工具箱中的控件ContextMenuStrip、NotifyIcon、Timer,以将其添加至窗体中:

5、设置“contextMenuStrip1”,添加4个列表项,将Text属性分别改为“显示、隐藏、闪烁、退出”:

6、设置“notifyIcon1”,将ContextMenuStrip属性改为“contextMenuStrip1”、Text属性改为“这里是托盘图标!”、Icon属性设置为“ichat.ico”:

7、编写相应事件的功能代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IconTwinkle
{
    public partial class Form1 : Form
    {
        private Icon blank = Properties.Resources.blank;
        private Icon normal = Properties.Resources.ichat;
        private bool _status = true;
        private bool _isBlank = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Visible = true;
        }
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (_isBlank == false)
            {
                _isBlank = true;
                timer1.Enabled = true;
                timer1.Start();
            }
            else
            {
                _isBlank = false;
                timer1.Stop();
                //气泡提示
                notifyIcon1.ShowBalloonTip(5000, "提示", "关闭闪烁效果!", ToolTipIcon.Info);
            }
        }
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        

        //定时器中修改图标的状态,定时反转图标
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_status)
            {
                notifyIcon1.Icon = normal;

            }
            else
            {
                notifyIcon1.Icon = blank;
            }
            _status = !_status;
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
//本段代码部分来源于网络,感谢大佬~

8、运行测试:

  

耶!大功告成啦!✌

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值