如何制作仅在系统托盘中运行的 .NET Windows 窗体应用程序? [英] How can I make a .NET Windows Forms application that only runs in the System Tray?

查看:30
本文介绍了如何制作仅在系统托盘中运行的 .NET Windows 窗体应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What do I need to do to make a Windows Forms application to be able to run in the System Tray?

不是可以最小化到托盘的应用,而是只会存在于托盘中的应用,无外乎

Not an application that can be minimized to the tray, but an application that will be only exist in the tray, with nothing more than

  • 一个图标
  • 工具提示,以及
  • 右键单击";菜单.

推荐答案

代码项目文章 CreatingTasktray Application 给出了一个非常简单的解释和示例,用于创建一个只存在于系统托盘中的应用程序.

The code project article Creating a Tasktray Application gives a very simple explanation and example of creating an application that only ever exists in the System Tray.

基本上更改 Program.cs 中的 Application.Run(new Form1()); 行,改为启动一个继承自 ApplicationContext 的类code>,并让该类的构造函数初始化一个 NotifyIcon

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

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

这篇关于如何制作仅在系统托盘中运行的 .NET Windows 窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