如何制作仅在系统托盘中运行的 .NET Windows 窗体应用程序?

问题描述 投票:0回答:13

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

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

  • 一个图标
  • 工具提示,以及
  • 右键单击”菜单。
c# .net winforms system-tray
13个回答
148
投票

代码项目文章创建任务托盘应用程序给出了创建仅存在于系统托盘中的应用程序的非常简单的解释和示例。

基本上更改

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

19
投票

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

18
投票

.NET 核心

我将已接受的答案改编为.NET Core,使用推荐的替代品来处理已弃用的类:

  • ContextMenu -> ContextMenuStrip
  • 菜单项 -> ToolStripMenuItem

程序.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();
        }
    }
}

16
投票

我用 .NET 1.1 编写了一个托盘栏应用程序,并且不需要表单。
首先,将项目的启动对象设置为Sub

Main
,定义在模块中。
然后以编程方式创建组件:
NotifyIcon
ContextMenu

请务必包含
MenuItem
“退出”或类似内容。
ContextMenu
绑定到
NotifyIcon

调用
Application.Run()

在 Quit
MenuItem
的事件处理程序中,请务必先调用 set
NotifyIcon.Visible = False
,然后调用
Application.Exit()
。 将您需要的添加到
ContextMenu
并妥善处理:)


14
投票
  1. 使用向导创建新的 Windows 应用程序。
  2. 从代码中删除
    Form1
  3. 删除 Program.cs 中启动
    Form1
    的代码。
  4. 使用
    NotifyIcon
    类创建系统托盘图标(为其分配一个图标)。
  5. 为其添加上下文菜单。
  6. 或者对
    NotifyIcon
    的鼠标单击做出反应并区分右键单击和左键单击,设置上下文菜单并显示按下的按钮(右/左)。
  7. Application.Run()
    使应用程序保持运行,使用
    Application.Exit()
    退出。或者一个
    bool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}
    。然后设置
    bRunning = false;
    退出应用程序。

13
投票

“系统托盘”应用程序只是一个常规的Win Forms应用程序,唯一的区别是它在Windows系统托盘区域创建一个图标。为了创建sys.tray图标,需要使用NotifyIcon组件,您可以在工具箱(常用控件)中找到它,并修改它的属性:图标、工具提示。它还使您能够处理鼠标单击和双击消息。

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

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

8
投票

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


4
投票

对于通知区域应用程序来说,这是非常友好的框架...将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;
        }
    }
}

4
投票

这是我使用 Visual Studio 2010、.NET 4

实现的方法
  1. 创建 Windows 窗体应用程序,在属性中设置“创建单实例应用程序”
  2. 添加一个
    ContextMenuStrip
  3. 向上下文菜单条添加一些条目,双击它们以获取处理程序,例如,
    'exit' (double click)
    ->
    handler
    ->
    me.Close()
  4. 添加一个
    NotifyIcon
    ,在设计器中将
    contextMenuStrip
    设置为您刚刚创建的图标,选择一个图标(您可以在Visual Studio文件夹中的“common7...”下找到一些图标)
  5. 在设计器中设置表单的属性:
    FormBorderStyle:none
    ShowIcon:false
    ShowInTaskbar:false
    Opacity:0%
    WindowState:Minimized
  6. Me.Visible=false
    的末尾添加
    Form1_Load
    ,这将隐藏图标 使用 Ctrl + Tab
  7. 根据需要运行并调整。

3
投票

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

}

1
投票

您可以创建表单,对其进行修改,然后将其作为参数传递给

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
并且有关联的图标。这还可以让您使用以后因任何原因可能需要的表格


0
投票

只需添加

this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;

到您的表单对象。 您只会在系统托盘中看到一个图标。


0
投票
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);
© www.soinside.com 2019 - 2024. All rights reserved.