我需要做什么才能使 Windows Forms 应用程序能够在系统托盘中运行?
不是一个可以最小化到托盘的应用程序,而是一个只会存在于托盘中的应用程序,仅此而已
代码项目文章创建任务托盘应用程序给出了创建仅存在于系统托盘中的应用程序的非常简单的解释和示例。
基本上更改
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();
}
}
正如 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();
右键单击获取上下文菜单是自动处理的,但如果您想在左键单击上执行某些操作,则需要添加一个单击处理程序:
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,使用推荐的替代品来处理已弃用的类:
程序.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
。MenuItem
“退出”或类似内容。ContextMenu
绑定到 NotifyIcon
。Application.Run()
。MenuItem
的事件处理程序中,请务必先调用 set 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;
退出应用程序。“系统托盘”应用程序只是一个常规的Win Forms应用程序,唯一的区别是它在Windows系统托盘区域创建一个图标。为了创建sys.tray图标,需要使用NotifyIcon组件,您可以在工具箱(常用控件)中找到它,并修改它的属性:图标、工具提示。它还使您能够处理鼠标单击和双击消息。
还有一件事,为了实现外观和感觉或标准托盘应用程序。在主窗体显示事件中添加以下行:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
据我所知,您仍然必须使用表单编写应用程序,但表单上没有控件,并且永远不会将其设置为可见。使用 NotifyIcon(可在此处找到 MSDN 示例)来编写您的应用程序。
对于通知区域应用程序来说,这是非常友好的框架...将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
实现的方法ContextMenuStrip
'exit' (double click)
-> handler
-> me.Close()
NotifyIcon
,在设计器中将contextMenuStrip
设置为您刚刚创建的图标,选择一个图标(您可以在Visual Studio文件夹中的“common7...”下找到一些图标)FormBorderStyle:none
、ShowIcon:false
、ShowInTaskbar:false
、Opacity:0%
、WindowState:Minimized
Me.Visible=false
的末尾添加Form1_Load
,这将隐藏图标
使用 Ctrl + Tab在 .Net 6 中,我必须像这样努力工作:
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 };
}
您可以创建表单,对其进行修改,然后将其作为参数传递给
Application.Run
。 :
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form1();
form.Hide();
form.Opacity = 0;
form.ShowInTaskbar = false;
Application.Run(form);
}
}
在设计时将
NotifyIcon
和 ContextMenu
(如果需要)添加到表单中作为常规应用程序。确保您的 Notifyicon
是 Visible
并且有关联的图标。这还可以让您使用以后因任何原因可能需要的表格
只需添加
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
到您的表单对象。 您只会在系统托盘中看到一个图标。
notifyIcon1->ContextMenu = gcnew
System::Windows::Forms::ContextMenu();
System::Windows::Forms::MenuItem^ nIItem = gcnew
System::Windows::Forms::MenuItem("Open");
nIItem->Click += gcnew System::EventHandler(this, &your_class::Open_NotifyIcon);
notifyIcon1->ContextMenu->MenuItems->Add(nIItem);