Logo

WindowsForm托盘实现

photo

2024年06月25日

创建一个空白的窗体也可以
在这里插入图片描述

拖放NotifyIcon到窗体中,然后右击属性,在Text取个应用名称,再拖一个ContextMenuStrip到窗体中
在这里插入图片描述
在这里插入图片描述
创建一个“退出”菜单,双击下生成退出事件,然后在退出事件中编写代码退出应用程序

  //退出
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
             this.Invoke(()=> {
                if (cancellToken != null)
                {
                    cancellToken.Cancel(true);
                    task1?.Dispose();
                }
                //停止接收消息
                RocketHelpter.StopConsumer();
            });

            //退出         
            Application.Exit();
        }

在notifyIcon1属性栏,选择关联右键菜单
在这里插入图片描述
Form1.cs代码参考:

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 WindowsFormsPrint
{
    public partial class Form1 : Form
    {
        public static Task task1 = null;
        public static CancellationTokenSource cancellToken = new CancellationTokenSource();

        /// <summary>
        /// 是否退出了?1=是,0=否
        /// </summary>
        public static int isExit = 0;

        public Form1()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Minimized;
            //隐藏任务栏区图标
            this.ShowInTaskbar = false;
            不显示
            this.Hide();
            //图标显示在托盘区
            notifyIcon1.Visible = true;
            //图标设置
            notifyIcon1.Icon = new Icon(AppDomain.CurrentDomain.BaseDirectory + "\\print_pref.ico");

            //启动接收
            task1 = Task.Run(() =>
            {
                // 接收RocketMQ打印消息
                RocketHelpter.ReviceMsg();
            }, cancellToken.Token);
        }

        //退出
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isExit = 1;
            this.Invoke(()=> {
                if (cancellToken != null)
                {
                    cancellToken.Cancel(true);
                    task1?.Dispose();
                }
                //停止接收消息
                RocketHelpter.StopConsumer();
            });

            //退出         
            Application.Exit();
        }


    }
}

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
本文为原创文章,请注意保留出处!

热门文章

Windows Server IIS+ARR反向代理(配置反向代理服务器) 1.概念说明:反向代理反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相...WindowsServerIIS+ARR反向代理(配置反向代理服务器) 作者:Pastore Antonio
1581 浏览量
ffmpeg 生成水印 1:先要配置ffmpeg的滤镜:参考:https://www.jianshu.com/p/9d24...ffmpeg生成水印 作者:Pastore Antonio
1508 浏览量
C#中List的FindAll方法的正确打开方式 初略的介绍一种常见的List写法,这种写法在3.0以后其实是很简单的,但是在2.0左右的系统运用中还...C#中List的FindAll方法的正确打开方式 作者:Pastore Antonio
1467 浏览量
IntelliJ IDEA 代码字体大小的快捷键设置放大缩小(很实用)(图文详解) 这是在设置IntelliJIDEA...IntelliJIDEA代码字体大小的快捷键设置放大缩小(很实用)(图文详解) 作者:Pastore Antonio
1461 浏览量
Navicat Premium 12.0.22 安装与破解 一、安装  NavicatPremium12.0.22的下载链接:https://pan.ba...NavicatPremium12.0.22安装与破解 作者:Pastore Antonio
1447 浏览量