WPF如何实现应用程序托盘

在WPF中实现应用程序托盘图标和菜单功能通常需要使用System.Windows.Forms.NotifyIcon类,因为WPF本身并没有直接提供这样的控件。为了使用NotifyIcon,你需要添加对System.Windows.Forms的引用。以下是如何实现的步骤:

1. 添加对 System.Windows.Forms 的引用

在你的WPF项目中,你需要添加对System.Windows.Forms的引用。这可以通过项目的"引用"对话框来完成,或者在项目文件中添加以下代码:

xml 复制代码
<Reference Include="System.Windows.Forms" />

2. 在 App.xaml.cs 中创建 NotifyIcon

你需要在应用程序的启动时创建托盘图标,并在应用程序退出时正确地清理资源。

csharp 复制代码
using System;
using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Application;

namespace YourNamespace
{
    public partial class App : Application
    {
        private NotifyIcon _notifyIcon;
        private bool _isExit;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            CreateTrayIcon();
        }

        private void CreateTrayIcon()
        {
            _notifyIcon = new NotifyIcon();
            _notifyIcon.DoubleClick += (s, args) => ShowMainWindow();
            _notifyIcon.Icon = new System.Drawing.Icon("YourIconPath.ico");
            _notifyIcon.Visible = true;

            CreateContextMenu();
        }

        private void CreateContextMenu()
        {
            _notifyIcon.ContextMenuStrip = new ContextMenuStrip();
            _notifyIcon.ContextMenuStrip.Items.Add("Open").Click += (s, e) => ShowMainWindow();
            _notifyIcon.ContextMenuStrip.Items.Add("Exit").Click += (s, e) => ExitApplication();
        }

        private void ShowMainWindow()
        {
            if (MainWindow.IsVisible)
            {
                if (MainWindow.WindowState == WindowState.Minimized)
                {
                    MainWindow.WindowState = WindowState.Normal;
                }
                MainWindow.Activate();
            }
            else
            {
                MainWindow.Show();
            }
        }

        private void ExitApplication()
        {
            _isExit = true;
            MainWindow.Close();
            _notifyIcon.Dispose();
            _notifyIcon = null;
        }

        protected override void OnExit(ExitEventArgs e)
        {
            if (!_isExit)
            {
                _notifyIcon.Dispose();
                _notifyIcon = null;
            }

            base.OnExit(e);
        }
    }
}

3. 设置 NotifyIcon 图标和菜单

在上面的代码中,我们设置了托盘图标的图标路径,并且定义了一个方法来创建上下文菜单。上下文菜单有两个选项:"Open"打开应用程序窗口,"Exit"退出应用程序。

4. 处理窗口关闭事件

如果你想在用户尝试关闭窗口时最小化到托盘而不是完全退出应用程序,你需要在你的主窗口的代码后面处理Closing事件。

csharp 复制代码
protected override void OnClosing(CancelEventArgs e)
{
    if (!_isExit)
    {
        e.Cancel = true;
        Hide(); // 隐藏主窗口
        // 可选:显示一条通知消息
        _notifyIcon.ShowBalloonTip(1000, "Application", "Application has been minimized to tray.", ToolTipIcon.Info);
    }
    base.OnClosing(e);
}

请确保你的应用程序有一个有效的图标文件,并且图标路径是正确的。此外,如果你的应用程序是.NET Core或.NET 5+项目,你还需要确保System.Windows.Forms的兼容性,并可能需要添加对Microsoft.Windows.Compatibility包的引用。

以上步骤将在应用程序中创建一个系统托盘图标,并且当用户尝试关闭窗口时,应用程序将最小化到系统托盘而不是完全退出。

相关推荐
donk13146 小时前
React Native for OpenHarmony 实战:三方库 react-native-circular-action-menu 的鸿蒙化适配指南
javascript·分布式·react native·react.js·wpf·harmonyos
donk13141 天前
React Native for OpenHarmony 实战:三方库 react-native-step-indicator 的鸿蒙化适配指南
分布式·react native·react.js·wpf·harmonyos
He BianGu1 天前
【笔记】WPF中RenderOptions类的详细功能
笔记·wpf
VidDown1 天前
上线三个月后的第一次事故:一个 Django 服务的日志、监控与故障复盘
数据库·python·django·wpf·音视频·视频编解码
He BianGu1 天前
【笔记】WPF中CachingHint的功能
笔记·wpf
He BianGu1 天前
【笔记】在WPF中BitmapScalingMode的作用
笔记·wpf
Vae_Mars2 天前
幂等性学习笔记
笔记·学习·wpf
samble3 天前
从零搭建灌装监控系统(十九):单元测试实战,xUnit + 内存SQLite + 反射测试私有方法
c#·wpf·mvvm·modbus·工业控制
光泽雨4 天前
路由概念和路由事件 继承链路 RadioButton的事件解析
wpf
samble4 天前
从零搭建灌装监控系统(十八):自定义控件,三色灯与状态指示
c#·wpf·mvvm·modbus·工业控制