• C# 托盘通知


    一 托盘通知

    NotifyIcon托盘通知:
    程序可以在通知区创建一个通知图标。一般地,可以提示一个气泡通知,右键菜单支持。
    准备:需要一个图标(*.ico),用于显示在通知区。如果没有ico格式,可以将png图片转成ico。

    演示:在项目中添加托盘通知。
    ① 设置文本Text;
    ② 选择图标Icon;

    默认地,图标资源保存在Form1.resx里,其实也可以使用全局图标资源。

    运行程序,观察托盘区的图标。

    二 气泡通知

    气泡通知:BalloonTip

    演示:点击下载时,开启一个下载任务。下载完成时,在托盘处显示气泡通知。点击气泡通知时,显示主窗口。
    代码实现:

    notifyIcon1.ShowBalloonTip(
    timeout,
    title,
    text,
    icon);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 气泡通知
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if(e.CloseReason==CloseReason.UserClosing)
                {
                    e.Cancel = true;
                    this.Hide();
                }
            }
    
            private void 显示主窗口ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Show();
                this.Activate();
            }
            //模拟下载
            private void button1_Click(object sender, EventArgs e)
            {
                this.Hide();
    
                Thread th = new Thread(this.DownloadTask);
                th.Start();
            }
    
            private void DownloadTask()
            {
                for(int i=0;i<5;i++)
                {
                    Thread.Sleep(1000);
                    int percent = i * 100 / 5;
                    Console.WriteLine("模拟下载任务:{0}%", i);
                }
                OnTaskFinished();
            }
    
            //下载任务完成时,在托盘处显示气泡通知
            private void OnTaskFinished()
            {
                if(this.InvokeRequired)
                {
                    this.Invoke(new Action(this.OnTaskFinished));
                    return;
                }
    
                this.notifyIcon1.ShowBalloonTip(0,
                    "下载完成",
                    "下载XXXX.ZIP已经完成",
                    ToolTipIcon.Info);
            }
            //用户点击气泡通知时,显示主窗口
            private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
            {
                this.Show();
                this.Activate();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
  • 相关阅读:
    AM@幂级数性质@幂级数和函数求解
    Java手写约瑟夫问题算法和约瑟夫问题算法应用拓展案例
    接口自动化测试的概述及流程梳理~
    【JavaWeb】你这么厉害,知道RBAC权限模型和ABAC权限模型吗?
    python入门(10)面向对象 :类的特殊方法
    EasyBIM增强包重磅上新:无损轻量化,多场景应用.....免费参与内测!
    [02] BLEMotion-Kit 基于QMI8658传感器使用加速度计进行倾斜检测
    纯JS,RSA,AES,公钥,私钥生成及加解密
    【尚庭公寓SpringBoot + Vue 项目实战】租约管理(十四)
    Java毕业设计-社区诊所管理系统
  • 原文地址:https://blog.csdn.net/weixin_42291376/article/details/128192762