VS2017 - Winform 简单托盘小程序
界面比较简单,主要两个button 一个NotifyIcon 和 右键菜单 控件,
NotifyIcon 属性,如下:
并为NotifyIcon指定了DoubleClick事件:
主窗体增加两个事件:
整体代码如下:
using Pallet_Common; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Pallet_01 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); this.btnEnd.Enabled = false; this.停止ToolStripMenuItem.Enabled = false; } private void MainForm_Load(object sender, EventArgs e) { this.btnEnd.Enabled = false; this.Resize += MainForm_Resize; } //单击窗体最小化时窗体隐藏 void MainForm_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.Hide(); } } private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) { this.Show(); // 窗体显现 this.WindowState = FormWindowState.Normal; //窗体回复正常大小 } private void btnStart_Click(object sender, EventArgs e) { Scheduler.StartUp(); this.btnStart.Enabled = false; this.btnEnd.Enabled = true; this.启动ToolStripMenuItem.Enabled = false; this.停止ToolStripMenuItem.Enabled = true; string pageSize = ConfigurationManager.AppSettings["PageSize"]; this.lblCount.Text = pageSize + " 条"; this.lblCount.Update(); } private void btnEnd_Click(object sender, EventArgs e) { Scheduler.Stop(); this.btnStart.Enabled = true; this.btnEnd.Enabled = false; this.启动ToolStripMenuItem.Enabled = true; this.停止ToolStripMenuItem.Enabled = false; this.lblCount.Text = "----"; this.lblCount.Update(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { //关闭所有的线程,释放占用内存 this.Dispose(); this.Close(); } else { e.Cancel = true; } } #region 右键菜单事件 private void 启动ToolStripMenuItem_Click(object sender, EventArgs e) { Scheduler.StartUp(); this.btnStart.Enabled = false; this.btnEnd.Enabled = true; this.启动ToolStripMenuItem.Enabled = false; this.停止ToolStripMenuItem.Enabled = true; } private void 停止ToolStripMenuItem_Click(object sender, EventArgs e) { Scheduler.Stop(); this.btnStart.Enabled = true; this.btnEnd.Enabled = false; this.启动ToolStripMenuItem.Enabled = true; this.停止ToolStripMenuItem.Enabled = false; } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { // 关闭所有的线程,释放占用内存 this.Dispose(); this.Close(); } } #endregion } }
其中打开一个程序后,不允许再次打开,也就是运行后,只能运行一个,如下:
using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Pallet_01 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { bool bCreateNew; Mutex m = new Mutex(false, "Pallent_01", out bCreateNew); if (bCreateNew) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)