LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】winform实现最小化至系统托盘

admin
2024年3月8日 15:7 本文热度 305

NotifyIcon类介绍

NotifyIcon 是 .NET中的一个类,它用于在系统托盘中显示图标。这个类在 System.Windows.Forms 命名空间下。使用 NotifyIcon 类,你可以在系统托盘中创建一个图标,当用户点击或右键点击这个图标时,可以触发一些事件。例如,你可以创建一个上下文菜单(右键菜单),或者当用户双击图标时打开一个窗口。

示例

通过设计页面使用

在设计页面中拖拽添加NotifyIcon:

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

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

设置contextMenuStrip1:

重写窗体关闭事件处理程序:

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

双击notifyIcon1写鼠标双击事件处理程序:

 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 显示气泡2ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     
// 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
     notifyIcon1.ShowBalloonTip(
3000);
 }

双击退出按钮,写点击事件处理程序:

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

查看实现效果:

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);
        }
    }
}

通过代码实现

首先全局声明一个NotifyIcon对象与一个ContextMenuStrip对象:

 private NotifyIcon notifyIcon1;
 
private ContextMenuStrip menuStrip;

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);

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;

notifyIcon1鼠标双击事件处理程序:

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

重写窗体关闭事件处理程序:

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;  // 显示托盘图标
            }
        }
    }
}


该文章在 2024/3/8 15:17:09 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved