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