这篇来自Code Project的文章"Creating a Tasktray Application"提供了一个非常简单的解释和示例,展示了如何创建一个仅存在于系统托盘中的应用程序。
基本思路是将Program.cs
中的Application.Run(new Form1());
替换为启动从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();
}
}
就像mat1t所说的那样 - 你需要向应用程序添加一个NotifyIcon,然后使用以下代码设置工具提示和上下文菜单:
this.notifyIcon.Text = "This is the tooltip";
this.notifyIcon.ContextMenu = new ContextMenu();
this.notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Option 1", new EventHandler(handler_method)));
这段代码仅在系统托盘中显示图标:
this.notifyIcon.Visible = true; // Shows the notify icon in the system tray
如果您有一个表单(无论出于何种原因),则需要以下内容:this.ShowInTaskbar = false; // Removes the application from the taskbar
Hide();
右键获取上下文菜单是自动处理的,但如果想在左键单击时执行某些操作,则需要添加Click处理程序:
private void notifyIcon_Click(object sender, EventArgs e)
{
var eventArgs = e as MouseEventArgs;
switch (eventArgs.Button)
{
// Left click to reactivate
case MouseButtons.Left:
// Do your stuff
break;
}
}
我将被接受的答案改编为.NET Core,并使用推荐的替代方案来替换已弃用的类:
Program.cs
namespace TrayOnlyWinFormsDemo
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new MyCustomApplicationContext());
}
}
}
MyCustomApplicationContext.cs
using TrayOnlyWinFormsDemo.Properties; // Needed for Resources.AppIcon
namespace TrayOnlyWinFormsDemo
{
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
public MyCustomApplicationContext()
{
trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenuStrip = new ContextMenuStrip()
{
Items = { new ToolStripMenuItem("Exit", null, Exit) }
},
Visible = true
};
}
void Exit(object? sender, EventArgs e)
{
trayIcon.Visible = false;
Application.Exit();
}
}
}
我使用.NET 1.1编写了一个托盘应用,没有必要使用窗体。
首先,将项目的启动对象设置为在模块中定义的Sub Main
。
然后,通过编程方式创建组件:NotifyIcon
和ContextMenu
。
确保包含一个名为“Quit”或类似的MenuItem
。
将ContextMenu
绑定到NotifyIcon
。
调用Application.Run()
。
在退出MenuItem
的事件处理程序中,确保调用NotifyIcon.Visible = False
,然后调用Application.Exit()
。
根据需要添加内容到ContextMenu
并进行适当处理 :)
Form1
。Form1
的代码。NotifyIcon
类创建系统托盘图标(为其分配一个图标)。NotifyIcon
的鼠标单击事件,并区分右键和左键单击,为按下的任何按钮(右键/左键)设置上下文菜单并显示它。Application.Run()
保持应用程序运行,并使用Application.Exit()
退出。或者使用bool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}
,然后设置bRunning = false;
以退出应用程序。"系统托盘"应用程序只是普通的Windows窗体应用程序,唯一的区别是它在Windows系统托盘区域创建了一个图标。要创建系统托盘图标,请使用NotifyIcon组件,在工具箱(常用控件)中可以找到它,并修改其属性:图标、工具提示。此外,它还使您能够处理鼠标单击和双击消息。
还有一件事,在主窗体显示事件中添加以下行以实现外观和标准托盘应用程序:
"private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
这是一个非常友好的通知区域应用框架... 只需在基础表单中添加NotificationIcon并将自动生成的代码更改为以下代码即可:
public partial class Form1 : Form
{
private bool hidden = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
//this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
if (hidden) // this.WindowState == FormWindowState.Minimized)
{
// this.WindowState = FormWindowState.Normal;
this.Show();
hidden = false;
}
else
{
// this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
}
}
以下是我如何使用 Visual Studio 2010 和 .NET 4 实现的
private NotifyIcon trayIcon;
private ContextMenuStrip contextMenu1;
private ToolStripMenuItem menuItem1;
public MyCustomApplicationContext()
{
contextMenu1 = new System.Windows.Forms.ContextMenuStrip();
menuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(Exit);
this.contextMenu1.Items.AddRange(
new System.Windows.Forms.ToolStripMenuItem[] {this.menuItem1 });
trayIcon = new NotifyIcon(){Icon = Resources.AppIcon, ContextMenuStrip = this.contextMenu1, Visible = true };
}
icon.Visible = false
,然后Dispose()
该图标。否则,您将在程序退出后仍然看到该图标。测试几次后,您将不再知道哪个图标是真实的。 - Tomasz Gandor