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


    }
}

橙子主题打折出售

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

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

热门文章

WordPress 后台编辑器样式实现直接预览 在WordPress3.0以后,有一个新的实用功能:你可以更改默认后台编辑器(TinyMCE)的样...WordPress后台编辑器样式实现直接预览 作者:Pastore Antonio
1451 浏览量
【干货】Chrome插件(扩展)开发全攻略 写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的...【干货】Chrome插件(扩展)开发全攻略 作者:Pastore Antonio
1415 浏览量
CentOS 编译错误+配置错误解决方法集合 ERROR:theHTTPXSLTmodulerequiresthelibxml2/l...CentOS编译错误+配置错误解决方法集合 作者:Pastore Antonio
1409 浏览量
WordPress中加载JavaScript脚本的方法 在WordPress中加载脚本(为CSS和JS,下同)文件,大多数人的做法是直接在hea...WordPress中加载JavaScript脚本的方法 作者:Pastore Antonio
1385 浏览量
wordpress学习五: 通过wordpress_xmlrpc的python包远程操作wordpress wordpress提供了丰富的xmlrpc接口api来供我们远程操控wp的内容。伟大的开源社区有人就...wordpress学习五:通过wordpress_xmlrpc的python包远程操作wordpress 作者:Pastore Antonio
1382 浏览量