• C#如何使用NotifyIcon类实现最小化至系统托盘效果
  • 发布于 1周前
  • 50 热度
    0 评论
NotifyIcon类介绍
NotifyIcon 是 .NET中的一个类,它用于在系统托盘中显示图标。这个类在 System.Windows.Forms 命名空间下。使用 NotifyIcon 类,你可以在系统托盘中创建一个图标,当用户点击或右键点击这个图标时,可以触发一些事件。例如,你可以创建一个上下文菜单(右键菜单),或者当用户双击图标时打开一个窗口。

示例
通过设计页面使用
1.在设计页面中拖拽添加NotifyIcon:

2.进行相关设置(在后面通过代码使用时会进行介绍):

3.这里的contextMenuStrip1也是由自己拖拽来的:

4.设置contextMenuStrip1:

5.重写窗体关闭事件处理程序:
 protected override void OnFormClosing(FormClosingEventArgs e)
 {
     if (e.CloseReason == CloseReason.UserClosing)
     {
         e.Cancel = true;  // 取消关闭窗体
         this.Hide();  // 隐藏窗体
         this.notifyIcon1.Visible = true;  // 显示托盘图标
     }
 }
6.双击notifyIcon1写鼠标双击事件处理程序:
 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     this.Show();  // 显示窗体
     this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
     this.notifyIcon1.Visible = false;  // 隐藏托盘图标
 }
7.双击显示窗体按钮,写点击事件处理程序:
 private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     this.Show();  // 显示窗体
     this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
     this.notifyIcon1.Visible = false;  // 隐藏托盘图标
 }
8.双击显示气泡按钮,写点击事件处理程序:
 private void 显示气泡2ToolStripMenuItem_Click(object sender, EventArgs e)
 {
    // 堆代码 duidaima.com
     // 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
     notifyIcon1.ShowBalloonTip(3000);
 }
9.双击退出按钮,写点击事件处理程序:
 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     Application.Exit();  // 退出应用程序
 }
10.查看实现效果:

winform实现最小至系统托盘效果

全部代码:
namespace Minimized_to_the_system_tray_demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;  // 取消关闭窗体
                this.Hide();  // 隐藏窗体
                this.notifyIcon1.Visible = true;  // 显示托盘图标
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();  // 显示窗体
            this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
            this.notifyIcon1.Visible = false;  // 隐藏托盘图标
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();  // 显示窗体
            this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
            this.notifyIcon1.Visible = false;  // 隐藏托盘图标
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();  // 退出应用程序
        }

        private void 显示气泡2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
            notifyIcon1.ShowBalloonTip(3000);
        }
    }
}
通过代码实现
1.首先全局声明一个NotifyIcon对象与一个ContextMenuStrip对象:
 private NotifyIcon notifyIcon1;
 private ContextMenuStrip menuStrip;
2.menuStrip的相关设置:
 // 创建 ContextMenuStrip。
 this.menuStrip = new ContextMenuStrip();

 // 创建并初始化 ToolStripMenuItem 对象。
 ToolStripMenuItem item1 = new ToolStripMenuItem("显示窗体");
 item1.Click += (object? sender, EventArgs e) => 
 {
     this.Show();  // 显示窗体
     this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
     this.notifyIcon1.Visible = false;  // 隐藏托盘图标
 };
 ToolStripMenuItem item2 = new ToolStripMenuItem("显示气泡");
 item2.Click += (object? sender, EventArgs e) => 
 {
     // 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
     notifyIcon1.ShowBalloonTip(3000);
 };
 ToolStripMenuItem item3 = new ToolStripMenuItem("退出");
 item3.Click += (object? sender, EventArgs e) => 
 {
     Application.Exit();  // 退出应用程序
 };

 // 将 ToolStripMenuItem 对象添加到 ContextMenuStrip 的 Items 集合中。
 this.menuStrip.Items.Add(item1);
 this.menuStrip.Items.Add(item2);
 this.menuStrip.Items.Add(item3);
