我需要做什么才能使Windows 窗体应用程序能够在系统托盘中运行?
不是可以最小化到托盘的应用程序,而是只会存在于托盘中的应用程序,无非就是
代码项目文章创建一个任务托盘应用程序提供了一个非常简单的解释和创建一个只存在于系统托盘中的应用程序的示例。
基本上将Application.Run(new Form1());行更改为Program.cs启动一个继承自的类ApplicationContext,并让该类的构造函数初始化一个NotifyIcon
Application.Run(new Form1());
Program.cs
ApplicationContext
NotifyIcon
static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MyCustomApplicationContext()); } } public class MyCustomApplicationContext : ApplicationContext { private NotifyIcon trayIcon; public MyCustomApplicationContext () { // Initialize Tray Icon trayIcon = new NotifyIcon() { Icon = Resources.AppIcon, ContextMenu = new ContextMenu(new MenuItem[] { new MenuItem("Exit", Exit) }), Visible = true }; } void Exit(object sender, EventArgs e) { // Hide tray icon, otherwise it will remain shown until user mouses over it trayIcon.Visible = false; Application.Exit(); } }