我需要做什么才能使Windows窗体应用程序能够在系统托盘中运行?

不是一个可以最小化到托盘的应用程序,而是一个只存在于托盘中的应用程序

一个图标 一个工具提示,和 一个“右键”菜单。


当前回答

正如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;
        }
    }

其他回答

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 :)

据我所知,您仍然必须使用表单编写应用程序,但在表单上没有控件,并且永远不会将其设置为可见。使用NotifyIcon(可以在这里找到它的MSDN示例)来编写应用程序。

在。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 };            

}

这是一个非常友好的框架通知区域应用程序…将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做到的

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.