3.notifyIcon1的相关设置:
 // 创建 NotifyIcon。
 this.notifyIcon1 = new NotifyIcon();

// Icon 属性设置将在系统托盘中显示的图标。
notifyIcon1.Icon = new Icon("你的ico图标路径"");

// ContextMenu 属性设置当右键点击系统托盘图标时显示的菜单。
notifyIcon1.ContextMenuStrip = this.menuStrip;

// Text 属性设置当鼠标悬停在系统托盘图标上时显示的提示文本。
notifyIcon1.Text = "最小化至系统托盘示例程序";
notifyIcon1.Visible = true;

// 气泡提示相关设置
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipTitle = "提示";
notifyIcon1.BalloonTipText = "您有一条新消息";

// 注册鼠标双击事件                           
notifyIcon1.MouseDoubleClick += NotifyIcon1_MouseDoubleClick;
4.notifyIcon1鼠标双击事件处理程序:
 private void NotifyIcon1_MouseDoubleClick(object? sender, MouseEventArgs e)
 {
     this.Show();  // 显示窗体
     this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
     this.notifyIcon1.Visible = false;  // 隐藏托盘图标
 }
5.重写窗体关闭事件处理程序:
protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;  // 取消关闭窗体
        this.Hide();  // 隐藏窗体
        this.notifyIcon1.Visible = true;  // 显示托盘图标
    }
}
实现效果与上述相同。
全部代码:
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 Minimized_to_the_system_tray_demo
{
    public partial class Form2 : Form
    {
        private NotifyIcon notifyIcon1;
        private ContextMenuStrip menuStrip;
        public Form2()
        {
            InitializeComponent();

            // 创建 NotifyIcon。
            this.notifyIcon1 = new NotifyIcon();

            // 创建 ContextMenuStrip。
            this.menuStrip = new ContextMenuStrip();

            // 创建并初始化 ToolStripMenuItem 对象。
            ToolStripMenuItem item1 = new ToolStripMenuItem("显示窗体");
            item1.Click += (object? sender, EventArgs e) => 
            {
                this.Show();  // 显示窗体
                this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
                this.notifyIcon1.Visible = false;  // 隐藏托盘图标
            };
            ToolStripMenuItem item2 = new ToolStripMenuItem("显示气泡");
            item2.Click += (object? sender, EventArgs e) => 
            {
                // 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
                notifyIcon1.ShowBalloonTip(3000);
            };
            ToolStripMenuItem item3 = new ToolStripMenuItem("退出");
            item3.Click += (object? sender, EventArgs e) => 
            {
                Application.Exit();  // 退出应用程序
            };

            // 将 ToolStripMenuItem 对象添加到 ContextMenuStrip 的 Items 集合中。
            this.menuStrip.Items.Add(item1);
            this.menuStrip.Items.Add(item2);
            this.menuStrip.Items.Add(item3);

            

            // Icon 属性设置将在系统托盘中显示的图标。
            notifyIcon1.Icon = new Icon("你的ico图标路径");

            // ContextMenu 属性设置当右键点击系统托盘图标时显示的菜单。
            notifyIcon1.ContextMenuStrip = this.menuStrip;

            // Text 属性设置当鼠标悬停在系统托盘图标上时显示的提示文本。
            notifyIcon1.Text = "最小化至系统托盘示例程序";
            notifyIcon1.Visible = true;

            notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
            notifyIcon1.BalloonTipTitle = "提示";
            notifyIcon1.BalloonTipText = "您有一条新消息";

            notifyIcon1.MouseDoubleClick += NotifyIcon1_MouseDoubleClick;

        }

        private void NotifyIcon1_MouseDoubleClick(object? sender, MouseEventArgs e)
        {
            this.Show();  // 显示窗体
            this.WindowState = FormWindowState.Normal;  // 恢复窗体正常大小
            this.notifyIcon1.Visible = false;  // 隐藏托盘图标
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;  // 取消关闭窗体
                this.Hide();  // 隐藏窗体
                this.notifyIcon1.Visible = true;  // 显示托盘图标
            }
        }
    }
}

用户评论