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

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

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


当前回答

您可以创建表单,修改它,然后将它传递给应用程序。作为参数运行。:

    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);
        }
    }

添加你的通知图标和上下文菜单(如果需要)到你的表单在设计时作为一个常规的应用程序。确保你的通知图标是可见的,并有一个相关的图标。这还将允许您使用以后可能出于任何原因需要的表单

其他回答

下面是我如何用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.

我将接受的答案改编为。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();
        }
    }
}

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

“系统托盘”应用程序只是一个普通的win表单应用程序,唯一的区别是它在windows系统托盘区域创建了一个图标。为了创建sys。托盘图标使用NotifyIcon组件,你可以在工具箱(常用控件)中找到它,并修改它的属性:图标,工具提示。它还允许您处理鼠标单击和双击消息。

还有一件事,为了实现外观和感觉或标准托盘应用程序。添加followinf行在你的主窗体显示事件:

private void MainForm_Shown(object sender, EventArgs e)
{
    WindowState = FormWindowState.Minimized;
    Hide();
}