当前位置: 首页 > news >正文

WPF如何实现应用程序托盘

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

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

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

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

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

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

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

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

http://www.lryc.cn/news/233407.html

相关文章:

  • ERROR: column “xxxx.id“ must appear in the GROUP BY
  • 【C++ 学习 ㊲】- 五种特殊类的设计
  • 探索arkui(2)--- 布局(列表)--- 2(支持分组/实现响应滚动位置)
  • systemverilog:interface中端口方向理解
  • 【GUI】-- 08 JButton、JRadioButton、JCheckBox
  • 【postgresql】CentOS7 安装Pgweb
  • 基于python和定向爬虫的商品比价系统
  • 使用GPT-4训练数据微调GPT-3.5 RAG管道
  • 二十三种设计模式全面解析-深入解析模板方法模式的奇妙世界
  • 【Spring】加载properties文件
  • react中间件的理解
  • React函数组件状态Hook—useState《进阶-对象数组》
  • linux 网络 cat /proc/net/dev 查看测试网络丢包情况
  • 记录配置VS,使用opencv与Eigen
  • uart控制led与beep
  • Linux修改root密码
  • C/C++模板类模板与函数模板区别,以及用法详解
  • van-dialog弹窗异步关闭-校验表单
  • Dynamic Wallpaper 16.7中文版
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • 《网络协议》06. HTTP 补充 · HTTPS · SSL/TLS
  • Python winreg将cmd/PowerShell(管理员)添加到右键菜单
  • redis运维(九)字符串(二)字符串过期时间
  • 【C++】多线程的学习笔记(3)——白话文版(bushi
  • kotlin--3.集合操作
  • 自动驾驶-BEV感知综述
  • 面试题-3
  • C++ Core Guidelines 中文版 GSL
  • 同时显示上下两层凸包特征的可视化程序
  • Django框架之模型层(二)