• WPF如何实现应用程序托盘


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

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

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

    <Reference Include="System.Windows.Forms" />
    
    • 1

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

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

    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);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    3. 设置 NotifyIcon 图标和菜单

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

    4. 处理窗口关闭事件

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

    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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

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

  • 相关阅读:
    嵌入式软件架构设计-函数调用
    【C语言】指针与动态内存
    Hugging Face使用Stable diffusion Diffusers Transformers Accelerate Pipelines
    【spring源码系列】之【FactoryBean类型的接口】
    Maven 快速入门
    VBA驱动SAP GUI实现办公自动化(一)
    数字政府一网统管体系下的运维管理软件应用探讨
    C++ 时间函数整理详解
    程序员面试金典 - 面试题 17.18. 最短超串
    【王道代码】【2.3链表】d4
  • 原文地址:https://blog.csdn.net/yao_hou/article/details/134471136