C# 如何制作仅在系统托盘中运行的 .NET Windows 窗体应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/995195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a .NET Windows Forms application that only runs in the System Tray?
提问by xyz
What do I need to do to make a Windows Formsapplication run in the System Tray?
我需要做什么才能使Windows 窗体应用程序在系统托盘中运行?
Not an application that can minimize to the tray, but one that exists only in the tray, with nothing more than an icon, tool tip, and "right click" menu.
不是一个可以最小化到托盘的应用程序,而是一个只存在于托盘中的应用程序,只有一个图标、工具提示和“右键单击”菜单。
回答by Matthew Steeples
As far as I'm aware you have to still write the application using a form, but have no controls on the form and never set it visible. Use the NotifyIcon (an MSDN sample of which can be found here) to write your application.
据我所知,您仍然必须使用表单编写应用程序,但在表单上没有控件并且永远不要将其设置为可见。使用 NotifyIcon(可以在此处找到 MSDN 示例)来编写您的应用程序。
回答by Gordon Freeman
"System tray" application is just a regular win forms application, only difference is that it creates a icon in windows system tray area. In order to create sys.tray icon use NotifyIcon component , you can find it in Toolbox(Common controls), and modify it's properties: Icon, tool tip. Also it enables you to handle mouse click and double click messages.
“系统托盘”应用程序只是一个普通的win 窗体应用程序,唯一的区别是它在windows 系统托盘区域创建了一个图标。为了使用 NotifyIcon 组件创建 sys.tray 图标,您可以在 Toolbox(Common controls) 中找到它,并修改它的属性:图标,工具提示。它还使您能够处理鼠标单击和双击消息。
And One more thing , in order to achieve look and feels or standard tray app. add followinf lines on your main form show event:
还有一件事,为了实现外观或标准托盘应用程序。在主窗体显示事件中添加以下行:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
回答by ChrisF
As mat1t says - you need to add a NotifyIcon to your application and then use something like the following code to set the tooltip and context menu:
正如 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 code shows the icon in the system tray only:
此代码仅在系统托盘中显示图标:
this.notifyIcon.Visible = true; // Shows the notify icon in the system tray
The following will be needed if you have a form (for whatever reason):
如果您有表格(无论出于何种原因),则需要以下内容:
this.ShowInTaskbar = false; // Removes the application from the taskbar
Hide();
The right click to get the context menu is handled automatically, but if you want to do some action on a left click you'll need to add a Click handler:
右键单击以获取上下文菜单是自动处理的,但是如果您想在左键单击时执行某些操作,则需要添加一个 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;
}
}
回答by M.Turrini
I've wrote a traybar app with .NET 1.1 and I didn't need a form.
First of all, set the startup object of the project as a Sub Main
, defined in a module.
Then create programmatically the components: the NotifyIcon
and ContextMenu
.
Be sure to include a MenuItem
"Quit" or similar.
Bind the ContextMenu
to the NotifyIcon
.
Invoke Application.Run()
.
In the event handler for the Quit MenuItem
be sure to call set NotifyIcon.Visible = False
, then Application.Exit()
.
Add what you need to the ContextMenu
and handle properly :)
我用 .NET 1.1 编写了一个托盘栏应用程序,我不需要表单。
首先,将项目的启动对象设置为一个 Sub Main
,定义在一个模块中。
然后以编程方式创建组件:NotifyIcon
和ContextMenu
。
请务必包含MenuItem
“退出”或类似内容。
将 绑定ContextMenu
到NotifyIcon
。
调用Application.Run()
.
在 Quit 的事件处理程序中,MenuItem
一定要调用 set NotifyIcon.Visible = False
,然后Application.Exit()
。添加您需要的内容ContextMenu
并正确处理:)
回答by Wolf5
- Create a new Windows Application with the wizard.
- Delete
Form1
from the code. - Remove the code in Program.cs starting up the
Form1
. - Use the
NotifyIcon
class to create your system tray icon (assign an icon to it). - Add a contextmenu to it.
- Or react to
NotifyIcon
's mouseclick and differenciate between Right and Left click, setting your contextmenu and showing it for which ever button (right/left) was pressed. Application.Run()
to keep the app running withApplication.Exit()
to quit. Or abool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}
. Then setbRunning = false;
to exit the app.
- 使用向导创建一个新的 Windows 应用程序。
Form1
从代码中删除。- 删除 Program.cs 中启动
Form1
. - 使用
NotifyIcon
该类创建您的系统托盘图标(为其分配一个图标)。 - 为其添加上下文菜单。
- 或者对
NotifyIcon
的鼠标单击做出反应并区分右键单击和左键单击,设置上下文菜单并显示按下哪个按钮(右/左)。 Application.Run()
保持应用程序运行Application.Exit()
以退出。或者一个bool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}
. 然后设置bRunning = false;
退出应用程序。
回答by Evolved
Here is how I did it with Visual Studio 2010, .NET 4
这是我如何使用Visual Studio 2010、 .NET 4
- Create a Windows Forms Application, set 'Make single instance application' in properties
- Add a ContextMenuStrip
- Add some entries to the context menu strip, double click on them to get the handlers, for example, 'exit' (double click) -> handler -> me.Close()
- Add a NotifyIcon, in the designer set contextMenuStrip to the one you just created, pick an icon (you can find some in the VisualStudio folder under 'common7...')
- Set properties for the form in the designer: FormBorderStyle:none, ShowIcon:false, ShowInTaskbar:false, Opacity:0%, WindowState:Minimized
- Add Me.Visible=false at the end of Form1_Load, this will hide the icon when using Ctrl+ Tab
- Run and adjust as needed.
- 创建一个 Windows 窗体应用程序,在属性中设置“制作单实例应用程序”
- 添加 ContextMenuStrip
- 向上下文菜单条添加一些条目,双击它们以获取处理程序,例如,'exit'(双击)-> handler -> me.Close()
- 添加一个 NotifyIcon,在设计器中将 contextMenuStrip 设置为你刚刚创建的那个,选择一个图标(你可以在 VisualStudio 文件夹中的“common7...”下找到一些)
- 在设计器中为表单设置属性:FormBorderStyle:none、ShowIcon:false、ShowInTaskbar:false、Opacity:0%、WindowState:Minimized
- 在Form1_Load的末尾添加Me.Visible=false,这样使用Ctrl+时会隐藏图标Tab
- 根据需要运行和调整。
回答by Fawzan Izy
The code project article Creating a Tasktray Applicationgives a very simple explanation and example of creating an application that only ever exists in the System Tray.
代码项目文章创建任务托盘应用程序给出了创建只存在于系统托盘中的应用程序的非常简单的解释和示例。
Basically change the Application.Run(new Form1());
line in Program.cs
to instead start up a class that inherits from ApplicationContext
, and have the constructor for that class initialize a 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();
}
}
回答by Sebastian Xawery Wi?niowiecki
It is very friendly framework for Notification Area Application... it is enough to add NotificationIcon to base form and change auto-generated code to code below:
这是一个非常友好的通知区域应用程序框架……将 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;
}
}
}
回答by YTerle
Simply add
只需添加
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
to your form object. You will see only an icon at system tray.
到您的表单对象。您只会在系统托盘上看到一个图标。