首页 > 其他分享 >winform实现最小化至系统托盘

winform实现最小化至系统托盘

时间:2024-02-19 23:13:59浏览次数:28  
标签:sender ToolStripMenuItem 系统托盘 object 窗体 最小化 winform notifyIcon1 图标

NotifyIcon类介绍

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

示例

通过设计页面使用

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

image-20240219205552725

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

image-20240219214654690

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

image-20240219215003889

设置contextMenuStrip1:

image-20240219215048745

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

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

标签:sender,ToolStripMenuItem,系统托盘,object,窗体,最小化,winform,notifyIcon1,图标
From: https://www.cnblogs.com/mingupupu/p/18022142

相关文章

  • electron delphi winform wpf qt的对比
    Electron、Delphi、WinForms、WPF和Qt都是用于开发桌面应用程序的工具或框架,它们各自有一些独特的优点和适用场景。以下是对这些工具的简要对比:Electron:基于Web技术(HTML、CSS和JavaScript)的跨平台桌面应用程序开发框架。使用Chromium渲染引擎提供强大的页面渲染能力。适用......
  • C# WINFORM判断程序已经运行,切只能运行一个实例
    判断程序是否已经运行,使程序只能运行一个实例:方法1://这种检测进程的名的方法,并不绝对有效。因为打开第一个实例后,将运行文件改名后,还是可以运行第二个实例.privatestaticboolisAlreadyRunning(){boolb=false;Process[]mProcs=Process......
  • DevExpress WinForms中文教程 - 如何创建可访问的WinForms应用?(二)
    为用户创建易访问的WindowsForms应用程序不仅是最佳实践的体现,还是对包容性和以用户为中心的设计承诺。在应用程序开发生命周期的早期考虑与可访问性相关的需求可以节省长期运行的时间(因为它将决定设计决策和代码实现)。一个可访问的WinForms应用程序提供了各种好处,包括:扩大......
  • Winform中设置隐藏窗体且不在任务栏中显示
    场景Winform中设置程序开机自启动(修改注册表和配置自启动快捷方式):https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/135764400通过以上方式设置winform程序开机自启动之后,需要启动后判断配置文件如果不为空则窗体隐藏,任务后台进行。Winform中实现保存配置到文件/......
  • C# Winform窗体里面怎么打开exe程序
    C#Winform窗体里面怎么打开exe程序System.Diagnostics.Processprocess=newSystem.Diagnostics.Process();process.StartInfo.FileName="要调用的exe名称";process.StartInfo.WorkingDirectory=path//要掉用得exe路径例如:"C:\windows";process.StartInfo.CreateNoWi......
  • dotnet winform 显示echart图表
    1、安装WebView2  2、新建index.html并下载echart.min.js到本地<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=......
  • DevExpress WinForms中文教程 - 如何创建可访问的WinForms应用?(一)
    为用户创建易访问的WindowsForms应用程序不仅是最佳实践的体现,还是对包容性和以用户为中心的设计承诺。在应用程序开发生命周期的早期考虑与可访问性相关的需求可以节省长期运行的时间(因为它将决定设计决策和代码实现)。一个可访问的WinForms应用程序提供了各种好处,包括:扩大......
  • SysTrayIcon 改的 python tkinter 最小化至系统托盘,适用TTK
    网上的SysTrayIcon改的,Tk页面最小化至托盘,托盘图标左键单击恢复Tk界面1.点击最小化隐藏至托盘2.托盘图标右键菜单展示,左键返回Tk界面。托盘图标可以自定义,修改了SysTrayIcon更容易调用,Demo窗口加了注释,具体查看_Main类。代码如下:importwin32api,win32con,win32gui_str......
  • Winform仅允许运行一个程序,当要打开多个时将显示已在运行的程序
    要实现此功能,只需在Program类中修改启动程序的方法即可首先引用对应的命名空间usingSystem.Diagnostics;usingSystem.Reflection;usingSystem.Runtime.InteropServices;接着在类中,Main方法外写///<summary>///Themainentrypointfortheapplication./......
  • NSIS打包教程 Winform程序打包
    NSIS软件下载地址:https://pan.baidu.com/s/1sbBX__7Q4ntHeEDp-yl1ng提取码:xeax相关的视频教程https://www.bilibili.com/video/BV1jf4y1a7ji?share_source=copy_web(如果不会请看我录制的视频教程)NSIS打包教程Wnform程序打包,,安装部署步骤如下图 桌面快......